0% found this document useful (0 votes)
25 views2 pages

Roll Up Summary

codes related to roll up summary trigger in salesforce , benefits while interview preparations and practice

Uploaded by

mahendra bhamare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views2 pages

Roll Up Summary

codes related to roll up summary trigger in salesforce , benefits while interview preparations and practice

Uploaded by

mahendra bhamare
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 2

trigger AccountOppRollUpSummary on Opportunity (after insert, after update, after

delete, after undelete) {

public static void countContacts(List<Contact> newOpp , List<Contact> oldOpp){


Set<Id> oppByIds = new Set<Id>();
=---------------------------------------------------

if(newOpp!=null){
for(Opportunity c : newOpp){
if(c.AccountId!=null){
oppByIds.add(c.AccountId);
}
}
}
if(oldOpp!=null){
for(Opportunity c : oldOppp){
oppByIds.add(c.AccountId);
}
}
---------------------------------------------------------
------------------

Map<Id,Account> accountMapToUpdate = new Map<Id,Account>();

if(oppByIds.size()>0){

//Using Aggregate functions in SOQL, to Count(), SUM() and MAX(), for roll up
and summarize data in a query.

for(AggregateResult aggr:[SELECT
Count(Id)ids,AccountId,SUM(Amount)amt,MAX(Paid_On__c)maxdate

FROM Opportunity WHERE AccountId IN:oppByIds GROUP BY AccountId])


{

Account acc = new Account();

acc.Total_Amount__c = (Decimal)aggr.get('amt');

acc.Total_Opportunity__c = (Integer)aggr.get('ids');

acc.Recent_Pay__c = (Date)aggr.get('maxdate');

acc.Id = (Id)aggr.get('AccountId');

accountMapToUpdate.put(acc.Id, acc);
}
}
try{
update accountMapToUpdate.values();

}catch(Exception e){
System.debug('Exception Message'+e.getMessage()+'Line No.
>>'+e.getLineNumber());
}
}

You might also like