07 December 2018

D365 F&O | UserInfo script to update networkDomain, IdentityProvider and SID for all users after DB copy

This runable class is really helpful when you copy a DB between environments with different tenants, different ADFS instances or if you messed up the SIDs 


using Microsoft.Dynamics.ApplicationPlatform.Environment;

class TWIXUserInfoUpdate
{           
    public static void main(Args _args)
    {     
        UserInfo            updateUserInfo;       
        NetworkDomain       updatedNetworkDomain;

        // Get network domain
        updatedNetworkDomain = EnvironmentFactory::GetApplicationEnvironment().get_Provisioning().get_AdminIdentityProvider();
       
        ttsbegin;
        TWIXUserInfoUpdate::callInsertUpdateRelatedSkipMethods(updateUserInfo);

        // Update network alias and network domain for all non admin users
        update_recordset updateUserInfo
            setting 
                networkDomain = updatedNetworkDomain,
IdentityProvider = updatedNetworkDomain
            where updateUserInfo.id != 'Admin' 
               && updateUserInfo.isMicrosoftAccount == 0
               && updateUserInfo.accountType == UserAccountType::ClaimsUser;
              
// Update SID
        while select forupdate updateUserInfo
            where updateUserInfo.id != 'Admin'
               && updateUserInfo.networkAlias
               && updateUserInfo.networkDomain == updatedNetworkDomain
               && updateUserInfo.isMicrosoftAccount == 0
               && updateUserInfo.accountType == UserAccountType::ClaimsUser
        {            
            AxaptaUserManager manager = new AxaptaUserManager();
            xAxaptaUserDetails userDetails = 
                        manager.getSIDFromName(updateUserInfo.networkAlias, updatedNetworkDomain, updateUserInfo.accountType);
            updateUserInfo.sid = userDetails.getUserSid(0);
            updateUserInfo.update();           
        }
        ttscommit;
       
    }

    public static void callInsertUpdateRelatedSkipMethods(Common _buffer)
    {
        //BP deviation documented
        _buffer.skipAosValidation(true);
        _buffer.skipDatabaseLog(true);
        _buffer.skipDataMethods(true);
        //needed in case of crossCompany
        _buffer.skipEvents(true);
    }
}

No comments: