Skip to main content

Notifications

Community site session details

Community site session details

Session Id :
Customer experience | Sales, Customer Insights,...
Answered

Javascript sample needed - Hide field when Security Role = X

(0) ShareShare
ReportReport
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
    84,331 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
    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
    84,331 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
    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
    84,331 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
    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
    84,331 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
    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
    84,331 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
    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

Jainam Kothari – Community Spotlight

We are honored to recognize Jainam Kothari as our June 2025 Community…

Congratulations to the May Top 10 Community Leaders!

These are the community rock stars!

Announcing the Engage with the Community forum!

This forum is your space to connect, share, and grow!

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Daivat Vartak (v-9davar) Profile Picture

Daivat Vartak (v-9d... 671 Super User 2025 Season 1

#2
Vahid Ghafarpour Profile Picture

Vahid Ghafarpour 167 Super User 2025 Season 1

#3
Muhammad Shahzad Shafique Profile Picture

Muhammad Shahzad Sh... 138 Most Valuable Professional

Product updates

Dynamics 365 release plans