Queueable Apex
By the end of this lesson, you'll be able to:
- Implement the Queueable interface
- Pass complex data (including sObjects) to a Queueable job
- Chain a second job from within an executing one
Prerequisites: "Future Methods"
Implementing Queueable
public class AccountSyncJob implements Queueable {
private List<Account> accountsToSync;
public AccountSyncJob(List<Account> accountsToSync) {
this.accountsToSync = accountsToSync;
}
public void execute(QueueableContext context) {
for (Account acc : accountsToSync) {
System.debug('Syncing: ' + acc.Name);
// real sync logic here
}
}
}
public class AccountSyncJob implements Queueable {— a real class, unlike@future's bare method.private List<Account> accountsToSync;— a field holding complex data.- (blank line)
public AccountSyncJob(List<Account> accountsToSync) {— Module 10's constructor pattern.this.accountsToSync = accountsToSync;— stores the full list directly, not just anId.}— closes the constructor.- (blank line)
public void execute(QueueableContext context) {— the method the platform calls when the job actually runs.for (Account acc : accountsToSync) {— iterates the stored data.System.debug('Syncing: ' + acc.Name);— placeholder logic.// real sync logic here— where the actual work would go.}— closes theforloop.}— closesexecute.}— closes the class.
Unlike @future, Queueable is a real class — Module 10's constructor pattern lets it accept genuinely complex data, including a full List<Account> directly, not just an Id. execute(QueueableContext context) is the method the platform calls when the job actually runs.
Enqueuing the job
List<Account> accounts = [SELECT Id, Name FROM Account WHERE Industry = 'Technology'];
System.enqueueJob(new AccountSyncJob(accounts));
List<Account> accounts = [SELECT Id, Name FROM Account WHERE Industry = 'Technology'];— a real, potentially large list.System.enqueueJob(new AccountSyncJob(accounts));— the entire list passed directly into the constructor.
System.enqueueJob(...) is the Queueable equivalent of simply calling a @future method — but notice the constructor accepting a real List<Account> directly, exactly the flexibility @future couldn't offer.
Chaining a second job
public class AccountSyncJob implements Queueable {
public void execute(QueueableContext context) {
// ... do the sync work ...
if (! Test.isRunningTest()) {
System.enqueueJob(new SendSyncCompleteNotificationJob());
}
}
}
public class AccountSyncJob implements Queueable {— the same class.public void execute(QueueableContext context) {— the same method.// ... do the sync work ...— the work from the previous section.- (blank line)
if (! Test.isRunningTest()) {— a guard specifically for the test context.System.enqueueJob(new SendSyncCompleteNotificationJob());— chains a second job — something@futurecannot do at all.}— closes theif.}— closesexecute.}— closes the class.
A Queueable job's execute method can enqueue another job — genuinely useful for a multi-step asynchronous process, something @future cannot do at all (a @future method can never call another @future method). Test.isRunningTest() guards against chaining during tests, where only one level of async job is allowed to run.
Exercise
Write a Queueable class OpportunityNotificationJob taking a List<Opportunity> in its constructor and debugging each one's Name in execute().
Show hint
implements Queueable, a constructor, and execute(QueueableContext context).
Queueable Apex Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
Queueable Apex lifts most of @future's restrictions — it accepts complex parameters, can be monitored, and can even chain to a second job — at the cost of a slightly more involved class shape.