Friday 2 August 2013

Sync AD info with user information in AX

Recently I created this job to synchronize information from Active Directory with AX. We needed to update the e-mail address for all the employees in AX after a rebranding. 

Below you find the code, as you can see, we update the e-mail address but as an example I also update name of the employee. Also you could add the check if a user is still active in AD and if not, disable the user account in AX as well. You could extend the job to also update the email address on the employee information. 

static void JLH_SyncADwithAX(Args _args)
{
    UserInfo                            userInfo;
    xAxaptaUserManager                  axUsrMgr;
    xAxaptaUserDetails                  axUsrDet;
    Boolean                             doUpdate;
    SysUserInfo                         sysUserInfo;
    SysCompanyUserInfo                  sysCompanyUserInfo;
    #define.NewDomain("contoso.com")    // Fill in your domain
    ;

    doUpdate = Box::yesNo("Update users with new info?", DialogButton::No) == DialogButton::Yes;
    axUsrMgr = new xAxaptaUserManager();

    ttsbegin;
    setPrefix('Update AD userinfo');

    while select forupdate userInfo
    where userinfo.id != "Guest"
    {
        axUsrDet = axUsrMgr.getDomainUser(#NewDomain, userInfo.networkAlias);
        
        if(userInfo && axUsrDet)
        {
            sysUserInfo.selectForUpdate(true);
            sysUserInfo = SysUserInfo::find(userinfo.id,true);
            sysUserInfo.Email           = axUsrDet.getUserMail(0);
            userInfo.name               = axUsrDet.getUserName(0);
            userInfo.enable             = axUsrDet.isUserEnabled(0);

            if(doUpdate)
            {
                userInfo.update();
                sysUserInfo.update();
                info(strfmt("Updated for: %1, Name: %2, E-mail: %3", userInfo.Id, userInfo.name, sysUserInfo.Email));
            }
            else
            {
                info(strfmt("Not updated for: %1, Name: %2, E-mail: %3", userInfo.Id, userInfo.name, sysUserInfo.Email));
            }
        }
        else
        {
            error(strfmt("User not found: %1", userInfo.Id));
        }
    }
    ttscommit;
}


No comments:

Post a Comment