- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-16-2022 07:59 AM
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
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-17-2022 12:08 AM
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);
}
}
Regards,
Musab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-17-2022 12:13 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-17-2022 12:08 AM
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);
}
}
Regards,
Musab
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-17-2022 12:13 AM
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
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎05-17-2022 08:06 AM
Hi Ankur,
It is not working.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-18-2022 12:21 PM - edited ‎10-18-2022 12:59 PM
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
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.