diff --git a/Server-Side Components/Background Scripts/Get Manager Hierarchy/Read.md b/Server-Side Components/Background Scripts/Get Manager Hierarchy/Read.md new file mode 100644 index 0000000000..f115092b45 --- /dev/null +++ b/Server-Side Components/Background Scripts/Get Manager Hierarchy/Read.md @@ -0,0 +1,10 @@ +**Get the complete hierarchy of the managers for a given user.** +This script uses a recursive function to fetch the complete list of managers for any given user. Consider the following example. +Screenshot 2025-10-20 at 1 29 11 AM + +In this example user Abel Tutor reports to Abraham Lincoln, Abrham reports to Adela Cervantsz, Adela reports to Aileen Mottern and so on. + +In order to fetch this complete hierachy the getManagerHierachy script can be used. Here is the example of this script with output. +Screenshot 2025-10-20 at 1 34 36 AM + + diff --git a/Server-Side Components/Background Scripts/Get Manager Hierarchy/getManagerHierarchy.js b/Server-Side Components/Background Scripts/Get Manager Hierarchy/getManagerHierarchy.js new file mode 100644 index 0000000000..d44be11f88 --- /dev/null +++ b/Server-Side Components/Background Scripts/Get Manager Hierarchy/getManagerHierarchy.js @@ -0,0 +1,16 @@ +//Recursive function to get the complete manager hierarchy +//Param 1: managerArray an Array to hold manager hierarchy +//Param 2: next is representing next person in the manager hierarchy +function getManagerHierarchy(managerArray, next) { + var glideRecord = new GlideRecord('sys_user'); + if (glideRecord.get(next)) {//Check if there is a user record + managerArray.push(glideRecord.manager.name + '');//Add the user detail to the manager array + return getManagerHierarchy(managerArray, glideRecord.getValue('manager')); //Recursively call the same method to get the details of manager + + } else {//No more manager found return the array to callling function + return managerArray.toString().split(',').join(" >> ").substring(0, managerArray.toString().split(',').join(" >> ").length-4); + } +} + +var managerArray = []; +gs.info(getManagerHierarchy(managerArray, '22826bf03710200044e0bfc8bcbe5dec')); //Pass the sys_id of person here