Advanced 35 min read

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
        }
    }
}
  1. public class AccountSyncJob implements Queueable { — a real class, unlike @future's bare method.
  2. private List<Account> accountsToSync; — a field holding complex data.
  3. (blank line)
  4. public AccountSyncJob(List<Account> accountsToSync) { — Module 10's constructor pattern.
  5. this.accountsToSync = accountsToSync; — stores the full list directly, not just an Id.
  6. } — closes the constructor.
  7. (blank line)
  8. public void execute(QueueableContext context) { — the method the platform calls when the job actually runs.
  9. for (Account acc : accountsToSync) { — iterates the stored data.
  10. System.debug('Syncing: ' + acc.Name); — placeholder logic.
  11. // real sync logic here — where the actual work would go.
  12. } — closes the for loop.
  13. } — closes execute.
  14. } — 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));
  1. List<Account> accounts = [SELECT Id, Name FROM Account WHERE Industry = 'Technology']; — a real, potentially large list.
  2. 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());
        }
    }
}
  1. public class AccountSyncJob implements Queueable { — the same class.
  2. public void execute(QueueableContext context) { — the same method.
  3. // ... do the sync work ... — the work from the previous section.
  4. (blank line)
  5. if (! Test.isRunningTest()) { — a guard specifically for the test context.
  6. System.enqueueJob(new SendSyncCompleteNotificationJob()); — chains a second job — something @future cannot do at all.
  7. } — closes the if.
  8. } — closes execute.
  9. } — 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).

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

Queueable Apex Quiz

1. What can a Queueable job do that a @future method cannot?

Log in to submit the quiz and save your score.

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.