web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Finance | Project Operations, Human Resources, ...
Suggested Answer

Assign a security role along with organisation using x++

(0) ShareShare
ReportReport
Posted on by 390

Hey Team,

I'm have assigned a security role to an associate I'm adding using x but I also want to add an organisation along with the role. I want to do it using x . Please can you suggest me how to do this as I'm very confused. 

Following is my code for assigning the role to the associate. Please help me in assigning the organisation to the associate along with the role as It's a bit confusing. I could only figure out that the table related to assigning the organisation is "omUserRoleOrganization"

 select role where role.Name=="Accountant";
                select * from userRole
                      where userRole.SecurityRole == role.RecId && userRole.User == systemuser.UserID;

                userRole.User = systemuser.UserID;

                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, systemuser.UserId));

I have the same question (0)
  • WillWU Profile Picture
    22,361 on at

    Hi skd,

    Please go to  Forms \ SysSecRoleAssignOM to check the standard code.

    public void addOrganization(boolean _withChildren)
        {
            OMUserRoleOrganization org;
            FormTreeItem item;
            TreeItemIdx idx = organizationTree.getFirstSelected();
            RefRecId organizationId;
    
            if (!idx)
                return;
    
            item = organizationTree.getItem(idx);
            organizationId = item.data();
    
            if (isUserRoleMode)
            {
                select firstonly org
                where
                    org.User == userId &&
                    org.SecurityRole == roleId &&
                    org.omInternalOrganization == organizationId &&
                    org.omHierarchyType == hierarchyId;
            }
            else
            {
                select firstonly org
                where
                    org.SecurityRoleAssignmentRule == assignmentRuleId &&
                    org.omInternalOrganization == organizationId &&
                    org.omHierarchyType == hierarchyId;
            }
    
            if (org)
                return;
    
            // Check the id of the role to add
            if ((roleId == 0) && (assignmentRuleId == 0))
            {
                error("@SYS338939"); // Error used role is not assigned to selected user
                return;
            }
    
            ttsbegin;
            org.User = userId;
            org.SecurityRole = roleId;
            org.SecurityRoleAssignmentRule = assignmentRuleId;
            org.omInternalOrganization = organizationId;
            org.omHierarchyType = (_withChildren) ? hierarchyId : 0;
    
            element.userRoleChange(org.SecurityRole,
                                    org.omInternalOrganization,
                                    org.User,
                                    AddRemove::Add);
    
            org.insert();
            ttscommit;
            omUserRoleOrganization_ds.research(true);
            element.enableButtons();
            element.updatePart();
        }
    

    Hope this helps.

  • Suggested answer
    vinitgoyal2005 Profile Picture
    6,332 on at

    Hi skd,

    As mentioned by Will, you can check the standard code but the question is why are you trying to do this in X++?

    how many users do you have and by doing this hardcoding in the x++, how many users are applicable for this code?

    The entire point of security is to be user-configurable, you might spend less time doing it manually from UI than working on X++ unless you have a ton of users.

  • Sukrut Parab Profile Picture
    71,710 Moderator on at

    Whats the purpose behind having  such a requirement where you want to assign security role through code ? You have other options like automatic role assignments , assigning roles through UI etc.. Could you please explain business scenario ?

  • skd Profile Picture
    390 on at

    Hey Sukrut Parab,

    Actually we are doing this to add associates to our system it requires to fill in many other tables but we have developed a custom form where we are only taking in the basic information of the associate and on submitting the data we are populating all other tables using x++ code to make the work easier as all the data to other tables is static and therefore assigning role with organisation is also a part of it.

  • André Arnaud de Calavon Profile Picture
    300,911 Super User 2025 Season 2 on at

    Hi skd,

    You asked a quite similar question before: community.dynamics.com/.../add-role-through-x

    Here you got the reply that it would be possible to use automatic role assignment. The automatic role assignment rules can also be created using your custom table joined with the UserInfo table. This saves you some development effort and you will have some manual override options.

  • alexmeyer.itguy Profile Picture
    648 on at

    The tables for assign user roles and user role organizations are SecurityUserRole and SecurityUserRoleCondition.

    You will first need to create the user role assignment in the SecurityUserRole table and then take the RecId from that record and use that to create your entries in the SecurityUserRoleCondition table.

    If no entry is made in the SecurityUserRoleCondition table for a SecurityUserRole entry, then that user gets the role access across all legal entities.

Under review

Thank you for your reply! To ensure a great experience for everyone, your content is awaiting approval by our Community Managers. Please check back later.

Helpful resources

Quick Links

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Finance | Project Operations, Human Resources, AX, GP, SL

#1
Martin Dráb Profile Picture

Martin Dráb 664 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 522 Super User 2025 Season 2

#3
Sohaib Cheema Profile Picture

Sohaib Cheema 303 User Group Leader

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans