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 

Collecting id in Recursion

Hi folks,
   Can anyone tell me how to collect all Ids using recursion?
Here's my code:

public class ParrentIdClass2 {
   
   public static List<Id> getParrentId(Id i){
        String ParentUserName;
        List<Id> ParentUserId=new List<Id>();
        User ur = new User(id=i);
        ur = [select ur.Id, ur.Name, ur.ManagerId from User ur where ur.Id =: i];
      
        System.debug('UrId='+ur.Id);
        ParentUserId.add(ur.Id);
        System.debug('UrName='+ur.Name);
        System.debug('UrManagerId='+ur.ManagerId);
       
        if(ur.ManagerId != null){ 
          
   ParentUserId.addAll(getParrentId(ur.ManagerId));
           
   }
 
 
        return ParentUserId;
 
}
}

They above code doesnt collect all iids
Please Help!
Best Answer chosen by SS Karthick
Vikash TiwaryVikash Tiwary
public class ParrentIdClass2 {
  List<Id> ParentUserId=new List<Id>();

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

Hope this helps!