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 :
Microsoft Dynamics 365 | Integration, Dataverse...
Answered

optionset visibility based on security role

(1) ShareShare
ReportReport
Posted on by 6
Help me out of Working with Optionsets for example letz say I have an optionset with values A1,A2,A3,A4 and based on User security role I want to show A1,A2 to User 1 and A3,A4 to User 2
I have the same question (0)
  • Suggested answer
    RudyZhang Profile Picture
    Microsoft Employee on at
    Hi,
     
    Your request to control the content of the option set through security roles is not supported.
     
    Our team, at first, considered field security, but field security can only control the entire field and not the specific options in the field.
    Later, based on an article on the D365 DEMYSTIFIED website, which mentions the use of JavaScript code to hide specific options in an option set, the link to the article is as follows.
    https://d365demystified.com/2023/01/10/hide-options-from-optionset-using-javascript-in-dynamics-365-crm/
     
    Based on this article we propose the following possible solutions
    Step1, get the security role of the current user, because a user may have more than one security role, we will use the form of traversal.
    Step2, compare the security role with the set security roles.
    Step3, according to the comparison result, replace the corresponding option value.
     
    Below is some sample code, please note that the JavaScript code needs to replace "Role1 GUID" and "Role2 GUID" with the GUIDs of the actual roles you want to check and replace "new_optionsetfieldname" with the logical name of your option set field. This code should be added to the form's web resource and the "Onload" event should be set in the form's "Form Properties" to call the onload function.
    function onLoad() {
        var currentUserRoles = Xrm.Utility.getGlobalContext().userSettings.roles;
        var userHasRole1 = false;
        var userHasRole2 = false;
      
        // Replace "Role1 GUID" and "Role2 GUID" with the GUIDs of the roles you want to check
        var role1GUID = "Role1 GUID";
        var role2GUID = "Role2 GUID";
      
        // Check if the current user has Role 1
        for (var i = 0; i < currentUserRoles.length; i++) {
          if (currentUserRoles[i].id === role1GUID) { // Use currentUserRoles[i].id here
            userHasRole1 = true;
            break;
          }
        }
      
        // Check if the current user has Role 2
        for (var i = 0; i < currentUserRoles.length; i++) {
          if (currentUserRoles[i].id === role2GUID) { // Use currentUserRoles[i].id here
            userHasRole2 = true;
            break;
          }
        }
      
        // Get the option set field
        var optionSetField = Xrm.Page.getAttribute("new_optionsetfieldname");
      
        if (userHasRole1) {
          // Set the option set values for User 1
          optionSetField.setValue(100000000); // A1
          optionSetField.setValue(100000001); // A2
        } else if (userHasRole2) {
          // Set the option set values for User 2
          optionSetField.setValue(100000002); // A3
          optionSetField.setValue(100000003); // A4
        }
      }
      

     
     
    I hope my answer is helpful to you! If you have any other questions, please feel free to contact me.
     
    Best Regards,
    Rudy Zhang

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 > Microsoft Dynamics 365 | Integration, Dataverse, and general topics

#1
Martin Dráb Profile Picture

Martin Dráb 49 Most Valuable Professional

#2
André Arnaud de Calavon Profile Picture

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

#3
#ManoVerse Profile Picture

#ManoVerse 31

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans