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,321 Most Valuable Professional 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,321 Most Valuable Professional 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,321 Most Valuable Professional 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,321 Most Valuable Professional 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,321 Most Valuable Professional 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

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

Anton Venter – Community Spotlight

Kudos to our October Community Star of the month!

Announcing Our 2024 Season 2 Super Users!

A new season of Super Users has arrived, and we are so grateful for the daily…

Leaderboard

#1
André Arnaud de Calavon Profile Picture

André Arnaud de Cal... 290,620 Super User 2024 Season 2

#2
Martin Dráb Profile Picture

Martin Dráb 228,884 Most Valuable Professional

#3
nmaenpaa Profile Picture

nmaenpaa 101,150

Leaderboard

Featured topics

Product updates

Dynamics 365 release plans