Apex – Determine if Transaction is Asynchronous

According to the Execution Governors and Limits the limits for either synchronous or asynchronous transactions can vary, that is why I would like to check whether my current apex transaction is asynchronous. Is there any way in apex to accomplish this?

Best Answer

Yes. The System class offers four methods you can call to determine if you're in Asynchronous Apex and, if so, which type:

  • isBatch()
    Returns true if a batch Apex job invoked the executing code, or false if not. In API version 35.0 and earlier, also returns true if a queueable Apex job invoked the code.
  • isFuture()
    Returns true if the currently executing code is invoked by code contained in a method annotated with future; false otherwise.
  • isQueueable()
    Returns true if a queueable Apex job invoked the executing code. Returns false if not, including if a batch Apex job or a future method invoked the code.
  • isScheduled()
    Returns true if the currently executing code is invoked by a scheduled Apex job; false otherwise.