Thursday 5 September 2019

Creating User and Assign a Role using X++

static void uploadUserDetails(Args _args)
{
    CommaTextIO                 csvFile;
    container                   readCon;
    Dialog                      dialog;
    DialogField                 dfFileName;
    FileName                    fileName;
    VendTable                   vendTable;
    DirParty                    dirParty;
    DirPartyRecId               partyRecId;
    AccountNum                  accountNum;
   
    DirPartyContactInfoView     contactView;
     SecurityRole        role,userRole_upload;

    SecurityUserRole    userRole;

    UserInfo userInfo,userInfo_loc;

    xAxaptaUserManager Axmanage;

    xAxaptaUserDetails Axdetails;
   
    #File
   
    dialog = new Dialog("Pick the file");
    dfFileName = dialog.addField(extendedTypeStr("FilenameOpen"));
    dialog.filenameLookupFilter(['csv','*.csv']);
   
    if (dialog.run())
    {
        csvFile = new CommaTextIo(dfFileName.value(), 'r');
        csvFile.inFieldDelimiter(',');
        readCon = csvFile.read();
       
        while(csvFile.status() == IO_Status::OK)
        {
            readCon = csvFile.read();

            if(readCon)
            {
               
    Axmanage = new xAxaptaUserManager();

    userInfo.accountType = UserAccountType::ADUser;

    userInfo.networkAlias = conPeek(readCon,9);

    userInfo.networkDomain = conPeek(readCon,10);

    userInfo.id = conPeek(readCon,6);

    userInfo.name = conPeek(readCon,8);

    userInfo.company = conPeek(readCon,1);

    userInfo.enable = conPeek(readCon,2);

     select userInfo_loc where userInfo_loc.id == userInfo.id;        
     if(!userInfo_loc)           
     {
        // To get SID of user, without this user will not be able to login to ax, just user will be shown into user form

            Axdetails = Axmanage.getSIDFromName(userInfo.id,userInfo.NetworkDomain,UserAccountType::ADUser);

            userInfo.sid = Axdetails.getUserSid(0);
               
         info(strFmt("Role %1 added to the user %2 successfully.", role.Name, userInfo.id));
  
            userInfo.insert();
      }

   userRole_upload.name =conPeek(readCon,13);

    select role where role.Name == userRole_upload.name;



    select * from userRole

            where userRole.SecurityRole == role.RecId &&

                userRole.User == userInfo.id;



    userRole.User = userInfo.id;

    userRole.SecurityRole = role.RecId;

    userRole.AssignmentMode = RoleAssignmentMode::Manual;

    userRole.AssignmentStatus = RoleAssignmentStatus::Enabled;

    SecuritySegregationOfDuties::assignUserToRole(userRole, null);



    info(strFmt("Role %1 added to the user %2 successfully.", role.Name, userInfo.id));

            
             
            }
        }
    }
}