User Criteria: Identify line managers

Brad Campbell
Tera Contributor

Morning community,

 

Probably a common ask but I can't find a previous post that fits the bill. We have a catalogue item that we want to restrict to just managers i.e. those who appear in the manager field of any user in the sys_user table.

 

Any help appreciated as always.

 

Thanks, Brad

1 ACCEPTED SOLUTION

Ankur Bawiskar
Tera Patron
Tera Patron

@Brad Campbell 

use script inside user criteria

this will work fine

var userRec = new GlideRecord('sys_user');
userRec.addQuery('manager', user_id);
userRec.setLimit(1);
userRec.query();
answer = userRec.hasNext(); 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

View solution in original post

9 REPLIES 9

Ravi Chandra_K
Kilo Patron
Kilo Patron

Hello @Brad Campbell 

you can try the following script to check if the user is manager for anyone.

var user_id = gs.getUserID();

var usgr = new GlideRecord('sys_user');

usgr.addActiveQuery();

usgr.addQuery('manager', user_id); //checking if the given user (user_id) is manager for anyone.

usgr.setLimit(1);

usgr.query();

return usgr.hasNext(); //returns true if user is manager

 

Help others to find a correct solution by marking the appropriate response as correct answer and helpful!!

 

Regards,

Ravi Chandra

Hi Ravi

 

I get an error when trying to save your code in the user criteria script field...

 

Could not save record because of a compile error: JavaScript parse error at line (7) column (7) problem = invalid return (<refname>; line 7)

 

Thanks

 

Brad

Hello @Brad Campbell 

can you  replace 7th line with 

if(usgr.hasNext())

return true;

 

Regards,

Ravi Chandra.

Ankur Bawiskar
Tera Patron
Tera Patron

@Brad Campbell 

use script inside user criteria

this will work fine

var userRec = new GlideRecord('sys_user');
userRec.addQuery('manager', user_id);
userRec.setLimit(1);
userRec.query();
answer = userRec.hasNext(); 

If my response helped please mark it correct and close the thread so that it benefits future readers.

Regards,
Ankur
Certified Technical Architect  ||  9x ServiceNow MVP  ||  ServiceNow Community Leader

Thank you Ankur. This works perfectly!