Home SALESFORCEAPEX How to find manager list using apex class in salesforce

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
}
}

You may also like

Leave a Comment