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
Mr. CooL....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
 
Best Answer chosen by Mr. CooL....
Arun Kumar 1141Arun Kumar 1141
Hi @Mr. CooL....


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

SubratSubrat (Salesforce Developers) 
Hello ,

Please try with the below code :
 
trigger UpdateTotalOpportunityAmount on Opportunity (after insert, after update, after delete, after undelete) {
    // create a map to store the account IDs and their corresponding total opportunity amount
    Map<Id, Decimal> accountToTotalOppAmount = new Map<Id, Decimal>();
    
    // iterate through the opportunities to calculate the total opportunity amount for each account
    for (Opportunity opp : [SELECT AccountId, Amount FROM Opportunity WHERE AccountId IN :Trigger.newMap.keySet()]) {
        if (opp.AccountId != null) {
            Decimal totalOppAmount = accountToTotalOppAmount.containsKey(opp.AccountId) ? accountToTotalOppAmount.get(opp.AccountId) : 0;
            totalOppAmount += opp.Amount;
            accountToTotalOppAmount.put(opp.AccountId, totalOppAmount);
        }
    }
    
    // update the total opportunity amount for each account
    List<Account> accountsToUpdate = new List<Account>();
    for (Id accountId : accountToTotalOppAmount.keySet()) {
        accountsToUpdate.add(new Account(Id = accountId, Total_Opportunity_Amount__c = accountToTotalOppAmount.get(accountId)));
    }
    update accountsToUpdate;
}

If it helps please mark this as Best Answer.
Thank you.
Arun Kumar 1141Arun Kumar 1141
Hi @Mr. CooL....


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.

 
This was selected as the best answer