If I enqueue an instance of a Queueable class during a before trigger, is it guaranteed that the Queueable class will not run until the after trigger completes?

  • Queueable is asynchronous. It will run whenever Salesforce decides there's enough resources for it. A question back to you: Why not call it in the after context? – Drew Kennedy Feb 21 at 18:34
  • I have a trigger framework in place; to call it in the after context, I need to create the class AccountAfterTriggerHandler. I'm being lazy. But I think I will do it in the after context anyway, because doing it in before context will probably confuse future maintainers. – Bo Laurent Feb 21 at 18:46

Yes, this is guaranteed (though not explicitly documented). The reason why is that, until the transaction fully commits, the Queueable job call may be rolled back, either because of governor limits, an sObject addError call, a Database.rollback command, or an uncaught exception, and probably a few other ways. As such, the system absolutely will not execute the Queueable until the transaction that spawned the trigger context has successfully run its course. This is also true for Scheduled jobs, Batchable jobs, future methods, sending emails, calculating sharing rows, and almost any other type of asynchronous code or action that may exist in the future. The two exceptions to this guarantee are the "DML Immedate" actions available on External Objects, and the EventBus.publish actions that can be called on Platform Events that execute immediately.

(Note to future readers: if future exceptions to this guarantee arise, please let me know.)

| improve this answer | |

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.