function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
SS KarthickSS Karthick 

Getting all Manager Id

Hi Everybody,
       Can anyone tell me how to get all managers id based on the user id??
like user-->manager1-->manager2-->manager3--->manager4 


My code is:
 

public static List<Id> getParrentId(Id i){
        List<Id> ParentUserId=new List<Id>();
        User ur = new User();
        ur = [select Id, Name, ManagerId from User  where Id =: i];
        
        if(ur.ManagerId != null)        
            ParentUserId.addAll(getParrentId(ur.ManagerId));
        return ParentUserId;
     }

But it doesnt get all parent id??
Thanks in advance
Karthick
Best Answer chosen by SS Karthick
Grazitti TeamGrazitti Team
Here is how it should go.
public static List<Id> getParrentId(Id i){
boolean bln = false;
id UserId = i;
List<string> lst = new List<string>();
User u = new User();
while(bln == false)
{
    u = [select id, manager.name, managerid from user where id = :UserId];
    if(u.managerid == null)
    {
        bln = true;
    }
    else
    {
        lst.add(u.manager.name);
        UserId = u.managerid;
    }
   
}
return ParentUserId;
     }

Please Mark this as Answer if it helps you

--
Regards,
Grazitti Team
Web: www.grazitti.com

All Answers

Grazitti TeamGrazitti Team
Here is how it should go.
public static List<Id> getParrentId(Id i){
boolean bln = false;
id UserId = i;
List<string> lst = new List<string>();
User u = new User();
while(bln == false)
{
    u = [select id, manager.name, managerid from user where id = :UserId];
    if(u.managerid == null)
    {
        bln = true;
    }
    else
    {
        lst.add(u.manager.name);
        UserId = u.managerid;
    }
   
}
return ParentUserId;
     }

Please Mark this as Answer if it helps you

--
Regards,
Grazitti Team
Web: www.grazitti.com
This was selected as the best answer
SS KarthickSS Karthick
Hy Grazitti Team
  I wanna gather all manager ids(Manager1.id,manager2.id,manager3.id and etc)
Not name
SS KarthickSS Karthick
Thanks in advance
Grazitti TeamGrazitti Team
This is crazy. You can simply replace manager.name with managerid. Is that too difficult to figure out

Please Mark this as Answer if it helps you

--
Regards,
Grazitti Team
Web: www.grazitti.com