Join the #BuildWithBuildAgent Challenge! Get recognized, earn exclusive swag, and inspire the ServiceNow Community with what you can build using Build Agent.  Join the Challenge.

How to hide a choice list value based on user role

Rhonda9
Tera Expert

Hello,

 

I am trying to hide a choice list value Category(category) on the work order form from a specific role.

I tried creating a onLoad client script but I must be doing something wrong because it is not working.

 
This is my script below which I am not good at.  Any assistance would be greatly appreciated.
 
function onLoad(){
 
if(g_user.hasRole('wm_security'))
 
 return;
        g_form.removeOption('category(category)', 'electrical');
 
}

 

 

Rhonda9_0-1705105945692.png

 

1 ACCEPTED SOLUTION

@Rhonda9 

uncomment the line number 6 and check logged user has role or not .

What it is giving . Is it true or false.

function onLoad() {
   //Type appropriate comment here, and begin script below
   
  var userRole = g_user.hasRoleExactly('wm_security'); 

 

   alert(userRole);

 

  if(userRole == true){
    g_form.removeOption('category','electrical');
  }

 

}

 

View solution in original post

21 REPLIES 21

@Rhonda9 

 

Glad to here that i can help you.

 

please mark my response as accepted solution & helpfull so that other can take help .

And close the thread.

 

thank you

Narsing1
Mega Sage

Do like this

  • Remove the Option "Electrical" from the category choice list from the backend
  • add the below code to Onload

 

function onLoad() {
    //Type appropriate comment here, and begin script below
    if (!g_user.hasRole("wm_security")) {
        g_form.addOption("category", "electrical", "Electrical");
        var control = g_form.getControl("category");
        var options = [];
        for (var m = 0; m < control.options.length; m++) {
            var op = {
                "text": control.options[m].text,
                "value": control.options[m].value
            };
            options.push(op);
        }
        options = options.sort(function(op1, op2) {

            if (op1.text > op2.text) return 1;
            if (op1.text < op2.text) return -1;

        });
        g_form.clearOptions("category");
        for (var b = 0; b < options.length; b++) {
            g_form.addOption("category", options[b].value, options[b].text);
        }
    }
}

 

 

 

Thanks,

Narsing