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
Jay SanbornJay Sanborn 

Trigger to update Opportunity after a OpportunityLineItem added - Please help

Folks

 

I am try to update a checkbox field on an Opportunity after I add or update an Opportunity Line Item.  Here is the code:

 

trigger CheckConfiguration on OpportunityLineItem (after insert, after update) {

      String oppID;      
      String pID;
      
      for(OpportunityLineItem oli : Trigger.new){
      
         oppID = oli.OpportunityID; 
         pID = 'Configuration';     

         if(oli.PricebookEntry.Product2.Name == pID ) {

            List<Opportunity> opps = [select Config_Required__c from Opportunity where Id = : oppID]; 
            for (Opportunity oppy: opps) {
   
                oppy.Config_Required__c = true ;

            update opps;    }
          }

      }
}

Please help and show me what I am doing incorrectly.

 

Thank You

 

Jay

 

 

Pradeep_NavatarPradeep_Navatar

Try out the given  code :

 

trigger CheckConfiguration on OpportunityLineItem (after insert, after update) 

{

      String oppID;      
      List<Opportunity> opprt= new List<Opportunity>();
      for(OpportunityLineItem oli : Trigger.new)

      {
         oppID = oli.OpportunityID; 
         
         if(oli.PricebookEntry.Product2.Name == 'Configuration' ) 

         {
            List<Opportunity> opps = [select Config_Required__c from Opportunity where Id = : oppID]; 
            for (Opportunity oppy: opps) 

            {
                 oppy.Config_Required__c = true ;
                 opprt.add(oppy);    

             }
         }

      }

 update opprt;
}

 

Hope this helps.

b-Forceb-Force

One more modification,

 

trigger CheckConfiguration on OpportunityLineItem (after insert, after update) 

{String oppID;      
      List<Opportunity> opprt= new List<Opportunity>();
      for(OpportunityLineItem oli : Trigger.new)
{ oppID = oli.OpportunityID; if(oli.PricebookEntry.Product2.Name == 'Configuration' ) {
Opportunity oppy =new Opportunity(Id = : oppID);  oppy.Config_Required__c = true ; opprt.add(oppy);
} } } Database.SaveResult[] sr=Database.update(opprt); //Check this sr , what is the result

}

 

 

 Thanks,

Bala