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
PallavPallav 

Issue with after and before delete trigger

Hi,

I have a issue with my code for the delete trigger. I have a custom field in the Product2 as Welcome_field__c and same is there in the opportunity page. The logic is when some one adds a product with that field name cheked and if the field in the opportunity is not checked then it should get updated with the checked value(minimum of one such product in needed to update the welcome_field__c in opportunity) and when deleted it should check for the remaining items that are there in the opportunityLineItem and if there is at least on of the product with the welcome_field__c, check is there then it should keep the field in the opportunity checed. else it should unchek it.
here is the code:-

if(trigger.isBefore)
  {
  if(trigger.isDelete)
    {
      try{
        OpportunityLineItem [] OpIds = [Select OpportunityId from OpportunityLineItem where Id In : trigger.old];
        List<Id> options= new List<Id>();
        for(OpportunityLineItem i: OpIds)
         {
           options.add(i.OpportunityId);
         }

       OpportunityLineItem [] oppIds = [Select Id, OpportunityId from OpportunityLineItem where OpportunityId In: options];

        List<Id> opt= new List<Id>();
        for(OpportunityLineItem p: oppIds)
         {
           opt.add(p.Id);
         }
       for (OpportunityLineItem c :oppIds)
    {
         OpportunityLineItem [] OLI = [Select PricebookEntry.Product2Id from OpportunityLineItem where Id in : opt];
         Set<Id> Product2Ids = new Set<Id>();
         for(OpportunityLineItem o:OLI)
         {
           Product2Ids.add(o.PricebookEntry.Product2Id);
         }
        
         Product2 [] P2 = [Select Welcome_Program__c from Product2 where Id in :Product2Ids];
          Opportunity opp=new Opportunity(Id=c.OpportunityId);
          for(Product2 pro2: P2)
          {
           if(pro2.Welcome_Program__c==true)
            {
               opp.Welcome_Program__c=true;
               break;
            }
           else
            {
               opp.Welcome_Program__c=false;
            }
          }
          update opp;
         }        
       }

      catch(System.DmlException e)
      {
      System.assert(e.getDmlMessage(0).indexOf('Cannot Update opportunity') > -1);
      }
    }
  }
}

the issue that I am facing is that.. when i put the piece of code in the aftre delete trigger then it does not get the id of the opportunityLineItem and so the code does nothing and no records are fatched. when I put the code in the before delete trigger then it fatched the old data. For example if I have 3 opportunityLineItem and one of them is having the welcome_field__c as checked. then if I delete it it should uncheck the same named field in the opportunity but what it is doing is that is picking up the whole 3 opportunity data items and checks and finds one opportunity data items with welcome_field__c as checked and does nothing and then deltes the item. and in the second run if i delete the one that is not with the welcome_field__c as checked then it updates the welcome_field__c field in the opportunity.
the refection of result is one step late.

How to get the issue resolved.

Thanks in anticipation of your result.

reagards
Pallav