In this article (4 sections)
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:
- The quote trigger calls the recalculation, which sets a "calculation running" guard.
- The recalculation does its work, then releases the guard.
- Right after, in the same trigger, another component writes to the quote again to sort the lines for display.
- 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
- Count the cycles before optimizing anything. A static counter bumped at the start of the calculation is enough: two lines of code.
- 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.
- 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.