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.

Advanced script for User Criteria.

Divya Sharma1
Kilo Explorer

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

12 REPLIES 12

Jeff Pascoe
Tera Expert

If "u_hr_function" is the checkbox on the user profile you are trying to check, then you should be able to use 

if (obj.u_hr_function == "true" || gs.hasRole('hr_coordinator')) {

as your if condition.

Slight correction; you would need to remove the "" around true, also it's best to get the value of the field, results can be a bit random if you reference the glide object directly.

if (obj.getValue('u_hr_function') == true || gs.hasRole('hr_coordinator')) {
//code
}

Its not working.

Its not working.