File tree Expand file tree Collapse file tree 2 files changed +38
-0
lines changed
Server-Side Components/Background Scripts/Find Top-Level Manager Hierarchy Expand file tree Collapse file tree 2 files changed +38
-0
lines changed Original file line number Diff line number Diff line change 1+ The script retrieves the top-level manager for the currently logged-in user by traversing the manager hierarchy in the sys_user table.
2+
3+ It starts from the current user and moves up through each manager until it reaches a user who does not have a manager.
4+
5+ The script starts with the current user (e.g., Employee).
6+
7+ It checks if the user has a manager.
8+
9+ If yes, it moves up the hierarchy to the manager.
10+
11+ It repeats this process until it reaches a user who does not have a manager.
12+
13+ That user is considered the Top-Level Manager.
Original file line number Diff line number Diff line change 1+ var currentUser = gs . getUser ( ) ; // Current logged-in user
2+ var userGR = new GlideRecord ( 'sys_user' ) ;
3+ var maxLevels = 7 ;
4+ var currentLevel = 0 ;
5+
6+ if ( userGR . get ( currentUser . getID ( ) ) ) {
7+
8+ // Loop until we find a user who has no manager or reach max level
9+ while ( userGR . manager && currentLevel < maxLevels ) {
10+ var managerID = userGR . getValue ( 'manager' ) ;
11+ var managerGR = new GlideRecord ( 'sys_user' ) ;
12+
13+ if ( managerGR . get ( managerID ) ) {
14+ userGR = managerGR ; // Move up one level
15+ currentLevel ++ ;
16+ // gs.print(" Level " + currentLevel + " Manager: " + userGR.getDisplayValue('name'));
17+ } else {
18+ break ; // Manager record not found
19+ }
20+ }
21+
22+ gs . print ( "Top-level (or Level " + currentLevel + ") Manager: " + userGR . getDisplayValue ( 'name' ) ) ;
23+ } else {
24+ gs . print ( "User not found." ) ;
25+ }
You can’t perform that action at this time.
0 commit comments