Introduction
Queueable Apex is a powerful asynchronous execution framework in Salesforce that allows developers to perform complex and long-running operations. It builds upon the functionality of @future
methods and provides additional features like chaining jobs and accessing non-primitive data types.
Key Features of Queueable Apex
- Enables job chaining.
- Allows passing of non-primitive data types.
- Provides better monitoring and debugging capabilities.
Syntax
Queueable Apex classes must implement the Queueable
interface.
public class QueueableExample implements Queueable {
public void execute(QueueableContext context) {
// Business logic here
List<Account> accounts = [SELECT Id, Name FROM Account WHERE Industry = 'Technology'];
for (Account acc : accounts) {
acc.Name = acc.Name + ' - Updated';
}
update accounts;
}
}
How to Enqueue a Job
QueueableExample job = new QueueableExample();
Id JobId = System.enqueueJob(job);
Chaining Jobs
public class ChainingQueueableExample implements Queueable {
public void execute(QueueableContext context) {
// First job logic
System.enqueueJob(new AnotherQueueableJob());
}
}
Monitoring Jobs
You can monitor Queueable jobs from the Apex Jobs page in Salesforce Setup.
Limitation for Asynchronous
From / What | Call @future method | System.enqueueJob() | Database.executeBatch() | System.schedule() |
---|---|---|---|---|
Anonymous Apex | 50 | 50 | 100 | 100 |
@AuraEnabled method | 50 | 50 | 100 | 100 |
@future method | 0 | 1 | 0 | 100 |
Queueable execute() | 50 | 1 | 100 | 100 |
Schedulable execute() | 50 | 50 | 100 | 100 |
Batch start() | 0 | 1 | 0 | 100 |
Batch execute() | 0 | 1 | 0 | 100 |
Batch finish() | 0 | 1 | 0 | 100 |
Platform Event trigger | 50 | 50 | 100 | 100 |