+ Start a Discussion
Fan18Fan18 

Assitance writing an after delete trigger

Good day,

On below trigger, my Insert and Update works. Please assist in an after delete trigger:

trigger calcClaim on Claim__c (after Insert, after Update, after Delete){
    
    set<Id> OccIds = new set<Id>();
    integer count = 0;
    for(Claim__c cl: Trigger.new)
    {    
        decimal temp = 0.0;
        try{temp = Trigger.old[count].Initial_Estimate__c;}
        Catch(exception e)
        {}
        if(cl.Initial_Estimate__c != temp)
        {
            OccIds.add(cl.Occurrence__c);
        } else
            if(Trigger.isDelete){
            /*{for(Claim__c c : trigger.old)
                system.debug(trigger.old);
                OccIds.add(cl.Occurrence__c);}*/
              count++;        
    }
    List<RK_Occurrence__c> occs  = ([SELECT id FROM RK_Occurrence__c where Id in :OccIds]);
    system.debug(occs);
    
    OccurenceClaimCalculator.calculateOcc(occs);
    
    }

SRKSRK

hi 
i think you should change you event to before delete, you can try below code and see if it work for you
trigger.new will not return anything as it doesn't know the value after delete and considered as Nul exception.
It should be before delete event and trigger.old
trigger calcClaim on Claim__c (after Insert, after Update, Before Delete){
set<Id> OccIds = new set<Id>();
    integer count = 0;
    for(Claim__c cl :  (Trigger.IsDelete ? Trigger.old: Trigger.new) )
    {    
        decimal temp = 0.0;
        try{
          if its update then only value of "Initial_Estimate__c " can change, and if its value got change then only record is under considreration , but if its delete or insert value will not change its either getting inserted or getting deleted so i assume in this case record iwill by defult will go under considreration
        if(Trigger.IsDelete || Trigger.IsInsert || (Trigger.IsUpdate && cl.Initial_Estimate__c != trigger.oldmap(cl.id).Initial_Estimate__c))
       {
                OccIds.add(cl.Occurrence__c);
       }

     }
          Catch(exception e)
         {

cl.adderror(e.geterrormessage());
         }
  } 

if(     !(OccIds.isempty() ){
    List<RK_Occurrence__c> occs  = ([SELECT id FROM RK_Occurrence__c where Id in :OccIds]);
    system.debug(occs);
    
    OccurenceClaimCalculator.calculateOcc(occs);
    }
    }

Fan18Fan18

Hi SRK,
I will try this and let you know