How to find manager list using apex class in salesforce

Its possible to find out from managerId field in User Object and below is the code to find manager hierarchy for particular user.
set<Id> ManagerUserId=new set<Id>();
ManagerUserId.add(‘User id’);// Add user Id to whom you want to find manager list
getManagerId(‘LOGGED in User id’);// If you want to add base user also add this line else no need
public static void getParrentId(Id userId){
User userDetails = new User();
userDetails = [select Id, Name, ManagerId from User where Id =: userId];
if(userDetails.ManagerId != null){
getParrentId(userDetails.ManagerId);// Recursive call to find manager of manager

Written by 

3x Salesforce MVP | Platform Champion | 18x Salesforce Certified | MuleSoft Certified | 8x Trailhead Ranger | TechForce Services | Sydney | Australia

Leave a Reply

Your email address will not be published.