Question

I use a calculation on Opportunity Products (i.e.line items) that uses the term of the opp which is stored in the Opp itself (ex: OLI.RecurringAmt = OPP.Term * OLI.UNITPRICE * OLI.QUANTITY)

If the valdue of the Term (integer) in the Opp changes, the math in each OLI needs to be updated manually. I'm proposing to use a trigger on the Opp to do this update any time the Term changes.

Here's the question: since the AMOUNT field in the Opp is auto-calculated by SFDC as the SUM of the UNITPRICE of each related line item times the QUANTITY, for every line item, will I run into recursion problems by updating the OLI records from an Opp trigger that will, by default, try to update the AMOUNT field of the same Opp that is firing the trigger?

If this would not cause issues, should I trigger on "after update" or "before update"? And does SFDC recalculate the updated AMOUNT value before or after trigger completes?

Thanks for any insight on this.

Was it helpful?

Solution

Trigger recursion could occur in this case if you are changing the OpportunityLineItem (OLI) UnitPrice or Quantity.

The standard practice is to use a static member or property within the transaction to detect if the operation has already been performed.

See How to avoid Recursive trigger. This is a very basic example. You could extend that to maintain a Set of Opportunity Ids that have already been processed.

I'd recommend using an after update trigger.

Incidentally, salesforce.stackexchange.com/ is a great place to ask Salesforce specific questions.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top