• Kiran Hunnaragi 25
  • NEWBIE
  • 0 Points
  • Member since 2022

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 1
    Questions
  • 1
    Replies
Hi All,

We have a requirement to calculate all child accounts amount field and show it on the parent account.

We have look up relationship so we need to write a trigger for this. Can I someone help on this requirement??

I have got a sample code in google however it is not working please check

Amount field 
__________________________________________________________
trigger ParentAccUpdate on Account  (after insert,after update, after delete,after undelete) {
    if(checkRecursive.runOnce())
        
    {
        
        
        List<id> ParentIds = new List<id>();
        if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
            For(Account Acc: Trigger.new){
                ParentIds.add(Acc.parentid);
            }
        }
        if(Trigger.isDelete){
            For(Account Acc: Trigger.old){
                ParentIds.add(Acc.parentid);
            }
        }
        List<account> accountToUpdate = new List<account>();
        decimal sum = 0;
        
        if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate || trigger.isdelete){
            For(account q : [SELECT Total_Revenue_2021__c ,X2020_Revenue__c, (SELECT id,X2020_Revenue__c,Total_Revenue_2021__c FROM  ChildAccounts) FROM account WHERE id =: ParentIds]){ 
                sum = 0;
                
                for(Account p : q.ChildAccounts){
                    
                    sum = sum + (q.X2020_Revenue__c!=NULL ? q.X2020_Revenue__c : 0);//Use the fields you want here to get the sum and include those                     fields in above query eg is given below from my understanding
                                   

                    q.Total_Revenue_2021__c = sum;
                    
                    
                }
                accountToUpdate.add(q);
            }
            try{
                update accountToUpdate ;
            }
            Catch(Exception e){
                System.debug('Exception :'+e.getMessage());
            }
        }
    }   
}

 
Hi All,

We have a requirement to calculate all child accounts amount field and show it on the parent account.

We have look up relationship so we need to write a trigger for this. Can I someone help on this requirement??

I have got a sample code in google however it is not working please check

Amount field 
__________________________________________________________
trigger ParentAccUpdate on Account  (after insert,after update, after delete,after undelete) {
    if(checkRecursive.runOnce())
        
    {
        
        
        List<id> ParentIds = new List<id>();
        if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate){
            For(Account Acc: Trigger.new){
                ParentIds.add(Acc.parentid);
            }
        }
        if(Trigger.isDelete){
            For(Account Acc: Trigger.old){
                ParentIds.add(Acc.parentid);
            }
        }
        List<account> accountToUpdate = new List<account>();
        decimal sum = 0;
        
        if(Trigger.isInsert || Trigger.isUndelete || Trigger.isupdate || trigger.isdelete){
            For(account q : [SELECT Total_Revenue_2021__c ,X2020_Revenue__c, (SELECT id,X2020_Revenue__c,Total_Revenue_2021__c FROM  ChildAccounts) FROM account WHERE id =: ParentIds]){ 
                sum = 0;
                
                for(Account p : q.ChildAccounts){
                    
                    sum = sum + (q.X2020_Revenue__c!=NULL ? q.X2020_Revenue__c : 0);//Use the fields you want here to get the sum and include those                     fields in above query eg is given below from my understanding
                                   

                    q.Total_Revenue_2021__c = sum;
                    
                    
                }
                accountToUpdate.add(q);
            }
            try{
                update accountToUpdate ;
            }
            Catch(Exception e){
                System.debug('Exception :'+e.getMessage());
            }
        }
    }   
}