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
Wenda McMahanWenda McMahan 

Opp Line Item Trigger not always updating

Hi, another newbie programmer here with a question about the following trigger. The intention is to get the Product Abbreviation of only the Product from the highest priced Opportunity Line Item into the Prim_Prod_Abbrev__c field on Opportunity.

The trigger works to get the Prim_Prod_Abbrev__c when there is one Opp Line Item, but it is unreliable about getting the highest priced OLI when there is more than one. Any thoughts/help would be very appreciated. Thank you.

trigger getProduct on Opportunity (before insert, before update) {
   public list<opportunity> Opps = trigger.new;
   public list<opportunityLineItem> Prods = new list<opportunityLineItem>([SELECT Id, TotalPrice, PricebookEntry.Product2Id, Product2.name,                                                                                                                 PricebookEntry.Product2.Abbreviation__c, opportunityId 
                                                                                                              FROM opportunityLineItem WHERE opportunityId =: trigger.new[0].id                                                                                                               ORDER BY TotalPrice LIMIT 1]);
  string productvar='';
  for(opportunityLineItem p : Prods) {
       productvar = p.PricebookEntry.Product2.Abbreviation__c;
  }

  for(Opportunity opp : trigger.new) {
     opp.Prim_Prod_Abbrev__c = productvar;
  }
}