Choosing Queueable, Batch, Scheduled or future Apex
The advanced async decisions PD II tests: why Queueable replaces future methods, bulk-safe enqueueing from triggers, Database.Stateful and callouts in Batch Apex, callouts from Scheduled Apex, transaction finalizers, and async as the fix for mixed DML and slow saves.
Analyze a set of requirements and determine the benefits of using asynchronous Apex coding.
~8 min read · 9 practice questions ·
What the exam expects
- Name what asynchronous Apex gives a requirement (a save that no longer waits, higher limits, scalability) and what it does not (a guaranteed start time).
- Replace a future method with Queueable Apex when the job needs sObjects, a job ID, chaining or callouts, and spot code that will not compile.
- Read a trigger or batch class and find the async bug: too many
System.enqueueJobcalls, state lost betweenexecutecalls, a callout from the wrong context. - Design failure recovery for a Queueable job with a transaction finalizer.
- Fix a mixed DML error by moving setup-object DML into an asynchronous job.
The per-transaction limits and the basic future/Queueable/Batch/Scheduled overview are covered in the Platform Developer guide's Governor limits and asynchronous Apex; this unit teaches the layer above it.
Key terms: Queueable (a class implementing Queueable, added to the queue with System.enqueueJob), chaining (a running job enqueuing the next one), Database.Stateful (keeps instance variables across batch transactions), Database.AllowsCallouts (marker interface that permits callouts in Queueable and Batch Apex), transaction finalizer (a System.Finalizer attached to a Queueable job that runs after it succeeds or fails).
Key facts
- Detail
- Static, void; parameters are primitives or arrays/collections of primitives
- Detail
- Job ID, non-primitive member variables, chaining
- Detail
- One child job per executing job; depth 5 in Developer and Trial orgs
- Detail
- 50 synchronous, 1 asynchronous, 50 for Bulk API
- Detail
- Queued jobs are not processed
- Detail
- Up to 50 million records; scope default 200, maximum 2,000
- Detail
- Keeps instance variables; statics still reset
- Detail
- Up to 5 queued or active jobs; flex queue holds 100
- Detail
- 100 jobs at once; no synchronous callouts
- Detail
- Re-enqueue up to 5 consecutive failures; one async job per finalizer
- Detail
- 250,000 or 200 × user licenses, whichever is greater
Sources for this section (6)
- Future Methodsdeveloper.salesforce.com (opens in a new tab)
- Queueable Apexdeveloper.salesforce.com (opens in a new tab)
- Execution Governors and Limitsdeveloper.salesforce.com (opens in a new tab)
- Use Batch Apexdeveloper.salesforce.com (opens in a new tab)
- Apex Schedulerdeveloper.salesforce.com (opens in a new tab)
- Transaction Finalizersdeveloper.salesforce.com (opens in a new tab)
- Why does a future method reject an sObject parameter?
- When does a batch class need `Database.Stateful`?
- When must a scheduled job hand its callout to another class?
- What happens to a Queueable enqueued by a transaction that later rolls back?
The questions are free to read. Answers with their sources and self-grading are included with the pack.
Keep reading with the Platform Developer II pack
- Core concepts
- Exam traps
- Worked scenario
The pack includes:
- All 28 lessons of the Platform Developer II guide, with reading progress
- Interactive decision walks and step-by-step sequence steppers
- Inline practice questions and one-click topic practice (9 questions for this lesson)
- An official source cited for every claim
- An AI tutor that answers from the guide
- Unlimited timed practice exams