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
Nandhini GovindrajNandhini Govindraj 

total revenue of all child accounts

I have created a field :Total Annual Revenue .
Result should be like
Parent Ann revenue-5 total ann rev-12
Child Ann rev-2 total Ann Rev-7
Grant Child Ann rev-5 total Ann Rev-0
 
i have written code as below
 
trigger TotalAnnualRevenueChildAccount on Account (after insert,after update) {
  List<Account> ac=new List<Account>();
    Set<Id> accids=new Set<Id>();
             for(Account acc:trigger.new){
            accids.add(acc.Id);      

if(accids != null){
List<Account> acclist = [Select Id, AnnualRevenue from Account Where ID IN :accIds];
                                for(Account acc: acclist){                                               
                                                                acc. Total_AnnualRevenue__c+= acc.AnnualRevenue ;
                                                                ac.add(acc);                              
                                }
                }
                Update ac;
}


Can you help me in this
Thanks
PriyaPriya (Salesforce Developers) 

Hey Nandhini,

Try to add the debug statement to check what is the value coming in the list. 

then write it like this :-
If(list.size>0){
   update acc;
}

Also you are using 'After Update' event, then this trigger will run in loop. Please remove that event first and then try.

Regards,

Priya Ranjan