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
EllenHEllenH 

Trigger to update field on Opportunity from Opp Line Items

Hi There,

 

Looking for a little assistance on this trigger.

 

Essentially I am looking to update a hidden field on an opportunity with a line item name.  I am searching for a specific name as the hidden field will then be used in a VF email template.

 

However, I am not exactly sure how to call the product name within the trigger.  I have tried Product2, Name, etc. but I get a compile error message that the "Product2" or "Name" (whatever I seem to choose) is not a column on the entity "OpportunityLineItem".  Do I need to use a name from the pricebook?

 

Thanks to anyone in the community that can provide some assistance.

 

Here is my trigger code:

 

trigger CECProductUpdate on Opportunity (after update, after insert) {

    List<Opportunity> OpptoUpdate= new List<Opportunity>();

    OpportunityLineItem[] OppLI = [Select o.Product2 from
                                      OpportunityLineItem o  where o.Product2 = '[5200] Online Product Course'];
                                      
                                      
    for (OpportunityLineItem CurOppLI : OppLI){

 OpptoUpdate.CEC_Hidden_Product__c = CurOpLI.Product2
    }


   update OpptoUpdate

}


 

imranrazaimranraza

Hi EllenH,

                       You can query Product from Opportunity Line Item like this

                       

trigger CECProductUpdate on Opportunity (after update, after insert) {

    List<Opportunity> OpptoUpdate= new List<Opportunity>();

    OpportunityLineItem[] OppLI = [Select o.PricebookEntry.Product2.Name from
                                      OpportunityLineItem o  where o.PricebookEntry.Product2.Name = '[5200] Online Product Course'];
                                      
                                      
    for (OpportunityLineItem CurOppLI : OppLI){

 OpptoUpdate.CEC_Hidden_Product__c = CurOpLI.PricebookEntry.Product2.Name;
    }


   update OpptoUpdate

}