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
SV MSV M 

Undelete action isn't working in my apex Trigger

Hi,
I have an apex trigger to count the total Opportunities related to an Account and Sum of Opportunity Amount related to Account. I am trying to implement "after undelete" in my trigger. But it isn't working. Can someone help to do this?
Below is my Trigger

trigger OpportunityCount on Opportunity (after insert, after delete, after update, after Undelete) {
    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<AggregateResult> lstResult = [SELECT AccountId, COUNT(Id) countId 
                                           FROM Opportunity 
                                           WHERE AccountId IN:acctIds
                                           GROUP BY AccountId];
        
        List<Account> lstAccount = new List<Account>();
        for(AggregateResult result:lstResult){
            Account acct = new Account (Id=(Id)result.get('AccountId'), Total_Opportunities__c = (Integer)result.get('countId'));
            lstAccount.add(acct);
        }
        update lstAccount;  
        
        List<Account> acctList = new List<Account>();
        acctList = [SELECT Total_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_Amount__c = totalOpptyAmount;
        }
        update acctList;
    }
}
Poornima HR 5Poornima HR 5
The execution logic depends on whether the set acctIds is populated. And you are populating only during insert, update or delete. 
Use a block like this for undelete as well :

if(trigger.isUnDelete){
        for(Opportunity oppty : trigger.old){
            if(oppty.AccountId != null){
                acctIds.add(oppty.AccountId);
            }
        }    
    }
 
SV MSV M
Hi, I've tried adding your code snippet but still, it doesn't work for me. Is there any other way to achieve unDelete..
sachinarorasfsachinarorasf
Hi Sai,

I have gone through your problem. 

Please you this block for after undelete event.

if(trigger.isUnDelete){
    for(Opportunity oppty : trigger.new){
        if(oppty.AccountId != null){
            acctIds.add(oppty.AccountId);
        }
    }    
}

I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.

Thanks and Regards,
Sachin Arora
@ M  Coder@ M Coder
Hello , 
The above code will now  work using trigger inser or update due to this " trigger.isUpdate && trigger.isInsert " . here i used custom field Amount__c
updated code :
trigger OpportunityCount on Opportunity (after insert, after delete, after update, after Undelete) {
    Map<Id, List<Opportunity>> acctIdOpptyListMap = new Map<Id, List<Opportunity>>();
    Set<Id> acctIds = new Set<Id>();
    List<Opportunity> opptyList = new List<Opportunity>();
    
    if(trigger.isInsert && trigger.isUpdate  ){
        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(trigger.isUnDelete){
    for(Opportunity oppty : trigger.new){
        if(oppty.AccountId != null){
            acctIds.add(oppty.AccountId);
        }
      }    
    }
    if(acctIds.size() > 0)
    {
        opptyList = [SELECT Amount__c, 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<AggregateResult> lstResult = [SELECT AccountId, COUNT(Id) countId FROM Opportunity WHERE AccountId IN:acctIds  GROUP BY AccountId];
        
        List<Account> lstAccount = new List<Account>();
      
      for(AggregateResult result:lstResult){
            Account acct = new Account (Id=(Id)result.get('AccountId'), Total_Opportunities__c = (Integer)result.get('countId'));
            lstAccount.add(acct);
        }
        update lstAccount;  
        
        List<Account> acctList = new List<Account>();
        acctList = [SELECT Total_Amount__c__c FROM Account WHERE Id IN: acctIds];
        for(Account acct : acctList)
        {
            List<Opportunity> tempOpptyList = new List<Opportunity>();
            tempOpptyList = acctIdOpptyListMap.get(acct.Id);
            Double totalOpptyAmount__c = 0;
            
            for(Opportunity oppty : tempOpptyList)
            {
                if(oppty.Amount__c != null){
                    totalOpptyAmount__c += oppty.Amount__c;
                }
            }
         acct.Total_Amount__c__c = totalOpptyAmount__c;
        }
        update acctList;
    }
}