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 CRM (Archived)

Hide Options from Option Set based on Security role

(0) ShareShare
ReportReport
Posted on by

Hi All , 

I need to hide some options from an option set ( Status Reason with schema name statuscode ) based on the security role . For example : if a user who has "Schedule Manager" security role and he/she opens a new opportunity , they should only be able to see some of the options from the Status Reason dropdown.

The code I wrote is :

<script>

var UserSecurityRoles= Xrm.Page.context.getUserRoles();
var userRoleName = "{997C4C00-BF39-E011-8DA3-1CC1DE72E35E}"


// Checking if Security Role Id found in User Security Roles

for (var x = 0; x < UserSecurityRoles.length; x++) {


if (UserSecurityRoles[x]==userRoleName)


{
Xrm.Page.getControl("statuscode").removeOption(100,000,003);

Xrm.Page.getControl("statuscode").removeOption(279640012);
Xrm.Page.getControl("statuscode").removeOption(100000026);
Xrm.Page.getControl("statuscode").removeOption(100000047);
Xrm.Page.getControl("statuscode").removeOption(100000005);
Xrm.Page.getControl("statuscode").removeOption(279640006);
Xrm.Page.getControl("statuscode").removeOption(279640011);
Xrm.Page.getControl("statuscode").removeOption(279640007);
Xrm.Page.getControl("statuscode").removeOption(100000017);
Xrm.Page.getControl("statuscode").removeOption(100000018);

}
}

</script>

*This post is locked for comments

I have the same question (0)
  • Community Member Profile Picture
    on at

    Apologies ! Yes I am getting error as XRM undefined plus not getting the results .

    I changed it to without commas , still not working What do you think I am doing wrong ?

  • Suggested answer
    anilambadan Profile Picture
    1,160 on at

    Are you trying to call it from a webresources(html)? if so try to call it with a code like parent.Xrm or window.parent.Xrm

  • Community Member Profile Picture
    on at

    Hi , no its a javascript type . Name will be wbah_teststatusreason .

    library name : test

    I changed the script again which is still not working :

    function test()

    {

    var UserSecurityRoles= Xrm.Page.context.getUserRoles();

    var userRoleName = `997C4C00-BF39-E011-8DA3-1CC1DE72E35E`

    // Checking if Security Role Id found in User Security Roles

    for (var x = 0; x < UserSecurityRoles.length; x++) {

     if (UserSecurityRoles[x]==userRoleName)

    {

    Xrm.Page.getControl("statuscode").removeOption(100000003);

    Xrm.Page.getControl("statuscode").removeOption(279640012);

    Xrm.Page.getControl("statuscode").removeOption(100000026);

    Xrm.Page.getControl("statuscode").removeOption(100000047);

    Xrm.Page.getControl("statuscode").removeOption(100000005);

    Xrm.Page.getControl("statuscode").removeOption(279640006);

    Xrm.Page.getControl("statuscode").removeOption(279640011);

    Xrm.Page.getControl("statuscode").removeOption(279640007);

    Xrm.Page.getControl("statuscode").removeOption(100000017);

    Xrm.Page.getControl("statuscode").removeOption(100000018);

       }

    }

    }

  • Suggested answer
    Community Member Profile Picture
    on at

    Hi ,

    Please check and use below code for your requirement, u can change as per ur business logic.
    Also attach JQuery&Json2 file on form.

    function HideOptions() {

    if (CheckUserRole("Schedule Manager") == True)
         {
            Xrm.Page.getControl("statuscode").removeOption(100000003);
            Xrm.Page.getControl("statuscode").removeOption(279640012);
            Xrm.Page.getControl("statuscode").removeOption(100000026);
            Xrm.Page.getControl("statuscode").removeOption(100000047);
            Xrm.Page.getControl("statuscode").removeOption(100000005);
            Xrm.Page.getControl("statuscode").removeOption(279640006);
            Xrm.Page.getControl("statuscode").removeOption(279640011);
            Xrm.Page.getControl("statuscode").removeOption(279640007);
            Xrm.Page.getControl("statuscode").removeOption(100000017);
            Xrm.Page.getControl("statuscode").removeOption(100000018);
        }                
    }

    function CheckUserRole(RoleName) {
        var currentUserRoles = Xrm.Page.context.getUserRoles();
        for (var i = 0; i < currentUserRoles.length; i++) {
            var userRoleId = currentUserRoles[i];

            var odataSelect = "/" + Xrm.Page.context.getServerUrl() + "/XRMServices/2011/OrganizationData.svc" + "/" + "RoleSet?$filter=RoleId eq guid'" + userRoleId + "'";
            var userRoleName = null;
            $.ajax(
                {
                    type: "GET",
                    async: false,
                    contentType: "application/json; charset=utf-8",
                    datatype: "json",
                    url: odataSelect,
                    beforeSend: function (XMLHttpRequest) { XMLHttpRequest.setRequestHeader("Accept", "application/json"); },
                    success: function (data, textStatus, XmlHttpRequest) {
                        userRoleName = data.d.results[0].Name;

                    },
                    error: function (XmlHttpRequest, textStatus, errorThrown) { alert('OData Select Failed: ' + textStatus + errorThrown + odataSelect); }
                }
            );

            if (userRoleName == RoleName) {
                return true;
            }
        }
        return false;
    }

    Hope this Helps !!

    Regards,

    Kamran

  • Mahendar Pal Profile Picture
    45,095 on at

    Can you post error screenshot and how you are using your script web resource

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 CRM (Archived)

#1
SA-08121319-0 Profile Picture

SA-08121319-0 4

#1
Calum MacFarlane Profile Picture

Calum MacFarlane 4

#3
Alex Fun Wei Jie Profile Picture

Alex Fun Wei Jie 2

Last 30 days Overall leaderboard

Featured topics

Product updates

Dynamics 365 release plans