Microsoft_MVP_banner

Assigning security roles to new AX user was never that easy !!!

Some time before I got a chance to write on a utility to make user role assignment a much easier….on ONE click.

For example; a new accountant joined your organization and CFO asked AX System Admin to add this new user into AX and give him/her same permissions as other accountants have in AX. Without this utility AX system Admin will open existing user roles in a seperate window and add these roles for new joined users….Is it not the time consuming activity when you have to add dozens of roles.
This utiltiy not limited to copy user roles but can copy User Groups and user Options too.

Some important methods of this utility are;
Create User lookup for FROM USER

public void
fromUserLookup(FormStringControl userLookupControl)
{
   
Query                   qry = new Query();
   
QueryBuildDataSource    qbd;
    QueryBuildRange         qbr;
   
SysTableLookup         
sysTableLookup;
   
Userinfo                userInfo;
   
systablelookup = SysTableLookup::newParameters(tableNum(UserInfo), userLookupControl);
   
SysTableLookup.addLookupfield(fieldNum(UserInfo,
ID));
   
SysTableLookup.addLookupfield(fieldNum(UserInfo,
NetworkAlias));
   
SysTableLookup.addLookupfield(fieldNum(UserInfo,
Name));
    qbd =
qry.addDataSource(tableNum(userInfo));
   
qbd.addRange(fieldNum(UserInfo,Company)).value(curext());
   
sysTableLookup.parmQuery(qry);
   
sysTableLookup.performFormLookup();
}
Create User lookup for TO USER

public void
toUserLookup(FormStringControl userLookupControl)
{
   
Query                   qry = new Query();
   
QueryBuildDataSource    qbd;
   
QueryBuildRange         qbr;
   
SysTableLookup         
sysTableLookup;
   
Userinfo                userInfo;
   
systablelookup = SysTableLookup::newParameters(tableNum(UserInfo), userLookupControl);
   
SysTableLookup.addLookupfield(fieldNum(UserInfo,
ID));
   
SysTableLookup.addLookupfield(fieldNum(UserInfo,
NetworkAlias));
   
SysTableLookup.addLookupfield(fieldNum(UserInfo,
Name));
    qbd =
qry.addDataSource(tableNum(userInfo));
   
qbd.addRange(fieldNum(UserInfo,Company)).value(curext());
    qbr =
qbd.addRange(fieldNum(UserInfo,
Enable));
   
qbr.value(‘1’);
   
sysTableLookup.parmQuery(qry);
   
sysTableLookup.performFormLookup();

}


Function to copy USER ROLES

/// <summary>
/// copy roles
assigned to one user to another
/// </summary>
/// <returns>
/// true; roles are
copied across. false; roles failed to copied
/// </returns>
/// <remarks>
/// this method is
used to copy user roles assigned to one user to another user.
/// </remarks>
private boolean
copyUserRoles()
{
    boolean                 ret = true;
   
SecurityRole           
securityRole;
   
SecurityUserRole       
securityUserRole;
   
SecurityUserRole       
securityUserRoleExist;
   
SecurityUserRole       
securityUserRoleInsert;
   
OMUserRoleOrganization 
userRoleOrganization, userRoleOrganization_Insert;
    List                    copiedUserRoles = new List(Types::String);
   
ListEnumerator         
lEnumerator;
    setPrefix(strFmt(“Copy user”, fromUser, toUser));
    try
    {
        select securityRole where securityRole.AotName == ‘SystemUser’;
        delete_from securityUserRole where securityUserRole.User == toUser
&& securityUserRole.SecurityRole == securityRole.RecId;
       
        while select
securityUserRole
               
where securityUserRole.User
== fromUser
            notExists join
* from securityUserRoleExist
               
where
securityUserRoleExist.SecurityRole    ==
securityUserRole.SecurityRole
                   
&& securityUserRoleExist.User           == toUser
        {
            select securityRole where securityRole.RecId ==
securityUserRole.SecurityRole;
           
copiedUserRoles.addStart(securityRole.Name);
           
securityUserRoleInsert.initValue();
           
securityUserRoleInsert.SecurityRole = securityUserRole.SecurityRole;
           
securityUserRoleInsert.User        
= toUser;
           
securityUserRoleInsert.insert();
           
securityUserRoleInsert.clear();
            while select
userRoleOrganization
                   
where
userRoleOrganization.User == fromUser
                        &&
userRoleOrganization.SecurityRole == securityUserRole.SecurityRole
            {
               
userRoleOrganization_Insert.initValue();
                userRoleOrganization_Insert.OMHierarchyType             =
userRoleOrganization.OMHierarchyType;
               
userRoleOrganization_Insert.OMInternalOrganization      =
userRoleOrganization.OMInternalOrganization;
               
userRoleOrganization_Insert.SecurityRole                =
userRoleOrganization.SecurityRole;
               
userRoleOrganization_Insert.SecurityRoleAssignmentRule  =
userRoleOrganization.SecurityRoleAssignmentRule;
               
userRoleOrganization_Insert.User       
                = toUser;
               
userRoleOrganization_Insert.insert();
               
userRoleOrganization_Insert.clear();
            }
        }
    }
    catch
    {
        ret =
false;
    }
    if (ret)
    {
       
lEnumerator = copiedUserRoles.getEnumerator();
        if (copiedUserRoles.empty())
           
info(strFmt(User %1 and %2 have already the same user role,fromUser,
toUser));
        while (lEnumerator.moveNext())
        {
           
info(strFmt(‘%1’,lEnumerator.current()));
        }
    }
    else
       
error(strFmt(User Roles aborted please review list));
    return ret;

}


