Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM (Archived)

Hide Options from Option Set based on Security role

Posted on by Microsoft Employee

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

  • Mahendar Pal Profile Picture
    Mahendar Pal 45,095 on at
    RE: Hide Options from Option Set based on Security role

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

  • Suggested answer
    Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Hide Options from Option Set based on Security role

    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

  • Community Member Profile Picture
    Community Member Microsoft Employee on at
    RE: Hide Options from Option Set based on Security role

    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
    anilambadan Profile Picture
    anilambadan 1,160 on at
    RE: Hide Options from Option Set based on Security role

    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
    Community Member Microsoft Employee on at
    RE: Hide Options from Option Set based on Security role

    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 ?

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

December Spotlight Star - Muhammad Affan

Congratulations to a top community star!

Top 10 leaders for November!

Congratulations to our November super stars!

Tips for Writing Effective Suggested Answers

Best practices for providing successful forum answers ✍️

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 291,253 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 230,188 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans