ApexSensei

Drills › Work in the background

Carry Account work in a Queueable

Work in the background · Intermediate · 18 min · Runs in your Salesforce Developer org · Apex Path

Scenario. A review request should return fast while a background job labels the chosen Accounts.

Carry Account Ids and a label into a Queueable job, then update every target Account with one DML statement, even for 200 records.

What you will learn

A Queueable class runs execute in a background job. Unlike @future, its fields can hold lists, sets and other objects, and System.enqueueJob gives back a job Id you can check.

Id jobId = System.enqueueJob(new ReviewJob(accountIds));

Gotcha. The job is saved and rebuilt later, so query fresh records inside execute. Update the whole list once: 200 updates in a loop pass the 150-DML limit.

The task

Complete execute so the job sets every target Account's Description to the label it was built with.

Examples:

  • two Account Ids + Reviewed → both Descriptions become Reviewed
  • one target and one other Account → only the target changes
  • 200 Account Ids → all 200 change, with one update

Requirements:

  1. In execute, query only the Accounts whose Ids are in accountIds.
  2. Set each Description to reviewLabel.
  3. Update the whole list once, after the loop.
  4. When accountIds is empty, do nothing. The job must still finish without errors.

Starter code

public class AS_BDD2EE486035_QueueableAccountJob implements Queueable {
    private Set<Id> accountIds;
    private String reviewLabel;

    public AS_BDD2EE486035_QueueableAccountJob(Set<Id> accountIds, String reviewLabel) {
        this.accountIds = accountIds;
        this.reviewLabel = reviewLabel;
    }

    public void execute(QueueableContext context) {
    }
}

When it passes

Nothing changes until the job runs. Then every target Account gets the label, other Accounts stay the same, 200 Accounts finish in one update, and an empty job completes cleanly.

Try this drill free

You write the Apex yourself. ApexSensei runs it and tells you what passed and what did not.

‹ Complete a Contact review in the future · Build a Batch lifecycle ›