You need to sign in to do that
Don't have an account?
Soundar
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.
Regards,
Soundar Raj,
+91- 7418425418
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
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 .
Let me know if it helps !!
Thanks
Manoj