Get the top manager
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-22-2016 08:17 AM
Hello, while working on a workflow, I need to get the approbation from the top manager who is in the XYZ group of the user for whom the item is requested for.
My script can only check if the manager and the manager's manager are in the XYZ group.
How can I make my script more dynamic to get the Nth top manager ?
var answer =[];
var manag = current.variables.requested_for.manager;
var supmanag = current.variables.requested_for.manager.manager;
function getManager(){
var gr = new GlideRecord('sys_user_grmember');
gr.addQuery('user', manag.sys_id);
gr.addQuery('group', 'XYZ_sys_id');
gr.query();
if(gr.next()){
answer.push(manag.toString());
}
}
function getManagerMan(){
var gr1 = new GlideRecord('sys_user_grmember');
gr1.addQuery('user', supmanag.sys_id);
gr1.addQuery('group', 'XYZ_sys_id');
gr1.query();
if(gr1.next()){
answer.push(supmanag.toString());
}
}
if(!manag.isNil())
{
if(!supmanag.isNil()){
getManagerMan();
}
else{
getManager();
}
}
else if(manag.isNil())
{
answer.push('sysadmin_id');
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-22-2016 08:38 AM
Hi Kimberly,
The key to making this work is good data. I tried doing this in an organization of 8,000 and found a lot of inconsistencies (most of them in upstream systems that fed ServiceNow. No fun).
The second factoid is that the top level manager is likely the CEO. Are you sure you want to include this person on notifications, approvals, etc?
Anyway. you'll need to use a little recursion. Keep looking at the current user's manager until there is no more manager value in their field. If you get to that point, then you've got the top level manager. If they have manager, then send that value back in to the function and try again.
How are you with recursion?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2016 10:19 AM
I did something similar. My use case was to determine if a given user was a member of the CIO's org (CIO's sys_id is saved in a sys_properties record). This recursive function calls itself until the condition (in this case, manager is empty) is satisfied.
// UNTESTED CODE...
var usr = new GlideRecord( 'sys_user' );
usr.get( 'name', 'Joe Employee' );
var topManager = getTopManager( usr );
function getTopManager( usr ) {
if ( usr.manager )
return getTopManager( usr.manager );
else
return usr;
}

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-26-2016 10:20 AM
That looks like a winner! Nice work Zachary.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-13-2019 01:37 AM
Hey Zach
This is really nice code!
What I would add would be a function that can determine the "level" of a manager, where for example the board has level -1, C-Level has level 0, etc.
Then, in the function I would ask for the top manager at a certain level, rather than just a "Top" manager.
Cheers
Daniel
If this answer was helpful, I would appreciate if you marked it as such - thanks!
Best
Daniel