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
Dave ShulmanDave Shulman 

Aggregating Roll Ups through Parent Account Hierarchy Class

Hi All,

Im trying to carry sales through account heirarchy adding throughout each level.  Sales Data is the individual sales line items object, and then it goes up to EndUser Accounts which Is parented to other accounts and so on.  I want the Sales Line items to roll up to the EndUser, the EndUsers to Roll Up to the Parents (who might also have sales that i want rolled in) and so on.  

Currently i have something close.  Cant figure out why its not working though.  please advise why this wont do what i am trying to do.
 
public class SalesDataRollingUp {
    private static final Date sixMonthsAgo = Date.today().addMonths(-6);
    private static final Date twelveMonthsAgo = Date.today().addMonths(-12);
    private static final Date startCurrentYear = Date.newInstance(Date.today().year(), 1, 1);
    private static Map<ID, Account> LevelCarryThrough(List<ID> AccountIDs) {
        Map<ID, Account> RelevantAccounts = new Map <ID, Account> ([SELECT ID, Rolling_6_Months__c, Rolling_12_months__c
                                     	    					  FROM Account
                                     	    					  WHERE ID in :AccountIDs]);
        for(Account Parent : RelevantAccounts.values()){
            Parent.Rolling_6_Months__c = 0;
            Parent.Rolling_12_months__c = 0;
            List<Account> ChildAccounts = Parent.ChildAccounts;
            if(ChildAccounts != null && !ChildAccounts.isEmpty()){
                for(Account ChildAcc : ChildAccounts){
                    if(ChildAcc.Rolling_6_Months__c > 0){
                        Parent.Rolling_6_Months__c += ChildAcc.Rolling_6_Months__c;
                    }
                    if(ChildAcc.Rolling_12_months__c > 0){
                        Parent.Rolling_12_months__c += ChildAcc.Rolling_12_months__c;
                    }
                } 
            }
            List<Sales_Data__c> RelatedSales = Parent.Sales_Data1__r;
            if(RelatedSales != null && !RelatedSales.isEmpty()){
                for(Sales_Data__c RelatedSale : RelatedSales){
                    if(RelatedSale.Date__c > sixMonthsAgo){
                        Parent.Rolling_6_Months__c += RelatedSale.Amount__c;
                    }
                    if(RelatedSale.Date__c > twelveMonthsAgo){
                        Parent.Rolling_12_months__c += RelatedSale.Amount__c;
                    }
                }
            }           
        }
        Database.update(RelevantAccounts.values());
        Map<ID, Account> ParentAccountsAll = new Map<ID, Account>([SELECT ParentID
                                    							  FROM Account
                                                                  WHERE ID in :AccountIDs]);
        Map<ID, Account> ParentAccounts = new Map<ID, Account>([SELECT ID
                                    							FROM Account
                                                                WHERE ID in :ParentAccountsAll.keySet()]);
        Return ParentAccounts;
    }
    public void UpdateParentRollings(List<ID> ChildAccountIDs) {
        List<ID> Level1 = new List<ID> (SalesDataRollingUp.LevelCarryThrough(ChildAccountIDs).keyset());
        List<ID> Level2 = new List<ID> (SalesDataRollingUp.LevelCarryThrough(Level1).keyset());
        List<ID> Level3 = new List<ID> (SalesDataRollingUp.LevelCarryThrough(Level2).keyset());
        List<ID> Level4 = new List<ID> (SalesDataRollingUp.LevelCarryThrough(Level3).keyset());
        List<ID> Level5 = new List<ID> (SalesDataRollingUp.LevelCarryThrough(Level4).keyset());
    }
}