+ Start a Discussion
Allan Lorentzen 6Allan Lorentzen 6 

How to Calculate the number of managers above user?

Do any body how if it’s possible to calculate the number of managers above the user.
 
Basically, I would like a field on the user object that indicate the manager level.
 
Example:
If the user’s “manager field” = null, then Level 1
If the user’s manager’s “manager field” = null, then Level 2
If the user’s manager’s manager’s “manager field” = null, then Level 3
Etc.
Else “Level 5”.
 
A daily trigger should set this value for each user records.
 
Does any body have an idea if it’s possible? Big or medium task?
Best Answer chosen by Allan Lorentzen 6
AmulAmul
Hi Alan,

Here is the Solution.

Please create a trigger on User Object. And also please let me know if you need any further help on this. Please update all the user records at least once with help of data loader.

plese email me amulhai@gmail.com if you have firther question

==================================================================================== 

trigger userTrigger on User(before update){

    List<User> UsrList=[Select id, ManagerId from User where IsActive=true];
    Map<id,id> MapUserWiseManager=new Map<Id,Id>();

    for(User usr: UsrList){
        if(usr.ManagerId!=null){
            MapUserWiseManager.put(usr.id,usr.ManagerId);
        }
    }
    
    
    for(User usr:Trigger.New){
        if(usr.ManagerId==null){
            usr.manager_level__c='Level 1';
        }else{
            if(MapUserWiseManager.get(usr.ManagerId)==null){
                usr.manager_level__c='Level 2';
             }else{
                    //Manager's Manager
                    Id Mgr_of_Mgr_2= MapUserWiseManager.get(usr.ManagerId);                    
                    if(MapUserWiseManager.get(Mgr_of_Mgr_2)==null){
                        usr.manager_level__c='Level 3';
                    }else{
                        //Manager's Manager's Manager
                        Id Mgr_of_Mgr_3= MapUserWiseManager.get(Mgr_of_Mgr_2);    
                        if(MapUserWiseManager.get(Mgr_of_Mgr_3)==null){
                            usr.manager_level__c='Level 4';
                        }else{
                            //Manager's Manager's Manager's Manager
                            Id Mgr_of_Mgr_4= MapUserWiseManager.get(Mgr_of_Mgr_3);
                            if(MapUserWiseManager.get(Mgr_of_Mgr_4)==null){
                                usr.manager_level__c='Level 5';
                            }
                        }    
                    
                    }
            
        }
        
    }
    }//end of for


}


 

All Answers

AmulAmul
Hi Alan,

Out of box is not possible. but this can be done by using custom apex trigger or batch apex very easily. and it requires 4 hour jobs to accomplish. please email me at amulhai@gmail.com. If you wanted to implement this.
Allan Lorentzen 6Allan Lorentzen 6
Thanks Amul - I know that apex was needed for this job. I just want to know if it was possible. 
AmulAmul
Hi Alan,

Here is the Solution.

Please create a trigger on User Object. And also please let me know if you need any further help on this. Please update all the user records at least once with help of data loader.

plese email me amulhai@gmail.com if you have firther question

==================================================================================== 

trigger userTrigger on User(before update){

    List<User> UsrList=[Select id, ManagerId from User where IsActive=true];
    Map<id,id> MapUserWiseManager=new Map<Id,Id>();

    for(User usr: UsrList){
        if(usr.ManagerId!=null){
            MapUserWiseManager.put(usr.id,usr.ManagerId);
        }
    }
    
    
    for(User usr:Trigger.New){
        if(usr.ManagerId==null){
            usr.manager_level__c='Level 1';
        }else{
            if(MapUserWiseManager.get(usr.ManagerId)==null){
                usr.manager_level__c='Level 2';
             }else{
                    //Manager's Manager
                    Id Mgr_of_Mgr_2= MapUserWiseManager.get(usr.ManagerId);                    
                    if(MapUserWiseManager.get(Mgr_of_Mgr_2)==null){
                        usr.manager_level__c='Level 3';
                    }else{
                        //Manager's Manager's Manager
                        Id Mgr_of_Mgr_3= MapUserWiseManager.get(Mgr_of_Mgr_2);    
                        if(MapUserWiseManager.get(Mgr_of_Mgr_3)==null){
                            usr.manager_level__c='Level 4';
                        }else{
                            //Manager's Manager's Manager's Manager
                            Id Mgr_of_Mgr_4= MapUserWiseManager.get(Mgr_of_Mgr_3);
                            if(MapUserWiseManager.get(Mgr_of_Mgr_4)==null){
                                usr.manager_level__c='Level 5';
                            }
                        }    
                    
                    }
            
        }
        
    }
    }//end of for


}


 
This was selected as the best answer
AmulAmul
great Allan,

For any future requirement please reach out to me amulhai@gmail.com

Here is my linked In Profile:
https://in.linkedin.com/in/amulbaranwal

And My Blog:
http://amulhai.blogspot.in/