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.

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

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

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

Hi Ankur,

It is not working.

baileycodes
Tera Contributor

EDIT/UPDATE:
One of my teammates put me onto the right path so maybe this info will help you too:
g_user is instantiated upon login (meaning it's session-based, which I did not know). So if you make any changes to the user record (including adding/removing roles), the g_user script isn't going to pull the right data. Once I logged out and logged back in, the script worked as-expected!


ORIGINAL RESPONSE:

I'm also running into this issue in a Client Script for a Catalog Item.


UI Type is set to All
Applies on a Catalog Item view is True
Applies on Requested Items is False
Applies on Catalog Tasks is False

And I cannot get around it by using "hasRole" because the fulfillers using this Catalog Item will not have the 'admin' role.

I used console.logs to check what's being returned, and it returns false.

screenshot of the console.log from the browser

Screen Shot 2022-10-18 at 11.58.45 AM.png

 

client script - including the console.logs

 

 

function onLoad() {
	console.log("onLoad Script for checking the role");
	console.log(g_user.hasRoleExactly('u_custom_role'));
   if(g_user.hasRoleExactly('u_custom_role')){
		g_form.setReadOnly('Requested_For_details',false);
	}
}

 

Note: I changed the name of our custom role, but I directly copy/pasted the role directly from the XML of the sys_user_role record into my script so I know the problem is NOT due to a transcription error.