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
Smurfet15Smurfet15 

trigger to update opportunity checkbox when adding/update opportunity line item

Hi there,

My requirement is to update(check) opportunity checkbox everytime there is a product added that has product category = "Frame".
Can you please help me?
Nothing is displaying on this : system.debug('Display'+ Olipc);

Below is my code:
--------------------------------------
trigger OpportunityUpdateFromOppProduct on OpportunityLineItem (after insert, after update) {
Map<Id,Opportunity> oppIds = new Map<Id,Opportunity>();
String OppID;
String Olipc;    
   List<Opportunity> opprt = new List<Opportunity>();
   for(OpportunityLineItem Oli: trigger.new){
      
     OppID = Oli.OpportunityID;
     Olipc = Oli.PricebookEntry.Product2.product_Category__c  ;
     system.debug('Display'+ Olipc);
    if(Oli.PricebookEntry.Product2.product_Category__c == 'Frame' )
     {
       Opportunity oppy = new Opportunity(Id = OppID);
       oppy.Frame__c = true ;
       opprt.add(oppy);
         
     }
   }

}
 
Best Answer chosen by Smurfet15
Neetu_BansalNeetu_Bansal
Hi,

You didn't get related fields in trigger.new, you need to query it:

trigger OpportunityUpdateFromOppProduct on OpportunityLineItem (after insert, after update) {
List<OpportunityLineItem> oppLineItems = [ Select Id, OpportunityID, PricebookEntry.Product2.product_Category__c from OpportunityLineItem where Id IN: trigger.new ];
Map<Id,Opportunity> oppIds = new Map<Id,Opportunity>();
String OppID;
String Olipc;    
   List<Opportunity> opprt = new List<Opportunity>();
   for(OpportunityLineItem Oli: oppLineItems ){
      
     OppID = Oli.OpportunityID;
     Olipc = Oli.PricebookEntry.Product2.product_Category__c  ;
     system.debug('Display'+ Olipc);
    if(Oli.PricebookEntry.Product2.product_Category__c == 'Frame' )
     {
       Opportunity oppy = new Opportunity(Id = OppID);
       oppy.Frame__c = true ;
       opprt.add(oppy);
         
     }
   }

}

Thanks,
Neetu

All Answers

Neetu_BansalNeetu_Bansal
Hi,

You didn't get related fields in trigger.new, you need to query it:

trigger OpportunityUpdateFromOppProduct on OpportunityLineItem (after insert, after update) {
List<OpportunityLineItem> oppLineItems = [ Select Id, OpportunityID, PricebookEntry.Product2.product_Category__c from OpportunityLineItem where Id IN: trigger.new ];
Map<Id,Opportunity> oppIds = new Map<Id,Opportunity>();
String OppID;
String Olipc;    
   List<Opportunity> opprt = new List<Opportunity>();
   for(OpportunityLineItem Oli: oppLineItems ){
      
     OppID = Oli.OpportunityID;
     Olipc = Oli.PricebookEntry.Product2.product_Category__c  ;
     system.debug('Display'+ Olipc);
    if(Oli.PricebookEntry.Product2.product_Category__c == 'Frame' )
     {
       Opportunity oppy = new Opportunity(Id = OppID);
       oppy.Frame__c = true ;
       opprt.add(oppy);
         
     }
   }

}

Thanks,
Neetu
This was selected as the best answer
Smurfet15Smurfet15
Hi Neetu... It is now working but it is not saving.

The debug it passed and successful on this:
Opportunity oppy = new Opportunity(Id = OppID);
oppy.Frame__c = true ;

IS there soemthing wrong in this: opprt.add(oppy);
 
RazuRazu
Do DML operation after the for loop for opportunities..