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
swain 10swain 10 

Trigger to update Parent account field as rollup from Child account field (Multilevel) when a parent account is added

My scenario , when a Parent account is added to a Child account then the value(sum of all child) of  one field should updated to the Parent Account Field. 
Total_Lease_Area_SF_from_Children__c (Number Field) needs to get updated in parent account from the Total_Account_Child_Lease_Area_SF__c(Formula Field) of child Account.
Note:- Total_Account_Child_Lease_Area_SF__c(Formula Field) is the sum of all child till that heirrachy updated by Total_Lease_Area_SF_from_Children__c (Number Field). But When a new account is added to an existing Account parent account field is not geeting updated.

trigger AccLeaseUpdate on Account   (after update, after delete) {
   
        set<id> ParentIds = new set<id>();
        if(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.isupdate || trigger.isdelete){
          
            For(account q : [SELECT Total_Lease_Area_SF_from_Children__c ,ParentId ,(SELECT id,Total_Lease_Area_SF_from_Children__c,Total_Account_Child_Lease_Area_SF__c FROM  ChildAccounts) FROM account WHERE id =: ParentIds]){ 
                if(q.ParentId!= null && q.Total_Lease_Area_SF__c!= null){
                sum = 0;
                for(Account p : q.ChildAccounts){
             //    for(Account ap : trigger.new){
            //      Account myParentAcc = aq.get(p.ParentId);
                    
                    sum = sum +p.Total_Lease_Area_SF_from_Children__c;
                    q.Total_Lease_Area_SF_from_Children__c= sum  ;
                 //   myParentAcc.Total_Lease_Area_SF_from_Children__c = sum; 
                    }
                    }
try{
                update accountToUpdate ;
            }Catch(Exception e){
                System.debug('Exception :'+e.getMessage());
            }
        
        }
                
                
                accountToUpdate.add(q);
                }
            }*/
Steven NsubugaSteven Nsubuga
Try this.

You need a class to prevent recursive calling of the trigger.
public class CheckRecursive { 
   public static boolean firstRun = true; 
}
Then here is the trigger
trigger AccLeaseUpdate on Account (after update, after delete) {
   
	if(CheckRecursive.firstRun){
		CheckRecursive.firstRun = false;
		set<id> ParentIds = new set<id>();
		if(Trigger.isupdate){
			For(Account acc: Trigger.new){
				ParentIds.add(acc.ParentId);
			}
		}
		if(Trigger.isDelete){
			For(Account acc: Trigger.old){
				ParentIds.add(acc.ParentId);
			}
		}
			
		Map<Id, Account> parentAccountsMap = new Map<Id, Account>([SELECT Id, Total_Lease_Area_SF_from_Children__c FROM Account WHERE Id IN :ParentIds]);
		List<AggregateResult> ars = [SELECT parentId, SUM(Total_Account_Child_Lease_Area_SF__c) FROM Account WHERE parentId IN :ParentIds GROUP BY parentId];
		
		for (AggregateResult ar : ars) {
			parentAccountsMap.get(String.valueOf(ar.get('parentId'))).Total_Lease_Area_SF_from_Children__c = Integer.valueOf(ar.get('expr0')); 
		}
		update parentAccountsMap.values();
	}
}


 
swain 10swain 10

Thanks a lot Steven Nsubuga
Steven NsubugaSteven Nsubuga
My pleasure @Srikant!!