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
jpbenjpben 

Revenue on Account to roll up to Parent Account

Hi All,

 

I need some help to figure out a solution to this issue.  We have Annual Revenue field on the Account record.  I want to be able to calcualate the sum of Annual Revenue from all the child Accounts + the Annual Revenue of the Parent Account.  I want the sum value to be on the Parent Account record.  Also, we have some accounts that have more than 1 level of hierarchy.  

 

Account A

    > Account B

     > Account C

              >  Account D

 

If you have any sample codes to share, I'd appreciate it.  Thanks in advance.  

 

kriskkrisk

Between Parent account and Child account how many layers of nesting do you have. From your post it appears like you might have an Account A with 2 Child accounts and each of these child accounts can have their own child accounts.

 

In this case you a roll up summary or another roll up summary field which is not possible. So you might have to do some kind of Trigger to roll up the totals into the Parent object 

anonymousXYZanonymousXYZ
trigger accountlookup on Account (after insert,after update, after delete,after undelete) {

    List<account> accountListToUpdate=new list<account>();
	set<id> parentId=new set<id>();
    list<id> childFoundList=new list<id>();
    
    
    if(Trigger.isInsert || Trigger.isUpdate || Trigger.isUndelete){
        for(Account acc:Trigger.new){
            parentId.add(acc.parentid);
            if(Trigger.isUpdate){
                if(acc.parentid!=Trigger.oldMap.get(acc.id).parentid){
                    parentid.add(Trigger.oldMap.get(acc.id).parentid);
                }
        	}
        }
    }
    if(Trigger.isDelete){
        for(Account acc:Trigger.old){
            parentId.add(acc.parentid);
        }
    }
       
    List<AggregateResult> AggregateResultlist = [select parentid,sum(annualrevenue) arSum from account 
                                                     where parentid=:parentId AND parentId!= null group by parentid];
    
        if(!AggregateResultlist.isEmpty()){
            for(AggregateResult ar:AggregateResultlist){
                Account a=new Account(id=(id)ar.get('parentid'),annualrevenue=(Double)ar.get('arSum'));
				accountListToUpdate.add(a);
                if(Trigger.isDelete){
        			childFoundList.add(a.id);
    			}
            }
         }
    if(Trigger.isDelete){
        for(Account a: Trigger.old){
            if(!childFoundList.contains(a.parentid)){
                System.debug('lst='+childFoundList+'parentid= '+a.parentid);
                Account acc=new Account(id=a.ParentId,annualrevenue=0);
                accountListToUpdate.add(acc);
            }
        }
    }
        if(!accountListToUpdate.isEmpty()){
            System.debug('account- '+accountListToUpdate);
            update accountListToUpdate;
        }
    
}