I had posted a question some time back for whats the best way to do account hierarchy and this is the way implemented at last. Old question is Recursive functions APEX . Can someone point out any obvious flaw in this(other than SOQL)

Use case is find the first account which has IS VGE flag checked or if no IS VGE ones in hierarchy get the topmost one. Once you get the record, Update some fields with this records' value If no parent present-don't bother No business limit in hierarchy levels

Below method is called by a before insert and before update triggers

    public with sharing class Find {
//Static variable to make sure method is called only once
public static boolean firstRun_GlobalUltimate = true;   
public static void  findGlobal(list<account>Triggerlist)
    map<Integer,id> Treemap = new map<Integer,ID>();
    account a = new account();
    set<ID> parentIdSet = new set<ID>();
    Map<ID,Account>parentRec= new map<ID,Account>();
    Id accid;
    account gm;
    map<ID,Account>queryMap =  new map<ID,Account>();
//Forming a map of current Account indexes and their parents,if there is any    
    for (integer n=0; n< TriggerList.size(); n++) 
        //Checking if Parent is missing for the record
        If (TriggerList[n].ParentId != null) 
                //Add key for map as index of item in list,Value as Parent ID present for Account
                treeMap.put(n, TriggerList[n].ParentId);
                //Add parent id  to parentIdset to be used in account heirarchy loop

    }//End of main for loop                 

//Account hierarchy traversy Loop:Applicable only to accounts with a parent
//Once loop is executed Treemap will be updated with either
//first VGE account it met in hierarchy or the top most account in heirarchy
            //Clear the querymap                                     
            //Select parent record with Is vge account and its parentid
            queryMap = new map<ID,Account>([select Is_VGE_Account__c ,Segment__c,Public_sector__c,parentid from account where id in :parentidset]);
            //Clear parentidset
            //check whether map is empty
                    //Traverse through the treemap
                    for(integer i:treemap.keyset())
                            accid = treemap.get(i);
                            if(querymap.containskey(accid) )
                                {   //Get the parent record
                                    gm = querymap.get(accid);
                                    //Form a  map of all parent record
                                    //If Parent is  not a VGE Account add its parent id to parentid set for next fetch and update treemap
                                    if((!gm.is_vge_account__c) && (gm.parentID!=null))
                                }//end of checking whether querymap returned record
                        }//end of traversing through keymap
                }//End of querymap empty checking
        }while((!querymap.isempty()) && Limits.getQueries()<50);//Making sure SOQL Limit of 100 is not hit

//Updating incoming Accounts
    for (integer n=0; n< TriggerList.size(); n++) 
            //Check if the account has a parent and has an entry in treemap,if so update the Global
            //Ultimate value to the value in Treemap.For accounts without parent ,Global Ultimate remains null
                    Triggerlist[n].Global_Ultimate__c = Treemap.get(n);                                                             
            //Check if  Customer Account and whether without a Global Ultimate
            if(GEN_Utilities.recordType(Triggerlist[n].recordTypeId) == 'Customer Account' && (Triggerlist[n].Global_Ultimate__c!=null) )   
                    //Get the record from parentRecmap
                    gm = parentRec.get(Triggerlist[n].Global_Ultimate__c);  
                    //Update the account with Global ultimate's segment and public sector               
                    Triggerlist[n].Segment__c = gm.Segment__c;
                    Triggerlist[n].Public_sector__c = gm.Public_sector__c;
        }//End of updating incoming accounts loop      
}//End of method
}//End of Class
  • This type of question might be a better fit on codereview.stackexchange.com. Metadaddy and I both watch it for Apex code. – Mike Chale Jul 31 '13 at 20:12
  • Oh thanks for letting me know.I have posted this in codereview – sfdc99999 Jul 31 '13 at 21:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.