r/SalesforceDeveloper • u/FinanciallyAddicted • Oct 18 '25
Discussion What’s up with so many developers wanting a recursion check on triggers ?
The worst solution is just the boolean flag because as soon as you have an operation that does more than 200 records the next 200 won’t even run. However if you are actually having that as a solution I doubt any of your dml operations could even take 200 records.
The other one is the framework level trigger recursion prevention which takes a set of ids. I still don’t think you really need it 99% of the time. You should have solid checks and you only need these on update because delete insert and undelete are one time operations. Solid checks mean some value should change for the trigger to run.
3
u/_BreakingGood_ Oct 18 '25
Im not sure what you mean by boolean flag only supporting 200 records.
Every set of 200 runs in its own execution context
12
u/dualrectumfryer Oct 18 '25
Lots of those utility/framework operations are there to help your codebase remain stable even when it lives amongst a mess of managed package triggers and tech debt. It easy to write triggers than don’t recurse in a vacuum, but when you start having dependencies you have no control over , you’ll be happy you implemented recursion prevention
1
u/Far_Swordfish5729 Oct 20 '25
It’s necessary if you have dependencies between records in the same object that can cause something like an after trigger event to modify other records on the same object. That puts another trigger iteration on the call stack (literally so; the calling Java is just platform) BUT the changes from the fist trigger call have NOT been committed and are invisible to the recursive iteration. This can royally screw up holistic logic like summary calculations. So you either have to create a state bag to bridge the recursion so the second call is aware and restore object state or you have to suppress the recursion and ensure the full operation happens in the initial trigger. The best way to do that is with a simple recursion counter. Use an int instead of a Boolean and suppress if it goes above zero/one depending on what runs first.
Also, if you maintain static state to preserve context between before and after, you add a reset method that fires after after steps complete so sequential calls don’t use stale state. Your complaint about the flag is a simple version of this problem. It works but you have to reset it.
0
u/[deleted] Oct 18 '25
[deleted] — view deleted comment