Activity › Forums › Salesforce® Discussions › How can I create rollup Summary field in Account Object with Opportunity Amount using Triggers?
Tagged: Account, Account Object, Rollup, Rollup Summary, Salesforce Objects, Salesforce Opportunity, Salesforce Trigger
-
How can I create rollup Summary field in Account Object with Opportunity Amount using Triggers?
Posted by Aman on July 13, 2017 at 1:21 PMI want to create a rollup summary field using triggers?
Aileen Kim replied 2 years, 10 months ago 4 Members · 3 Replies -
3 Replies
-
Hi Aman,
First make a field on account object with name “OppAmount” and then use this code:
#Trigger
trigger calAmount on Opportunity (after insert, after update, after delete) {
Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
Set<Id> acctIds = new Set<Id>();
List<Opportunity> opptyList = new List<Opportunity>();
if(trigger.isUpdate || trigger.isInsert){
for(Opportunity oppty : trigger.New){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(trigger.isDelete){
for(Opportunity oppty : trigger.old){
if(oppty.AccountId != null){
acctIds.add(oppty.AccountId);
}
}
}
if(acctIds.size() > 0){
opptyList = [SELECT Amount, AccountId FROM Opportunity WHERE AccountId IN : acctIds];
for(Opportunity oppty : opptyList){
if(!acctIdOpptyListMap.containsKey(oppty.AccountId)){
acctIdOpptyListMap.put(oppty.AccountId, new List<Opportunity>());
}
acctIdOpptyListMap.get(oppty.AccountId).add(oppty);
}
List<Account> acctList = new List<Account>();
acctList = [SELECT OppAmount__c FROM Account WHERE Id IN: acctIds];
for(Account acct : acctList){
List<Opportunity> tempOpptyList = new List<Opportunity>();
tempOpptyList = acctIdOpptyListMap.get(acct.Id);
Double OppAmount = 0;
for(Opportunity oppty : tempOpptyList){
if(oppty.Amount != null){
OppAmount += oppty.Amount;
}
}
acct.OppAmount__c = OppAmount;
}
update acctList;
}
}Hope this may help u.
- [adinserter block='9']
-
Hello Aman,
First make a field on account object with name “OppAmount” and then use this code:
I have written a sample trigger for you:
#Trigger
trigger totalAmount on Opportunity (after insert, after update, after delete) {
Map<Id, List<Opportunity>> acctIdOppoListMap = new Map<Id, List<Opportunity>>();
Set<Id> acctIds = new Set<Id>();
List<Opportunity> oppoList = new List<Opportunity>();
if(trigger.isUpdate || trigger.isInsert){
for(Opportunity oppo : trigger.New){
if(oppo.AccountId != null){
acctIds.add(oppo.AccountId);
}
}
}
if(trigger.isDelete){
for(Opportunity oppo : trigger.old){
if(oppo.AccountId != null){
acctIds.add(oppo.AccountId);
}
}
}
if(acctIds.size() > 0){
oppoList = [SELECT Amount, AccountId FROM Opportunity WHERE AccountId IN : acctIds];
for(Opportunity oppo : oppoList){
if(!acctIdOppoListMap.containsKey(oppo.AccountId)){
acctIdOppoListMap.put(oppo.AccountId, new List<Opportunity>());
}
acctIdOppoListMap.get(oppo.AccountId).add(oppo);
}
List<Account> acctList = new List<Account>();
acctList = [SELECT Amount__c FROM Account WHERE Id IN: acctIds];
for(Account acct : acctList){
List<Opportunity> tempOppoList = new List<Opportunity>();
tempOppoList = acctIdOppoListMap.get(acct.Id);
Double OpportunityAmount = 0;
for(Opportunity oppo : tempOppoList){
if(oppo.Amount != null){
OpportunityAmount += oppo.Amount;
}
}
acct.Amount__c = OpportunityAmount;
}
update acctList;
}
}Hope, this will solve your problem.
Thanks!!
-
Hi @shaharyar and @parul-2 the code is very helpful however, it also calculates Closed Lost Opportunities. How do I filter it and only calculate the Opportunities that are in Closed Won and Invoice Sent? Thank you.
Log In to reply.