diff --git a/Server-Side Components/Background Scripts/Find Top-Level Manager Hierarchy/Readme.md b/Server-Side Components/Background Scripts/Find Top-Level Manager Hierarchy/Readme.md new file mode 100644 index 0000000000..574cf34069 --- /dev/null +++ b/Server-Side Components/Background Scripts/Find Top-Level Manager Hierarchy/Readme.md @@ -0,0 +1,13 @@ +The script retrieves the top-level manager for the currently logged-in user by traversing the manager hierarchy in the sys_user table. + +It starts from the current user and moves up through each manager until it reaches a user who does not have a manager. + +The script starts with the current user (e.g., Employee). + +It checks if the user has a manager. + +If yes, it moves up the hierarchy to the manager. + +It repeats this process until it reaches a user who does not have a manager. + +That user is considered the Top-Level Manager. diff --git a/Server-Side Components/Background Scripts/Find Top-Level Manager Hierarchy/script.js b/Server-Side Components/Background Scripts/Find Top-Level Manager Hierarchy/script.js new file mode 100644 index 0000000000..a725a1a65d --- /dev/null +++ b/Server-Side Components/Background Scripts/Find Top-Level Manager Hierarchy/script.js @@ -0,0 +1,25 @@ +var currentUser = gs.getUser(); // Current logged-in user +var userGR = new GlideRecord('sys_user'); +var maxLevels = 7; +var currentLevel = 0; + +if (userGR.get(currentUser.getID())) { + + // Loop until we find a user who has no manager or reach max level + while (userGR.manager && currentLevel < maxLevels) { + var managerID = userGR.getValue('manager'); + var managerGR = new GlideRecord('sys_user'); + + if (managerGR.get(managerID)) { + userGR = managerGR; // Move up one level + currentLevel++; + // gs.print(" Level " + currentLevel + " Manager: " + userGR.getDisplayValue('name')); + } else { + break; // Manager record not found + } + } + + gs.print("Top-level (or Level " + currentLevel + ") Manager: " + userGR.getDisplayValue('name')); +} else { + gs.print("User not found."); +}