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
Anil IngleAnil Ingle 

How to find "Ultimate Parent" or "Top Level Account" in Account hierarchy?

Our organizations with work multi-level hierarchies of Account.
I was wondering if anyone knows how one can create a script Or SOQL to find out the "Ultimate Parent" and store the result in a field.

Thanks
Best Answer chosen by Anil Ingle
Lokeswara ReddyLokeswara Reddy
Hi Anil,

Try using this utility method which returns ultimate parent account for any given account id
 
public String GetTopLevleElement( String objId ){
        Boolean topLevelParent = false;
        while ( !topLevelParent ) {
            Account a = [ Select Id, ParentId From Account where Id =: objId limit 1 ];
            if ( a.ParentID != null ) {
                objId = a.ParentID;
            }
            else {
                topLevelParent = true;
            }
        }
        return objId ;
    }

Regards
Lokesh

All Answers

ManojjenaManojjena
Hi Anil,
How many level you may have ? Where you want this to implement in trigger or in class ? 

 
Lokeswara ReddyLokeswara Reddy
Hi Anil,

Try using this utility method which returns ultimate parent account for any given account id
 
public String GetTopLevleElement( String objId ){
        Boolean topLevelParent = false;
        while ( !topLevelParent ) {
            Account a = [ Select Id, ParentId From Account where Id =: objId limit 1 ];
            if ( a.ParentID != null ) {
                objId = a.ParentID;
            }
            else {
                topLevelParent = true;
            }
        }
        return objId ;
    }

Regards
Lokesh
This was selected as the best answer
hiteshpareek0091.3932285851008196E12hiteshpareek0091.3932285851008196E12
Use below code to get account to topmost account map , though this is also a kind of recursion but it will reduce the SOQL by 80% :
===========================================================

public static Map<Id,Id> getTopMostParent(List<Id> AccountIds){
        Map<Id,Id> accountToParentAccMap_current = new Map<Id,Id>();        
        
        for(Account acc : [select parent.id, parent.parent.id, parent.parent.parent.id, parent.parent.parent.parent.id,parent.parent.parent.parent.parent.Id from Account where id in :AccountIds]){            
            if(acc.parent.parent.parent.parent.parent.Id != null){        //check for 5th parent
                //add accid to list for recursion                
                accountToParentAccMap_current.put(acc.id,getTopMostParent(new List<Id>{acc.parent.parent.parent.parent.parent.Id}).values()[0]);                
            }else if(acc.parent.parent.parent.parent.id != null){        //check for 4th parent                
                accountToParentAccMap_current.put(acc.id,acc.parent.parent.parent.parent.id);
            }else if(acc.parent.parent.parent.id != null){                //check for 3rd parent                
                accountToParentAccMap_current.put(acc.id,acc.parent.parent.parent.id);
            }else if(acc.parent.parent.id != null){                        //check for 2nd parent                
                accountToParentAccMap_current.put(acc.id,acc.parent.parent.id);
            }else if(acc.parent.id != null){                            //check for 1st parent                
                accountToParentAccMap_current.put(acc.id,acc.parent.id);
            }else{                                                        // no parent account                 
                accountToParentAccMap_current.put(acc.id,acc.id);    // put the same account id
            }
        }        
        return accountToParentAccMap_current;
    }