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.

Sandeep Rajput
Tera Patron
Tera Patron

Came across an interesting question here where the questioner requested for a solution which could give them the entire hierarchy of a user's manager (manager's manager and so on) till the topmost person without a manager in the most performant way. 

 

Here is the solution proposed by me.

 

The following code can be run inside a background script and the function can be tested by simply providing the sys_id of the user for whom the entire hierarchy needs to be fetched.

function getManagerHierarchy(managerArray, next) {
    var glideRecord = new GlideRecord('sys_user');
    if (glideRecord.get(next)) {
        managerArray.push(glideRecord.manager.name + '');
        return getManagerHierarchy(managerArray, glideRecord.getValue('manager'));
        
    } else {        
		return managerArray.toString();
    }
}

var managerArray = [];
gs.info(getManagerHierarchy(managerArray, 'b13cb325931721100143ad44246a4db0')); //Pass the sys_id of person here

Hope this helps 

 

Version history
Last update:
‎08-30-2025 09:13 AM
Updated by:
Contributors