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
SoundarSoundar 

Rollup Trigger For Account

Hi,

I need to update Opportunities SUM(Amount) in Account field (Total_Opportunities_Amount__c) .

So i have create a small trigger as below. Now i am getting size of opportunities, right now i need  SUM,AVG,MIN & MAX of Opportunities (i can update in account fields__c). Please review my code and advice me once how can we achieve this process.

 
trigger ContactRollUp on Contact (after insert, after update,after delete) {
 

    
   List<Id> conList = New List<Id>();
    
    if(Trigger.isInsert || Trigger.isUndelete){
        for(Contact con : Trigger.new){
            conList.add(con.accountId);
        }
    }

    if(Trigger.isdelete){
        for(Contact con : Trigger.old){
            conList.add(con.accountId);
        }
    }
    
    List<Account> accList = New List<Account>();
    
    List<aggregateResult> opp = [Select  SUM(amount) sumAmt from opportunity];
    
    For(Account acc : [Select id,name, (Select id,amount from opportunities) from Account Where id =:conList]){
                //acc.Number_Of_Contacts__c = acc.Contacts.size();
               acc.Total_Opportunities__c = acc.Opportunities.size();   // Number Of Opp
               // acc.Opportunity_Total_Amount__c  = acc.opportunities.SUM(amount);
               accList.add(acc);
    } 
    
    if(accList.size() > 0){
        
        update accList;
    }
}

Regards,

Soundar Raj,
+91- 7418425418​
ManojjenaManojjena
Hi Soundar,

You need to write trigger on Opportunity and needs to create different fields for roll up min.Max,Avg and  sum You can add rest functions like SUM I have added in query then update.

Check below code it will work to update sum of amount to account .
 
trigger OpportunityRollUp   on Opportunity  ( after insert, after update,after delete,after undelete) {
	 Set<Id> accIdSet=new Set<Id>();
	 List<Account> accListToUpdate=new List<Account>();
	 if(Trigger.isInsert || Trigger.isUndelete){
		  for(Opportunity  opp : Trigger.new){
			 if(opp.AccountId != null){
				accIdSet.add(opp.AccountId);
			 }		
		  }
	 }
	 if(Trigger.isUpdate){
	    for(Opportunity  opp : Trigger.new){
			 if(opp.AccountId != null && opp.Amount != trigger.oldMap.get(opp.Id).Amount){
				accIdSet.add(opp.AccountId);
			 }		
		 }
	 }
	 If(Trigger.isDelete){
		for(Opportunity  opp : Trigger.old){
			if(opp.AccountId != null){
				accIdSet.add(opp.AccountId); 
			}		
		}
	}
	if(!accIdSet.isEmpty()){
		for(AggregateResult res : [SELECT AccountId,sum(Amount)can FROM Opportunity  WHERE AccountId IN : accIdSet GROUP BY AccountId]) {
			accListToUpdate.add(new Account(Id=(Id)res.get('AccountId'),Total_Opportunities_Amount__c=(Double)res.get('can')));
		}
	}
	if(!accListToUpdate.isEmpty()){
		try{
			update accListToUpdate;
		 }catch(DmlException de){
			System.debug(de);
		 }
	 }
}
Let me know if it helps !!
Thanks
Manoj