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
seahorcesolnsseahorcesolns 

SOQL Query to Get Child Accounts

I have a class that will look at a global Account and get all of the child Account records. When it's the ultimate parent, this code is working because it only takes one query to get the Account IDs based on their GlobalUltimateDUNSNumber__c being the same as the ultimate parent's DUNSNumber__c. However, if I just want the child Accounts in the middle of the hierarchy to be related to their parent, I'm running into governor limits when the hierarchy is large. 

Is there a better way to move the Select statement out of the For Loop to iterate through the child Accounts?
 
public class OV_BAUtility {
    public static List<String> accountList{get;set;}
    public static Set<String> hAccountList = new Set<String>();
    public static Map<id,Account> accMap{get;set;}
    
    public static String getAcctIds(String id){
        String acctId ='';  
        Cache.SessionPartition sessionPartition = Cache.Session.getPartition('ovs5');
        Account acc = [SELECT id,GlobalUltimateDUNSNumber__c,DUNSNumber__c from Account where id=:id ];
        if(acc.GlobalUltimateDUNSNumber__c == acc.DUNSNumber__c){
           if (sessionPartition.get('acctid'+id)!=null && sessionPartition.get('acctid'+id)!='' && sessionPartition.get('acctid'+id)!='null') {
                acctId = (String)sessionPartition.get('acctid'+id);
           }else{
               List<Account> accLIst =  [SELECT id from Account where GlobalUltimateDUNSNumber__c=:acc.DUNSNumber__c ];
               for(Account account : accLIst){
                    hAccountList.add(account.id);
               }
               acctId =quoteId(new List<String>(hAccountList));
               sessionPartition.put('acctid'+id, acctId);
           }
           system.debug('ULTIMATE ACCOUNT----->'+acctId);
        }else{             
            if (sessionPartition.get('acctid'+id)!=null && sessionPartition.get('acctid'+id)!='' && sessionPartition.get('acctid'+id)!='null') {
                acctId = (String)sessionPartition.get('acctid'+id);
            }else{
                getHierarchyAcctIds(id);
                acctId =quoteId(new List<String>(hAccountList));
                sessionPartition.put('acctid'+id, acctId);
           }
        }
        return acctId;        
    }    
    private static String quoteId(List<String> ids){
        String idStr = '' ;
        for(String str : ids)
            idStr += '\'' + str + '\',';
        idStr = idStr.lastIndexOf(',') > 0 ? '(' + idStr.substring(0,idStr.lastIndexOf(',')) + ')' : idStr ;
        return idStr;
    }
    public static void getHierarchyAcctIds(String Id){       
        hAccountList.add(Id);
        for(Account acc : [SELECT id from Account where ParentId=:Id]){           
            hAccountList.add(acc.id); 
            getHierarchyAcctIds( acc.id);
        }
     }
}