Set Field Visiblity based on Security Roles in Dynamics 365


In this blog we will demonstrate how to set visibility of a field in Dynamics CRM based on Security Roles using JavaScript.


Step 1: First we create a security role, Here for our demonstration I have created a Blank Security Role with the name “CustomEditAccess”.

Copy the GUID of the security Role as this will be used later in the Code.

Step 2: If the user does not have the custom security role that we have created we will hide the Parent Account field else we will show the Parent Account field.

For this first we go to the Form Editor and set the Visible by Default to “No” for the field “Parent Account”

Account Entity Properties

Step 3: Create a new JavaScript Web resource  and add the following code:

var accountFormCustomization = {

customEditAccess: function (execContext) {
var formContext = execContext.getFormContext();
var userSettings = Xrm.Utility.getGlobalContext().userSettings;

//Get Security Roles of the current User
var securityRoles = userSettings.securityRoles;

//Below is the GUID of the Security Role "CustomEditAccess"
var securityRoleId = "B9E23C5A-F5F9-E811-A96C-000D3AF29D99";

for (i = 0; i < securityRoles.length; i++) {
//If current User contains the Required Security Role
if (securityRoles[i].toUpperCase() == securityRoleId.toUpperCase()) {
//Field should be Set to Visible

Note: Here we have set the visibility of the Parent Account(parentaccountid) field to “True” to show the field on the form.

Step 4: Once this is done we register an Event On Form Load, as shown below:

OnLoad Properties

Remember to pass the Execution context as the first parameter

Step 5: Once this is done when the form is loaded, the script will check if the User has the required Security Role, if yes then the “Parent Account” field will be shown else it will be hidden by default.

Tip: Here we have hardcoded the GUID of the security role, what we could also do if fetch the GUID of the security role based on the Name of the Security role and then follow with the Hide/Show visibility logic.


This is another alternative to field level security and this can be implemented if you find Field level security too complex as your requirements in the organization grow.

Share Story :