We have code using the Database.insert/update(records, false) syntax. I know how to handle the errors after the fact, so that is not the issue, but when using static recursion flags, the first attempt which has at least one failure sets the recursion flag to true, and the second retry attempt with only the successes is causing any triggers checking that recursion flag to not run, and so technically no logic is being performed if there is at least one error. Is there a reliable way to detect when a retry is occuring so that we can reset the recursion flag(s)? I don't have specific code for this, its a more generic question.


The main flag that can tell you a retry has happened, is that a new ID you stashed in a static variable is no longer valid. If your trigger code creates any new records you could check those IDs against the DB. I wish Salesforce gave us a better way to do this.

One crafty way I thought of in another thread was to enqueue a disposable Queueable class, for no purpose other than to get an ID returned from enqueueJob. Stash that in a static. If you see an ID in your static and are wondering if a rollback has happened, you can query AsyncApexJob to check. If the Job ID ain't in the table, the enqueue got rolled back. However enqueuing and rolling back a job still counts against your Queueables limit, and you still waste a SOQL query checking.

One more thought to add though: If you are simply looking to implement transactional logic that should run on successful records, regardless of whether any partial rollbacks happened... another tool available for you now is Change Event triggers. These will run asynchronously, post commit, only on records that actually completed a change.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.