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
Javier MaldonadoJavier Maldonado 

Update a field based on the largest value amount, from a related list

Hello everyone,
I'm facing a challenge now, and I just want to obtain your best advice because this is a really interesting project...
I have a custom related list called "Opportunity Products" in to my standard object Opportunity, this related list contain every item used in this opportunity, including price, quantity and also the amount... the challenge begin when I'm looking  automate the process, to updated a field called "Product Description" using the "Product Description" from my "Opportunity Products" related list, and using just the 3 largest amount records, shown in this related list... so the first step is, to identify the 3 largest amount, to then update this field...
I could identify the first large amount using a RollUp field (easy), but I'm trying to identify, the 2 seconds largest amount, and I found this really tricky.. so any light will be really welcomed... Thanks in advance...

User-added image
Shikha AgashiShikha Agashi
I would suggest you to write your logic before insert and before Update trigger on Opportunity Product to update Opportunity. Using trigger, you would be able to populate Product Description as well as Amount 1 & Amount2 field. 
LakshmanLakshman
You can achieve this with following steps:
  • Create a trigger on OpportunityLineItem with events after insert, after update, after delete and after undelete(if you want to cover undelete scenario)
  • Collect the Opportunity Products which have change in Amount change
  • Query for Top 3 opportunity products for the opportunity having amount change in Opportunity Product, query should looke like:
Opportunity[] lstOpp = [Select Id, Product_Description__c, (Select Description, TotalPrice from OpportunityLineItems Order By TotalPrice Limit 3) from Opportunity where Id =: setOppIds];
  • Iterate over lstOpp and collect all line description (split it by ' ' and take first two words) in a string in comma separated fashion.
  • Update the lstOpp.

Hope these steps are clear, let us know if it helps.