how to make a field read only based on Logged in user

ursnani
Giga Guru

I have a Field on my form and I want ti make that field READONLY based on Logged in user.

It should be editable to only 4 users.

I have tried scripting in UI Policy and Onload Client Script, But both are not working.

UI Policy Script:

if(g_user.userName != 'username' || g_user.userName != 'username'){

g_form.setReadOnly('fieldname', true);

Onload SCript:

function onLoad() {

    //Type appropriate comment here, and begin script below

    alert('the current Logged in user is '+g_user.userName);

var ga = g_user.userName;

if(ga != 'username' || ga != 'username'){

g_form.setReadOnly('field name', true);

}

}

I had imperonated the user and user and checked but it is readonly for him too..

I dont know where i am going Wrong.   Can anyone please help.

Thanks,

5 REPLIES 5

dave_edgar
Mega Guru

you should be able to use something like this:


an onLoad client script


if (gs.getUser().isMemberOf('<user group>')){


g_form.setReadOnly('<field_name>', true);


}



OR


a Scripted UI Policy


function onCondition() {


var notAllowedToEdit = g_user.hasRole('change_manager');


if(allowedToEdit)


{


g_form.setReadOnly('<field_name>', true);


}}



Hope you can use this


Hi Dave,



We cannot use gs. on client side right ???


yeah, you can use jslog and open up the Javascript log and field watcher


lks
ServiceNow Employee
ServiceNow Employee

Ideally, you should consider using g_user.hasRole() but anyways if you have decided on specific user names still it should work. I used below sample script and it works for me:



function onCondition() {


if(g_user.userName == 'userName');{


g_form.setReadOnly('field_name', true);


}


}