Revenue Cloud project notebook, written by two Meir IT consultants who implement it.

What is Revenue Cloud?

Quote and Order·Admin

Slow Quotes in Revenue Cloud: Four Recalculations per Save

Large quotes were hitting the Apex CPU time limit. Before optimizing the calculation, the first thing to see was that it ran four times on every save.

Nathan Altmann · Revenue Cloud consultant at Meir IT

Published 3 min read

In 3 linesfor leadership

  1. Large quotes, up to 300 lines, became very slow to save and then failed outright.
  2. The real cause was the pricing calculation running four times for every single save.
  3. Count the runs and remove the extra ones before spending effort on optimizing the code.
In this article (4 sections)
  1. The limit that breaks: CPU time
  2. Four recalculations for one save
  3. Remove the extra cycles, then optimize
  4. In practice

Stage: saving the quote, and the recalculation that runs on every save. Most of the client's quotes had a few dozen lines, but some had 300, because of offers made up of many products (bundles). On those big quotes, saving got very slow, then failed. The ticket just said "Quotes are slow."

The limit that breaks: CPU time

On a Revenue Cloud quote, you'd expect the query count to blow up. The measurements showed something else: queries, DML and rows retrieved all stayed under their limits. The one that broke was CPU time, capped at 10,000 ms per transaction (the transaction is everything that runs for one save, triggers and flows included). The breaking point fell between 165 and 310 quote lines depending on the mix, and the problem quotes had 300.

In the code, one function stood out quickly. For every line, it walked through all the quote's lines again to find that line's bundle subtree. On 20 lines you'd never notice. On 300, that's 90,000 loop iterations per pass. The next question was how many passes ran per save.

Four recalculations for one save

To find out, the project team instrumented the code. The main calculation method showed eight entries where one was expected: a single save kicked off four full recalculation cycles. On one big quote, the 92 cycles measured broke down exactly into 4 saves times 23 lines.

The mechanism is a classic with after-save triggers:

  1. The quote trigger calls the recalculation, which sets a "calculation running" guard.
  2. The recalculation does its work, then releases the guard.
  3. Right after, in the same trigger, another component writes to the quote again to sort the lines for display.
  4. That write fires the trigger again. Since the guard is already released, the recalculation runs again.

So the guard was released at the end of the calculation, when it should have been released at the end of the transaction. An after-save flow on the quote added its own rounds: it updated its own record twice, and each update fired the quote triggers again.

The time a save takes is the cost of one pass times the number of passes. The code shows the first number; only measuring shows the second. And nobody counts the passes, because each piece of automation, Apex or flow, was written on its own, without seeing what the others fire. Optimizing the loop without removing the extra cycles meant speeding up a calculation that ran four times.

Remove the extra cycles, then optimize

  1. Count the cycles before optimizing anything. A static counter bumped at the start of the calculation is enough: two lines of code.
  2. Remove the unneeded cycles: release the guard at the end of the transaction, stop flows from rewriting their own record, write only when the value changes.
  3. Only then, go after the algorithm.

The first two steps take a few lines; the third is a project. It's tempting to start there because you can see it in the code, while the extra cycles only show up when you measure.

In practice

If I had to do it again, I'd count how many times the calculation runs first, and only then look for the slow code.

  • On a slow quote, read the CPU time in the debug log before the query count.
  • Put a static counter at the start of the calculation and read it after a single save. Anything above 1 means extra cycles.
  • Release a reentry guard at the end of the transaction, not the end of the calculation.
  • List the after-save flows on the quote that update the quote itself, and make them write only when the value changes.
  • Time things with detailed logging turned off: at FINEST level, it added 2 to 4 seconds to the observed time.
A technical question about this article?

A question about this article? I reply within 48 hours.

Something unclear, a mistake, or different behavior in your Salesforce org? Write to me.

Prefer email? Write to nathan@meir-it.com

Get notified of new articles

KeywordsQuoteBundle

Type at least two letters.