You need to sign in to do that
Don't have an account?
Mr. CooL....
Write a trigger to calculate all opportunity amount related to its account and update the total/sum of opportunity amount in an account field name as Total_Opportunity_Amount__c
solve the trigger
Here's the solution for your question.
TRIGGER:
trigger DevOppQue on Opportunity (after insert,after update) {
DevOppQueHandler.ans(Trigger.new);
}
HANDLER:
public class DevOppQueHandler {
public static void ans(List<Opportunity> oppList){
Set<Id> accIds = new Set<Id>();
for(Opportunity opp: oppList){
if(opp.Amount != null && opp.AccountId != null){
accIds.add(opp.AccountId);
}
}
List<AggregateResult> agList = [Select SUM(Amount) amount,accountId Id from Opportunity where AccountId =:accIds GROUP BY AccountId];
List<Account> accList = [Select id,name,Total_Opportunity_Amount__c from Account where Id =:accIds];
for(Account acc:accList){
for(AggregateResult ag:agList){
if(acc.Id == (Id)ag.get('Id')){
acc.Total_Opportunity_Amount__c = (Decimal)ag.get('amount');
}
}}
update accList;
}
}
Hope this is helpful.
Thank you.
All Answers
Please try with the below code :
If it helps please mark this as Best Answer.
Thank you.
Here's the solution for your question.
TRIGGER:
trigger DevOppQue on Opportunity (after insert,after update) {
DevOppQueHandler.ans(Trigger.new);
}
HANDLER:
public class DevOppQueHandler {
public static void ans(List<Opportunity> oppList){
Set<Id> accIds = new Set<Id>();
for(Opportunity opp: oppList){
if(opp.Amount != null && opp.AccountId != null){
accIds.add(opp.AccountId);
}
}
List<AggregateResult> agList = [Select SUM(Amount) amount,accountId Id from Opportunity where AccountId =:accIds GROUP BY AccountId];
List<Account> accList = [Select id,name,Total_Opportunity_Amount__c from Account where Id =:accIds];
for(Account acc:accList){
for(AggregateResult ag:agList){
if(acc.Id == (Id)ag.get('Id')){
acc.Total_Opportunity_Amount__c = (Decimal)ag.get('amount');
}
}}
update accList;
}
}
Hope this is helpful.
Thank you.