Search through blog..

Monday, September 1, 2014

Code: X++ script to disable AX users

If you are looking for script to quickly disable AX users. This way you can keep people out of the AOS without actually needing to remove the user. 
 
Three approaches for the code:
1. Simple Job to disable the specified user in UserInfo table.
static void disableUsers(Args _args)
{
    UserInfo userInfo;
    ;
    //a00008 is the user id which we want to disable.
    while select forUpdate userInfo where userInfo.id == 'a00008'
    {
        if(userInfo)
        {
            ttsBegin;
            
            userInfo.enable = 0;
            userInfo.update();
            
            ttsCommit;
        }
    }
}

 
2. A step more, the below job uses a Container to store multiple user entries and disables all of them in one go.  
 
static void disableListOfUsers(Args _args)
{
    UserInfo    userInfo;
    //list of users to be disabled
    container   userList = ['a00225', 'a03559', 'a03971']; 
    int         x, noOfRecords;
    str 8       userId;
    ;
    
    noOfRecords = conLen(userList);
    for(x=0; x<noOfRecords; x++)
    {
        userId = conPeek(userList, x);
        while select forUpdate userInfo where userInfo.id == userId
        {
            if(userInfo)
            {
                ttsBegin;
                userInfo.enable = 0;
                userInfo.update();
                ttsCommit;
                info(strFmt("%1 : User updated", userInfo.name));
            }
        }
    }

}

 
3. Excel (csv file) having user details is a prerequisite here, which can be generated using Excel Addins (Ctrl+T from Users form). This job reads saved .csv file from specified location and disables all user list in the file. 
static void disableUserFromCSV(Args _args)
{
 
    #File   //needed for use of Macro #io_read
    UserInfo            userInfo;
    CommaTextIo         commaTextIO;
    FileIOPermission    permission;
    container           userList;
    str                 fpath;
    str 8               userId;
    ;
 
    //File consisting of list of users with columns {userid, username, enable}
    fpath = "D:\\Temp\\Code\\UserList.csv";
    permission = new FileIOPermission(fpath, #io_read);
    permission.assert();
 
    commaTextIO = new commaTextIO(fpath, 'R');
    commaTextIO.inRecordDelimiter('\n');
    commaTextIO.inFieldDelimiter(';');
    userList = commaTextIO.read();
    userList = commaTextIO.read(); //To overwrite the header read.
    while(userList)
    {
        userId = conPeek(userList, 1);
        
        while select forUpdate userInfo where userInfo.id == userId
        {
            //strReplace() is used to replace the Carriage return with space, so that the validations act properly
     //Checking if enable is already False, if it is control shouldn't go into the loop.
            if(userInfo && strReplace(conPeek(userList,3), '\r', '') == "TRUE") 
            {
                ttsBegin;
                userInfo.enable = 0;
                userInfo.update();
                ttsCommit;
                info(strFmt("%1 : User updated", userInfo.name));
            }
        }
        userList = commaTextIO.read();        
    }
}
Post a Comment