logo

NJP

Script to get the manager hierarchy

New article articles in ServiceNow Community · Aug 30, 2025 · article

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

View original source

https://www.servicenow.com/community/servicenow-studio-articles/script-to-get-the-manager-hierarchy/ta-p/3366400