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
system.debug(getManagerId);
public static void getParrentId(Id userId){
User userDetails = new User();
userDetails = [select Id, Name, ManagerId from User where Id =: userId];
if(userDetails.ManagerId != null){
ManagerUserId.add(userDetails.managerId);
getParrentId(userDetails.ManagerId);// Recursive call to find manager of manager
}
}

Written by 

I am Sakthivel Madesh, a certified Salesforce Advanced Developer & Administrator working on Salesforce Technology since 2011. I am currently working in Techforce Services as Sr. Salesforce Developer. I have worked on multiple technologies Like PHP, Java, Salesforce, I am very passionate about Salesforce and to earn 15x Salesforce Certification. I love Trailhead for learning Salesforce Skill and Become a Trailhead 5x Ranger too. I Love to Learn & Share my Salesforce knowledge to Salesforce Learners/Beginner using My Blog and Salesforce Community.

Leave a Reply

Your email address will not be published. Required fields are marked *

*