web
You’re offline. This is a read only version of the page.
close
Skip to main content

Notifications

Announcements

No record found.

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!  

I have the same question (0)
  • Suggested answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Hello,

    Check this 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("msdyn_systemstatus").setVisible(!roleAvailable);
    }

  • Chrisbra22 Profile Picture
    on at

    Thanks!  Looks like when I implement that onLoad there is an error on setVisible...

    TypeError: Cannot read properties of null (reading 'setVisible')

       at HideWOSystemStatus (bridgestonevandev.crm.dynamics.com/.../bf_HideWOSystemStatus:9:46)

       at y._executeFunctionInternal (bridgestonevandev.crm.dynamics.com/.../app.js

       at y.execute (bridgestonevandev.crm.dynamics.com/.../app.js

       at bridgestonevandev.crm.dynamics.com/.../app.js

       at i (bridgestonevandev.crm.dynamics.com/.../app.js

       at ee._executeIndividualEvent (bridgestonevandev.crm.dynamics.com/.../app.js

       at ee._executeEventHandler (bridgestonevandev.crm.dynamics.com/.../app.js

       at Object.execute (bridgestonevandev.crm.dynamics.com/.../app.js

       at N._executeSyncAction (bridgestonevandev.crm.dynamics.com/.../app.js

       at N._executeSync (bridgestonevandev.crm.dynamics.com/.../app.js

    Error Details:

    Event Name: onload

    Function Name: HideWOSystemStatus

    Web Resource Name: msdyn_/WorkOrder/WorkOrder.Library.js

    Solution Name: msdyn_FieldService_patch_update

    Publisher Name: microsoftdynamics

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Are you sure that the field is present on the form? Can you please provide the screenshot of the form?

  • Chrisbra22 Profile Picture
    on at

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

    pastedimage1666034398368v1.png

  • Verified answer
    a33ik Profile Picture
    84,331 Most Valuable Professional on at

    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

    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

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

  • Chrisbra22 Profile Picture
    on at

    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?

  • a33ik Profile Picture
    84,331 Most Valuable Professional on at

    Can you please provide the exact name of the role?

  • Chrisbra22 Profile Picture
    on at

    pastedimage1666038378310v1.png

    Maybe need to use GUID instead of name?

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

Responsible AI policies

As AI tools become more common, we’re introducing a Responsible AI Use…

Neeraj Kumar – Community Spotlight

We are honored to recognize Neeraj Kumar as our Community Spotlight honoree for…

Leaderboard > Customer experience | Sales, Customer Insights, CRM

#1
Tom_Gioielli Profile Picture

Tom_Gioielli 83 Super User 2025 Season 2

#2
Gerardo Rentería García Profile Picture

Gerardo Rentería Ga... 49 Most Valuable Professional

#3
#ManoVerse Profile Picture

#ManoVerse 40

Last 30 days Overall leaderboard

Product updates

Dynamics 365 release plans