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
tom sanchestom sanches 

Trigger that updates a custom variable

I have a trigger that updates a custom variable. (after insert/update/delete). This works, but I want the trigger to fire and update the variable for all entries. But so that my records are not updated, inserted and deleted
Best Answer chosen by tom sanches
mukesh guptamukesh gupta
Hi Ekatephaa,

Please use below code:-
 
trigger Opportunity_AIUD on Opportunity (after update, after insert, after delete) {
    Set<Id> acctIds = new Set<Id>();
    List<Opportunity> opptyList = new List<Opportunity>(); 

    opptyList = (trigger.isUpdate || trigger.isInsert) ? trigger.New : trigger.old;
    for(Opportunity oppty : opptyList){
        if(oppty.AccountId != null){
            acctIds.add(oppty.AccountId);
        }
    }    

    if(acctIds.size() > 0)
    {
        List<Account> acctList = new List<Account>();
        AggregateResult[] groupedResults  = [SELECT SUM(Amount), AccountId 
                                             FROM Opportunity 
                                             WHERE StageName = 'Closed Won' 
                                             AND AccountId IN : acctIds GROUP BY AccountId];        

        for (AggregateResult ar : groupedResults)  {          
            Account acct = new Account (Id = (Id)ar.get('AccountId'));
            acct.Total_Opportunity_Amount__c = (Decimal)ar.get('expr0');
          acctList.add(acct);
        }        

  if(acctList.size() >0){
     update acctList;
   }
       
    }
}
if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 

All Answers

AbhinavAbhinav (Salesforce Developers) 
Please share the relevant code as well.
tom sanchestom sanches
trigger Opportunity_AIUD on Opportunity (after update, after insert, 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 StageName = 'Closed Won' AND 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 Total_Opportunity_Amount__c FROM Account WHERE Id IN: acctIds];
        for(Account acct : acctList){
            List<Opportunity> tempOpptyList = new List<Opportunity>();
            tempOpptyList = acctIdOpptyListMap.get(acct.Id);
            Double totalOpptyAmount = 0;
            for(Opportunity oppty : tempOpptyList){
                if(oppty.Amount != null){
                    totalOpptyAmount += oppty.Amount;
                }
            }
            acct.Total_Opportunity_Amount__c = totalOpptyAmount;
        }
        update acctList;
    }
}

My variable Total_Opportunity_Amount__c in Account object 
Abdul KhatriAbdul Khatri
Hi Екатерина

Not sure If I understood you rquirements but I was able to reduce your code with many lines. Here is the code. Also please clarify a little more abotu your requirements.
 
trigger Opportunity_AIUD on Opportunity (after update, after insert, after delete) {
    Set<Id> acctIds = new Set<Id>();
    List<Opportunity> opptyList = new List<Opportunity>(); 

    opptyList = (trigger.isUpdate || trigger.isInsert) ? trigger.New : trigger.old;
    for(Opportunity oppty : opptyList){
        if(oppty.AccountId != null){
            acctIds.add(oppty.AccountId);
        }
    }    

    if(acctIds.size() > 0)
    {
        List<Account> acctList = new List<Account>();
        AggregateResult[] groupedResults  = [SELECT SUM(Amount), AccountId 
                                             FROM Opportunity 
                                             WHERE StageName = 'Closed Won' 
                                             AND AccountId IN : acctIds GROUP BY AccountId];        

        for (AggregateResult ar : groupedResults)  {          
            Account acct = new Account (Id = (Id)ar.get('AccountId'));
            acct.Total_Opportunity_Amount__c = (Decimal)ar.get('expr0');
        }        
        update acctList;
    }
}
mukesh guptamukesh gupta
Hi Ekatephaa,

Please use below code:-
 
trigger Opportunity_AIUD on Opportunity (after update, after insert, after delete) {
    Set<Id> acctIds = new Set<Id>();
    List<Opportunity> opptyList = new List<Opportunity>(); 

    opptyList = (trigger.isUpdate || trigger.isInsert) ? trigger.New : trigger.old;
    for(Opportunity oppty : opptyList){
        if(oppty.AccountId != null){
            acctIds.add(oppty.AccountId);
        }
    }    

    if(acctIds.size() > 0)
    {
        List<Account> acctList = new List<Account>();
        AggregateResult[] groupedResults  = [SELECT SUM(Amount), AccountId 
                                             FROM Opportunity 
                                             WHERE StageName = 'Closed Won' 
                                             AND AccountId IN : acctIds GROUP BY AccountId];        

        for (AggregateResult ar : groupedResults)  {          
            Account acct = new Account (Id = (Id)ar.get('AccountId'));
            acct.Total_Opportunity_Amount__c = (Decimal)ar.get('expr0');
          acctList.add(acct);
        }        

  if(acctList.size() >0){
     update acctList;
   }
       
    }
}
if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

 
This was selected as the best answer