How to hide a field with user roles

tsam
Kilo Explorer

Hi -

How can I hide a field on a form based on the user roles? I have created a role and assigned it to the field on the form but it's not working as expected.

Thanks.

6 REPLIES 6

Jace Benson
Mega Sage

ACL's are best at allowing people to see the data, edit or otherwise manipulate the data. If you just want it not to show up for people with a given role, you can use a client script to show/hide the field. Below is an example on how to do that;



function onLoad() {

var isAdmin = g_user.hasRole('admin');
//console.log("isAdmin: " + isadmin);
if (isAdmin == true) {
g_form.setDisplay('staff_type', 'true');
} else {
g_form.setDisplay('staff_type', 'false');
}
}


what if I don't want the logged in user but a person who is selected on the form in the reference field. 

CapaJC
ServiceNow Employee
ServiceNow Employee

I'm not sure I fully understand your goal, but you could also try a Read ACL on that field with the following script, and make sure the "Admin overrides" field is checked on the ACL:



!gs.hasRole("u_role_you_want_to_deny_access_with");

The users with that role who don't have the "admin" role WON'T be able to see the field.


trdaniels
Tera Expert

Hello,

How would I modify the below scipt to allow 3 or more roles to view the configuration item on incident tickets?

example roles:
admin
field_services
engineering

function onLoad() {

var isAdmin = g_user.hasRole('admin');
//console.log("isAdmin: " + isadmin);
if (isAdmin == true) {
g_form.setDisplay('cmdb_ci', 'true');
} else {
g_form.setDisplay('cmdb_ci', 'false');
}
}