How can i hide fields based on user and role?

kasirao
Kilo Contributor

In my custom table have two fields

1.Manager Name

2.Mobile phone

When ever i am Impersonate to User Kasi Rao   with Admin role   on custom table above two fields will be displayed.

My requirement is like this

When ever i am Impersonate to User   Karthik Reddy with   Custom role .I want hide two field.

Is it possible to hide fields   based on Impersonate User?

If possible please help me.

11 REPLIES 11

shloke04
Kilo Patron

Hi,



Give your Custom Role to the users you want and then Based on role you can show or hide the fields as mentioned below:



Write an On Load Client script:



function onLoad()


{


var role_check =   g_user.hasRole('Mention the Role Name');



if(role_check == 'admin')


{


g_form.setDisplay('Field Name', false);


}



else if(role_check == 'Mention your Custom Role')


{


g_form.setDisplay('Field Name', true);


}


Hope this helps. Please mark the answer as correct/helpful based on impact.

Regards,
Shloke

kasirao
Kilo Contributor

Hello Srivastava


I have tried your code but still not working.


I have write client script like this


adcccc.PNG



This is my custom table by default User Kasi Rao   have "admin" role only


Up to here everything work's fine


add.PNG



When i am impersonate to User Karthik Reddy   have x_163353_campus_pr.manager(custom role)


Here i want manager field will hide but not working it's still showing manager field.



mng.PNG



Please help me in this requirement i am new to ServiceNow.


the role_check variable will return true or false depending on whether the user has that role eg:



function onLoad()


{


var role_check =   g_user.hasRole('Mention the Role Name');



if(role_check == true){


g_form.setDisplay('Field Name', true);


}


else if(role_check == false){


g_form.setDisplay('Field Name', false);


}


kasirao
Kilo Contributor

Hello David


same script is not working for Reference field


here


Field Name = Manager Name (Reference field)