+ Start a Discussion
bathybathy 

Apex trigger to Update the Parent record and delete the record

Hi,

I am trying to develop a trigger to Update the Aprent Record and Delete the curretn record. We have a contract custom object and Offer custom object where Offer is the parent and COntract id the child. My requirement is once the COntract is submitted for cancellation(I customised approval process to cancellation process) and then its cancellation is approved then the Offer(parent Object) Status(custom Field in Offer) must be updated to Cancelled and the child contract record must be deleted.

so far I have developed the following trigger:
trigger Updateoffer on pba__Closing__c (after update) {
  
  Set<String> offerids= new Set<String>();
  
    for (pba__Closing__c cl : Trigger.new) 
    {
      if(cl.Cancellation_Approved__c == true){
      offerids.add(cl.pba__Offer__c);
  }}
  
  
     List<pba__Offer__c> offers= [SELECT Id,pba__SystemIsCancelled__c  FROM pba__Offer__c WHERE Id =: offerids];
    
     Map<String, pba__Closing__c> cprmap = new Map<String, pba__Closing__c>();
  
    for (pba__Closing__c c : Trigger.new){
      
       
          cprmap.put(c.pba__Offer__c, c);
        }
      
     
        
    for (pba__Offer__c o : offers) {
      if (cprmap.containsKey(o.Id)) 
      {
      
       
        {
        o.pba__SystemIsCancelled__c  =  true;
        }
        
        
      }
  }
  update offers;
  delete [select id from pba__Closing__c where id in :Trigger.new];
}
This trigger is deleting the newly created Contracts, i belive it is because there is no if condition for the select statement at the bottom. Can anyone suggest corrections for this.
Thanks in advance.
 
BalajiRanganathanBalajiRanganathan
you need to create one more set to store the list pba__Closing__c ids where Cancellation_Approved__c is true (populate in the first for loop).

and use the set in the delete SOQL instead of Trigger.new
JWykelJWykel
Pseudo-code based on your description:
trigger ContractTrigger on Contract(after update){
    Map<Id, Offer> offers = new Map<Id, Offer>();
    Set<Contract> contracts = new Set<Contract>();
    for(Contract c : Trigger.new){
       if(c.IsCancellation && c.IsApproved){
            offers.put(c.OfferId, new Offer(Id = c.OfferId, Status = 'Cancelled'));
            contractIds.add(c);
        }
    }
        
    if(!offers.isEmpty()) update offers;
    if(!contracts.isEmpty()) delete contracts;
}

If that last line doesn't work (might not be able to delete the same records of an update); move the delete logic into the OfferTrigger after update:
trigger ContractTrigger on Contract(after update){
    Map<Id, Offer> offers = new Map<Id, Offer>();
    for(Contract c : Trigger.new){
       if(c.IsCancellation && c.IsApproved){
            offers.put(c.OfferId, new Offer(Id = c.OfferId, Status = 'Cancelled'));
            contractIds.add(c);
        }
    }
        
    if(!offers.isEmpty()) update offers;
}

trigger OfferTrigger on Offer(after update){
    Set<Id> offerIds = new Set<Id>();
    for(Offer o : Trigger.new){
        if(o.Status == 'Cancelled' && Trigger.oldMap.get(o.Id).Status != 'Cancelled'){
            offerIds.add(o.Id);
        }
    }

    if(!offerIds.isEmpty()){
        delete [SELECT Id FROM Contract WHERE IsCancellation = true AND IsApproved = true AND OfferId IN :offerIds];
    }
}

 
bathybathy
Hi guys,

thanks for your help. Appreciate that. I was able to accomplish this by creating two separate Apex triggers.

Regards,