Skip to main content

Notifications

Announcements

No record found.

Microsoft Dynamics CRM forum
Answered

Javascript sample needed - Hide field when Security Role = X

Posted on by

Hi, 
Looking for a quick sample code to hide a field (msdyn_systemstatus) on the Work Order entity when user Security Role = "*BF Field Service - Resource".  Does anyone have a few lines for this that I can review and test?

Thanks!  

  • Verified answer
    a33ik Profile Picture
    a33ik 84,313 Super User on at
    RE: Javascript sample needed - Hide field when Security Role = X

    I reviewed the code. And it's written the way you wanted it to work.

  • Chrisbra22 Profile Picture
    Chrisbra22 on at
    RE: Javascript sample needed - Hide field when Security Role = X

    OK will look, thanks.  Can you review the code again?  Does it not need a setVisible true, else, false...so that the field is shown for all roles except for *BF Field Service - Resource?  

  • Suggested answer
    a33ik Profile Picture
    a33ik 84,313 Super User on at
    RE: Javascript sample needed - Hide field when Security Role = X

    Usage of the Guid is possible but I won't recommend going this path.

    You can run the following code from the Developer tools to verify that your user really has that role:

    var userRoles = Xrm.Utility.getGlobalContext().userSettings.roles.getAll();

    var allRoles = userRoles.map(role => role.name).join("\r\n");

    alert(allRoles);

    developer.chrome.com/.../

    And one more recommendation - learn a bit of troubleshooting your code. That won't harm - www.youtube.com/watch

  • Chrisbra22 Profile Picture
    Chrisbra22 on at
    RE: Javascript sample needed - Hide field when Security Role = X

    pastedimage1666038378310v1.png

    Maybe need to use GUID instead of name?

  • a33ik Profile Picture
    a33ik 84,313 Super User on at
    RE: Javascript sample needed - Hide field when Security Role = X

    Can you please provide the exact name of the role?

  • Chrisbra22 Profile Picture
    Chrisbra22 on at
    RE: Javascript sample needed - Hide field when Security Role = X

    OK, error has ceased to appear. However, the I have the Security Role and the field still is shown on the form (header).  Any ideas?

  • Verified answer
    a33ik Profile Picture
    a33ik 84,313 Super User on at
    RE: Javascript sample needed - Hide field when Security Role = X

    Ok. Checked in my instance. The right name for that control is header_msdyn_systemstatus. Check that out.

  • Chrisbra22 Profile Picture
    Chrisbra22 on at
    RE: Javascript sample needed - Hide field when Security Role = X

    Thanks again.  Implemented that and am still getting the script error Cannot read properties of null (reading 'setVisible')

  • Verified answer
    a33ik Profile Picture
    a33ik 84,313 Super User on at
    RE: Javascript sample needed - Hide field when Security Role = X

    Gotcha. Header controls are different. Try to use the following code:

    function workorderOnLoad(executionContext) {
    	var formContext = executionContext.getFormContext();
    	var userRoles = Xrm.Utility.getGlobalContext().userSettings.roles.getAll();
    	var roleAvailable = userRoles.some(function(role) {
    		return role.name === "*BF Field Service - Resource";
    	});
    
    	formContext.getControl("header_process_msdyn_systemstatus").setVisible(!roleAvailable);
    }

  • Chrisbra22 Profile Picture
    Chrisbra22 on at
    RE: Javascript sample needed - Hide field when Security Role = X

    It is in the header. It does have Field Level Security against it OOB. 

    pastedimage1666034398368v1.png

Helpful resources

Quick Links

Replay now available! Dynamics 365 Community Call (CRM Edition)

Catch up on the first D365 Community Call held on 7/10

Community Spotlight of the Month

Kudos to Saurav Dhyani!

Congratulations to the June Top 10 community leaders!

These stars go above and beyond . . .

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 288,459 Super User

#2
Martin Dráb Profile Picture

Martin Dráb 225,783 Super User

#3
nmaenpaa Profile Picture

nmaenpaa 101,148

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans