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

Jaspal Singh
Mega Patron
Mega Patron

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.

Its not working.

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.

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

 

I'm afraid that hasRoleExactly() can only be used with g_user. So impersonating a user is what you can try with.

 

Chetan