I'm writing my first Before Delete trigger (2nd half of the trigger) and am a bit confused about a couple of issues. In this particular case, the isDeleted field doesn't exist on the custom object and since the record hasn't yet been deleted, it also wouldn't have been set to true anyway for me to be able to sort on.

The object the trigger fires on are related to opportunity. The trigger performs roll-up calculation of the number of related records per opportunity (there's a many to one relationship between the object Ids per opportunity) and saves the calculation to a field in Opportunity.

My understanding is that trigger.new will be empty of records, so my question is, will trigger.old only contain the object records that are to be deleted? If not, how can I determined which records have been selected for deletion? I have no other criteria to go on that I know of. Beyond that, I believe the relevant trigger code would look something like this:

trigger RecapRollupToOpp on Recap_Form__c (after insert, before delete) {

   if (Trigger.isBefore && Trigger.isDelete) {            
      set<Id> OIds = trigger.oldmap.keySet();
      system.debug('RecapForms Deleted = '+ OIds.size());
      list <Opportunity>OppsUp = new list<Opportunity>();
      list<Recap_Form__c>PrMoRC = new list<Recap_Form__c>();    
      set<Id>OpId = new set<Id>();

      for(Id OI:OIds){


      map<Id,Opportunity> PrMoUp = new map<Id,Opportunity>([SELECT Id,Recap_Count_New__c, (SELECT Id, PromoEvent__c FROM Recap_Forms_New__r)FROM Opportunity WHERE Id IN: OpId ]);

      set<Id>PrMoIds = PrMoUp.keySet();

      /* Use loops to retrieve lists of Opps and Recap_Forms */
      for (Id OpportunityId : PrMoIds) {

         Opportunity opp = PrMoUp.get(OpportunityId);

         PrMoRC = opp.Recap_Forms_New__r;
         PrMoUp.get(OpportunityId).Recap_Count_New__c = 0; // Reset count to zero

         for(Recap_Form__c RC: PrMoRC){

            system.debug(RC.PromoEvent__c +'='+ OpportunityId);     

               PrMoUp.get(OpportunityId).Recap_Count_New__c = PrMoUp.get(OpportunityId).Recap_Count_New__c + 1;

            }else if(OIds.contains(RC.Id)==true){
               PrMoUp.get(OpportunityId).Recap_Count_New__c = PrMoUp.get(OpportunityId).Recap_Count_New__c - 1;
               // This adds 1 for each related Recap_Form__c returned from the query for the Opportunity
               // and subtracts 1 for each related Recap Form in the set of RC ID's being deleted to get a new total

         } // end for(Recap_Form__c RC: PrMoRC) // Recap_Form__c

         if(PrMoUp.get(OpportunityId).Recap_Count_New__c < 0){ 
            PrMoUp.get(OpportunityId).Recap_Count_New__c = 0;
            // can't have a value less than zero
      } // end for for (Id OpportunityId : PrMoIds)

      system.debug('Num Opps Updated = '+ OppsUp.size() + ' vs Recaps Deleted = '+ OIds.size());

      if(OppsUp.isEmpty() == false) update OppsUp;

   } // END if (Trigger.isBefore && Trigger.isDelete)       

1 Answer 1


When the Trigger.isDelete is true, all the records in Trigger.old are being deleted.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .