Queueable API
Apex classes QueueableBuilder.cls
, QueueableManager.cls
, and QueueableJob.cls
.
Common Queueable example:
QueueableJob job = new MyQueueableJob();
Async.Result result = Async.queueable(job)
.priority(5)
.delay(2)
.continueOnJobExecuteFail()
.enqueue();
result.customJobId; // MyQueueableJob Custom Job Id
Methods
The following are methods for using Async with Queueable jobs:
asyncOptions(AsyncOptions asyncOptions)
delay(Integer delay)
priority(Integer priority)
continueOnJobEnqueueFail()
continueOnJobExecuteFail()
rollbackOnJobExecuteFail()
chain()
chain(QueueableJob job)
asSchedulable()
INIT
queueable
Constructs a new QueueableBuilder instance with the specified queueable job.
Signature
Async queueable(QueueableJob job);
Example
Async.queueable(new MyQueueableJob());
Build
asyncOptions
Sets AsyncOptions for the queueable job. Cannot be used with delay().
Signature
QueueableBuilder asyncOptions(AsyncOptions asyncOptions);
Example
AsyncOptions options = new AsyncOptions();
Async.queueable(new MyQueueableJob())
.asyncOptions(options);
delay
Sets a delay in minutes before the job executes. Cannot be used with asyncOptions().
Signature
QueueableBuilder delay(Integer delay);
Example
Async.queueable(new MyQueueableJob())
.delay(5); // Execute in 5 minutes
priority
Sets the priority for the queueable job. Lower numbers = higher priority.
Signature
QueueableBuilder priority(Integer priority);
Example
Async.queueable(new MyQueueableJob())
.priority(1); // High priority
continueOnJobEnqueueFail
Allows the job chain to continue even if this job fails to enqueue.
Signature
QueueableBuilder continueOnJobEnqueueFail();
Example
Async.queueable(new MyQueueableJob())
.continueOnJobEnqueueFail();
continueOnJobExecuteFail
Allows the job chain to continue even if this job fails during execution.
Signature
QueueableBuilder continueOnJobExecuteFail();
Example
Async.queueable(new MyQueueableJob())
.continueOnJobExecuteFail();
rollbackOnJobExecuteFail
Rolls back any DML operations if this job fails during execution.
Signature
QueueableBuilder rollbackOnJobExecuteFail();
Example
Async.queueable(new MyQueueableJob())
.rollbackOnJobExecuteFail();
chain
Adds the Queueable Job to the chain without enqueing it. All jobs in chain will be enqueued once enqueue()
method is invoked.
Signature
QueueableBuilder chain();
Example
Async.Result result = Async.queueable(new MyQueueableJob())
.chain();
result.customJobId; // MyQueueableJob unique Custom Job Id
chain next job
Adds the Queueable Job to the chain after previous job. All jobs in chain will be enqueued once enqueue()
method is invoked.
Signature
QueueableBuilder chain(QueueableJob job);
Example
Async.Result result = Async.queueable(new MyQueueableJob())
.chain(new MyOtherQueueableJob());
result.customJobId; // MyOtherQueueableJob Unique Custom Job Id.
// To obtain MyQueueableJob Unique Custom Job Id use chain() method separately
asSchedulable
Converts the queueable builder to a schedulable builder for cron-based scheduling. For scheduling, look into the SchedulableBuilder API.
Signature
SchedulableBuilder asSchedulable();
Example
Async.queueable(new MyQueueableJob())
.asSchedulable();
Execute
enqueue
Enqueues the queueable job with the configured options. Returns an Async.Result.
Signature
Async.Result enqueue();
Example
Async.Result result = Async.queueable(new MyQueueableJob())
.priority(5)
.enqueue();
result.salesforceJobId; // MyQueueableJob Saleforce Job Id of either Queuable Job or Initial Scheduled Job, if MyQueueableJob was the enqueued one in chain, otherwise empty
result.customJobId; // MyQueueableJob Unique Custom Job Id.
result.asyncType; // Async.AsyncType.QUEUEABLE
result.isChained; // If job was chained
result.queueableChainState; // queueable chain state
result.queueableChainState.jobs; // All jobs that were chained or in chain, including finalizers and processed jobs
result.queueableChainState.nextSalesforceJobId; // Salesforce Job Id that will run next from chain
result.queueableChainState.nextCustomJobId; // Custom Job Id that will run next from chain
result.queueableChainState.enqueueType; // QueueableManager.EnqueueType - determine how the chain was enqueued, either added to currently running chain (EXISTING_CHAIN), enqueued as separate chain (NEW_CHAIN), or scheduled by initial job (INITIAL_SCHEDULED_BATCH_JOB)
attachFinalizer
Attaches a finalizer job to run after the current job completes. Can only be called within a QueueableChain context.
Signature
Async.Result attachFinalizer();
Example
// Inside a QueueableJob's work() method
Async.Result result = Async.queueable(new MyFinalizerJob())
.attachFinalizer();
result.customJobId; // MyQueueableJob unique Custom Job Id
Context
getQueueableJobContext
Gets the current queueable job context, providing access to job information and Salesforce QueueableContext.
Signature
Async.QueueableJobContext getQueueableJobContext();
Example
Async.QueueableJobContext ctx = Async.getQueueableJobContext();
QueueableJob currentJob = ctx.currentJob;
QueueableContext sfContext = ctx.queueableCtx;
getQueueableChainBatchId
Gets the ID of the QueueableChain batch job if the current execution is part of a batch-based chain.
Signature
Id getQueueableChainBatchId();
Example
Id batchId = Async.getQueueableChainBatchId(); // Initial Scheduled Batch Job
getCurrentQueueableChainState
Gets details about the current Queueable Chain.
Signature
QueueableChainState getCurrentQueueableChainState();
Example
QueueableChainState currentChain = Async.getCurrentQueueableChainState();
currentChain.jobs; // All jobs in chain including processed ones and finalizers
currentChain.nextSalesforceJobId; // Salesforce Job Id that will run next from chain, can be empty if chain not enqueued or from Chain context
currentChain.nextCustomJobId; // Custom Job Id that will run next from chain
currentChain.enqueueType; // empty, value set during enqueue() method