Function to copy USER OPTIONS
/// <summary>
/// copy options
assigned to one user to another
/// </summary>
/// <returns>
/// true; options are
copied across. false; options failed to copied
/// </returns>
/// <remarks>
/// this method is
used to copy user’s options assigned to one user to another user.
/// </remarks>
private boolean
copyUserOptions()
{
    boolean                 ret = true;
   
UserInfo               
userInfoSource;
   
UserInfo               
userInfoTarget;
   
SysUserInfo            
sysUserInfoSource;
   
SysUserInfo            
sysUserInfoTarget;
    setPrefix(strFmt(Copy user options, fromUser,
toUser));
    try
    {
        select userInfoSource
            where userInfoSource.id == fromUser
        join sysUserInfoSource
            where sysUserInfoSource.Id ==
userInfoSource.id;
        ttsBegin;
           
            select forUpdate
userInfoTarget where
userInfoTarget.id == toUser;
           
userInfoTarget.filterByGridOnByDefault =
userInfoSource.filterByGridOnByDefault;
           
userInfoTarget.statuslineInfo = userInfoSource.statuslineInfo;
           
userInfoTarget.update();
           
            select forUpdate
sysUserInfoTarget where
sysUserInfoTarget.Id == toUser;
           
sysUserInfoTarget.DefaultCountryRegion =
sysUserInfoSource.DefaultCountryRegion;
           
sysUserInfoTarget.update();
        ttsCommit;
    }
    catch
    {
        ret =
false;
    }
    if (ret)
    {
        info(strFmt(User %1
and %2 have already the same user options
,
fromUser, toUser));
    }
    else
       
error(strFmt(User Options aborted please review list ));
    return ret;

}


Function to copy USER GROUPS
/// <summary>
/// copy groups
assigned to one user to another
/// </summary>
/// <returns>
/// true; groups are
copied across. false; groups failed to copied
/// </returns>
/// <remarks>
/// this method is
used to copy user groups assigned to one user to another user.
/// </remarks>
private boolean
copyUserGroups()
{
    boolean                 ret = true;
   
UserGroupList          
userGroupList;
   
UserGroupList          
userGroupListExist;
   
UserGroupList          
userGroupListInsert;
    List                    copiedGroups = new List(Types::String);
   
ListEnumerator         
lEnumerator;
    setPrefix(strFmt(“Copy user groups”, fromUser,
toUser));
    try
    {
        while select
userGroupList
               
where userGroupList.userId ==
fromUser
            notExists join
* from userGroupListExist
               
where
userGroupListExist.groupId == userGroupList.groupId
                   
&& userGroupListExist.userId == toUser
        {
           
copiedGroups.addStart(userGroupList.groupId);
           
userGroupListInsert.initValue();
           
userGroupListInsert.groupId = userGroupList.groupId;
           
userGroupListInsert.userId  =
toUser;
           
userGroupListInsert.insert();
           
userGroupListInsert.clear();
        }
    }
    catch
    {
        ret =
false;
    }
    if (ret)
    {
        lEnumerator =
copiedGroups.getEnumerator();
        if (copiedGroups.empty())
           
info(strFmt(“User %1 and %2 have already the same user Groups
,fromUser, toUser));
        while (lEnumerator.moveNext())
        {
           
info(strFmt(‘%1’,lEnumerator.current()));
        }
    }
    else
       
error(strFmt(“User Groups aborted please review list “));
    return ret;

}

Share:

Related Posts

Microsoft Copilot
admin

Exploring Microsoft Copilot Architecture

Microsoft Copilot isn’t just another AI tool; it’s a comprehensive platform designed to be your indispensable companion, enhancing productivity, fostering creativity, and facilitating information comprehension all through a user-friendly chat interface. The concept of Copilot emerged two years ago when Microsoft introduced GitHub Copilot, aiming to assist developers in writing

Read More »
How to enable new Microsoft teams - Public Preview!
Microsoft Teams
Faisal Fareed

How to enable new Microsoft teams – Public Preview!

New Microsoft Teams is just AWESOME, quick but useful post below shows how you have this preview feature to make your life EASY!  Open Microsoft Teams admin center [Ask admin in your organization if you don’t have access] and follow path Teams > Teams update policies > Click on an existing

Read More »

Send Us A Message

Leave a Reply

Your email address will not be published. Required fields are marked *

Recent Post

Exploring Microsoft Copilot Architecture

Exploring Microsoft Copilot Architecture

Microsoft Copilot isn’t just another AI tool; it’s a comprehensive platform designed to be your indispensable companion, enhancing productivity, fostering creativity, and facilitating information comprehension all through a user-friendly chat interface. The concept of Copilot emerged two years ago when Microsoft introduced GitHub Copilot, aiming to assist developers in writing…

How to enable new Microsoft teams – Public Preview!

How to enable new Microsoft teams – Public Preview!

New Microsoft Teams is just AWESOME, quick but useful post below shows how you have this preview feature to make your life EASY!  Open Microsoft Teams admin center [Ask admin in your organization if you don’t have access] and follow path Teams > Teams update policies > Click on an existing…

Electronic Reporting: Send vendor payments to external azure storage via X++

Electronic Reporting: Send vendor payments to external azure storage via X++

Electronic Reporting module in Microsoft Dynamics 365 Finance Operation lets you archive file generated by ER at SharePoint location and in Azure Storage as per this link Archive ER destination type – Finance & Operations | Dynamics 365 | Microsoft Learn. APIs can be used to check message status and read…

FaisalFareed@2025. All rights reserved

Design by T3chDesigns