Skip to main content

Notifications

Announcements

No record found.

Community site session details

Community site session details

Session Id :
Dynamics 365 general forum

check user role not working

(0) ShareShare
ReportReport
Posted on by 5

Hello

I'm trying to check if a user role is not in finance then disable all fields.

function ReadOnly() {
	
	var UserRole = Xrm.Page.context.getUserRoles().toString().replace("{", "").replace("}", "");
	var check = "f0d793f3-9e84-dd11-a5c4-001ec9d279c2"; //finance GUID

        alert(UserRole +" "+ check);
	
	if (UserRole != check) {
       Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {
            var control = Xrm.Page.getControl(attribute.getName());
            if (control) {
                control.setDisabled(true)
            }
        });
	} 
	
}

I tried adding a few steps to make sure that the UserRole was formatted correctly for me to compare the two strings and ive also included a alert to I could make sure that the values were being passed through correctly. I set my user role to finance for testing but despite the variables matching it is still disabling all fields.

Can someone please help ?

Thanks
 

  • Suggested answer
    Kalpavruksh D365 CoE Profile Picture
    Kalpavruksh D365 CoE 2,545 on at
    RE: check user role not working
    Hi,
    Please find the code below.
     
    // JavaScript source code
     
    // getUserRoles returns a array not a single GUID
    //docs.microsoft.com/.../gg334511(v=crm.7)
     
    function ReadOnly() {
     
        var UserRoles = Xrm.Page.context.getUserRoles();
        var check = "f0d793f3-9e84-dd11-a5c4-001ec9d279c2"; //finance GUID
        for (var i = 0; i < UserRoles.length; i++) {
            if (UserRoles[i] === check) {
                Xrm.Page.data.entity.attributes.forEach(function (attribute, index) {
                    var control = Xrm.Page.getControl(attribute.getName());
                    if (control) {
                        control.setDisabled(true);
                    }
                });
                break;
            }
        }
    }
  • Suggested answer
    Johnny Gong Profile Picture
    Johnny Gong 6,482 on at
    RE: check user role not working

    Good day ns95,

    Since my test account is also system administrator , so I modified some 'UserRole != check' as 'UserRole.indexOf(check)<0' to confirm whether the user role contains related role guid.

    function ReadOnly() {
    	
    	var UserRole = Xrm.Page.context.getUserRoles().toString().replace("{", "").replace("}", "");
    	var check = "22045fce-a782-4bd7-b57d-d05103522d27"; //Sales Manger GUID
    
            alert(UserRole +" "+ check);
    	
    	if (UserRole.indexOf(check)<0) 
    	{
           	    Xrm.Page.data.entity.attributes.forEach
    	    (
    		function (attribute, index) 
    	   	{
                	var control = Xrm.Page.getControl(attribute.getName());
                	if (control) 
    			{
                    	control.setDisabled(true);
                		}
           	   	}
    	    );
    	} 
    }

    The above script is nearly the same with yours. Since I don't have 'Finance' role so I changed it to 'Sales Manager'.
    Scenario 1: Test Account without 'Sales Manager Role' , all fields locked as follow:

    nolock.PNG

    Scenario 2: Test Account with 'Sales Manager Role' , all fields are unlocked as follow(need to unlogin and clean the cache first):

    Alet reports user role contain Sales Manager Roleid:

    5444.SM.PNG

    Successfully skip the Foreach disable logic.

    checkpoint.PNG

    nolock.PNG

    So the coding should be normal. I would suggest you to validate your Finance role Guid and set breakpoint debugging to run the process.

    Regards

    Johnny

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

November Spotlight Star - Khushbu Rajvi

Congratulations to a top community star!

Forum Structure Changes Coming on 11/8!

In our never-ending quest to help the Dynamics 365 Community members get answers faster …

Dynamics 365 Community Platform update – Oct 28

Welcome to the next edition of the Community Platform Update. This is a status …

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 292,516 Super User 2025 Season 1

#2
Martin Dráb Profile Picture

Martin Dráb 231,409 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,156

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans