The CreatorCon Call for Content is officially open! Get started here.

Set Variable to read only based on role

hongsok
Tera Contributor

Hello all,

I have a catalog item and I would to set some of the variables to read only based on the login user role.

I created the following OnLoad client script to set the variable to read only if the login user does not has "hari_admin" role.

The script is not working. Could someone help me to take a look? 

__________________________

function onLoad() {

var hariUser = g_user.hasRoleExactly('hari_admin)');
if (hariUser) {
g_form.setReadonly('list_of_assets', false);

}}

__________________________

Regards,

Hong

2 ACCEPTED SOLUTIONS

Looks like either there is another script/ policy which is there on Asset field or backend name of asset field is incorrect, this is very simple requirement please check again. Try below once.

function onLoad() {
   //Type appropriate comment here, and begin script below
   var hariUser = g_user.hasRoleExactly('hari_admin');
	
if (!hariUser) {
g_form.setReadOnly('list_of_assets', true);
}
}
Please hit like and mark my response as correct if that helps
Regards,
Musab

View solution in original post

Ankur Bawiskar
Tera Patron
Tera Patron

@hongsok 

it should be like this

function onLoad() {

	var hariUser = g_user.hasRoleExactly('hari_admin)');
	if (hariUser.toString() == 'true') {
		g_form.setReadonly('list_of_assets', false);
	}
}

Regards
Ankur

Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

View solution in original post

8 REPLIES 8

Brian Lancaster
Tera Sage

On your list_of_assets variable, is mandatory checked? Are there any UI Policies that make the variable mandatory? The reason I ask is if a variable is mandatory it cannot be made read only.

Hi Brian,

No.

Musab Rasheed
Tera Sage

Hi,

'O' is small in Readonly function, it should be upper case like below

g_form.setReadOnly();

Mark my answer as correct if that helps.

Regards

Please hit like and mark my response as correct if that helps
Regards,
Musab

Hi Musab,

Even I changed to g_form.setReadOnly(), it is still not working.