Advanced script for User Criteria.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2018 05:48 AM
Dear All,
I have a requirement in which I need ti grant access to item if user has a role or has a checked box in user profile
as true.
I checked the code using logs, but inside_2 is returning.
Below is my code:
checkCondition();
function checkCondition()
{
gs.log("start");
var obj = new GlideRecord('sys_user');
obj.addQuery('sys_id',gs.getUserID());
obj.query();
gs.log("Record : "+obj.getRowCount());
while(obj.next())
{
gs.log("Inside_1");
if (gs.getUser().getRecord().getValue('u_hr_function') == true || gs.hasRole('hr_coordinator')) {
gs.log("Inside_2");
return true;
} else {
return false;
}
}
}
Please let me what needs to be changed.
Thanks in advance!
Regards,
Divya Sharma
- Labels:
-
Service Catalog

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2018 06:02 AM
Hi Divya,
Can you check with below once.
checkCondition();
function checkCondition()
{
gs.log("start");
var obj = new GlideRecord('sys_user');
obj.addQuery('sys_id',gs.getUserID());
obj.query();
gs.log("Record : "+obj.getRowCount());
while(obj.next())
{
gs.log("Inside_1");
if (obj.u_hr_function == 'true' || gs.hasRole('hr_coordinator')) { //considering u_hr_function is a field on User table
gs.log("Inside_2");
return true;
} else {
return false;
}
}
}
Thanks,
Jaspal Singh
Hit Helpful or Correct on the impact of response.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2018 06:56 AM
Its not working.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2018 07:01 AM
Check with & also confirm the log values you get once.
checkCondition();
function checkCondition()
{
gs.log("start");
var curuser=gs.getUserID();
gs.log('Current usr is ',curuser);
var obj = new GlideRecord('sys_user');
obj.addQuery('sys_id',curuser);
obj.query();
gs.log("Record : "+obj.getRowCount());
while(obj.next())
{
gs.log("Inside_1");
if (obj.u_hr_function == 'true' || gs.hasRole('hr_coordinator')) { //considering u_hr_function is a field on User table
gs.log("Inside_2");
return true;
}
else {
return false;
}
}
}
Thanks,
Jaspal Singh
Hit Helpful or Correct on the impact of response.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2018 07:26 AM
Hi Divya,
Script shared by Jaspal looks good, so just to confirm - Are you testing this script with admin account ? If yes, gs.hasRole('hr_coordinator') will always return true.
Try using gs.hasRoleExactly() instead.
Thanks!
Chetan

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
07-11-2018 07:32 AM
I'm afraid that hasRoleExactly() can only be used with g_user. So impersonating a user is what you can try with.
Chetan