I have an apex code which is called from an after trigger. The apex code calls a future method and sometimes the future method runs after the execution of the current thread and sometimes does not.

I want to force the future method to run only when the context of its call ends. So the idea is: when the thread that started the future methods ends, the future methods is allowed to run.

Any ideas? Thanks.


It is not possible to tell when the future method will run. If you want it to run after your current code is done executing, you have 2 options:

  1. Call your future method as the very last step of your execution
  2. Create a scheduleable class and schedule it in a couple of minutes, that way you will be sure that your code has enough time to process
  • 5
    Since @future methods are committed to an execution queue at the end of the current execution context (together with all other changes), is not that a case that future method will be always executed after first context is done completely? – kurunve Feb 7 '20 at 13:20

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.