Queueable Apex

Queueable Apex Practice Problems

3 free Salesforce Apex challenges · Full problem statements, best-practice notes, and step-by-step hints · Solve live on ApexArena

This guide walks through 3 Queueable Apex for chainable, stateful asynchronous processing. Each entry below includes the full problem statement, the governor-limit and best-practice constraints it's testing, and a numbered approach to solving it — then you can open the same problem in ApexArena's browser-based Apex editor and get instant pass/fail feedback against real test cases.

On this page
  1. 1. Queueable: Recalculate Opportunity Health Score
  2. 2. Queueable: Chained Jobs — Account Tier then Contact Tag
  3. 3. Queueable: Mass Opportunity Stage Update with Retry Logic
Medium QueueableAsync Apex

1. Queueable: Recalculate Opportunity Health Score

Problem #210 · Salesforce Apex Coding Challenge

Problem Statement

Write OpportunityHealthScoreQueueable that implements Queueable and recalculates a custom Health_Score__c field on a set of Opportunities.

Scoring Rules

  • +30 if Amount ≥ 50,000
  • +20 if CloseDate ≥ today (not in the past)
  • +25 if StageName is 'Proposal/Price Quote' or 'Value Proposition'
  • +50 if StageName is 'Closed Won'

Requirements

  • Implement Queueable.
  • Constructor accepts Set<Id> opportunityIds.
  • execute(QueueableContext context): guard empty input, query Opps, compute score, bulk update.

Best Practices

  • SOQL and DML outside loops.
  • Guard with isEmpty() before DML.
  • Enqueue: System.enqueueJob(new OpportunityHealthScoreQueueable(ids));
Approach
  • 1Class signature: public class OpportunityHealthScoreQueueable implements Queueable
  • 2execute() signature: public void execute(QueueableContext context)
  • 3Guard: if (opportunityIds == null || opportunityIds.isEmpty()) return;
  • 4Enqueue from trigger/class: System.enqueueJob(new OpportunityHealthScoreQueueable(myIds));
Hard QueueableAsync Apex

2. Queueable: Chained Jobs — Account Tier then Contact Tag

Problem #211 · Salesforce Apex Coding Challenge

Problem Statement

Implement the chained Queueable pattern with two classes:

  1. AccountTierQueueable — sets Revenue_Tier__c on Accounts, then chains to ContactTagQueueable.
  2. ContactTagQueueable — sets Contact_Segment__c = 'Processed' on related Contacts.

Requirements

  • Both classes implement Queueable.
  • Both accept Set<Id> accountIds via constructor.
  • AccountTierQueueable.execute(): update Revenue_Tier__c per scoring rules, then call System.enqueueJob(new ContactTagQueueable(accountIds)) guarded by !Test.isRunningTest().
  • ContactTagQueueable.execute(): set Contact_Segment__c = 'Processed' on all Contacts whose AccountId IN :accountIds.

Best Practices

  • Guard chaining with if (!Test.isRunningTest()) — you cannot enqueue from within a Queueable during test execution without wrapping in Test.startTest()/stopTest().
  • All SOQL and DML outside loops.
Approach
  • 1Both classes must implement Queueable and have matching constructors.
  • 2Chain with: if (!Test.isRunningTest()) { System.enqueueJob(new ContactTagQueueable(accountIds)); }
  • 3Test.isRunningTest() guard is essential — you cannot enqueue a job from within a Queueable in test context without startTest/stopTest.
  • 4In tests, call ContactTagQueueable directly to test it independently of the chain.
Hard QueueableAsync Apex

3. Queueable: Mass Opportunity Stage Update with Retry Logic

Problem #225 · Salesforce Apex Coding Challenge

Problem Statement

Write OpportunityStageUpdateQueueable that updates a batch of Opportunities to a target StageName. If the DML fails, re-enqueue itself up to MAX_RETRIES = 3 times.

Requirements

  • Implement Queueable.
  • Constructor: (List<Id> opportunityIds, String targetStage, Integer retryCount).
  • Declare private static final Integer MAX_RETRIES = 3.
  • execute(): guard empty input and blank stage; skip records already at the target stage; wrap DML in try-catch; on failure re-enqueue if retryCount < MAX_RETRIES guarded by !Test.isRunningTest().

Best Practices

  • Skip records that are already at the target stage — avoids unnecessary DML.
  • Log the attempt number in the error message for debugging.
  • Re-enqueue with retryCount + 1 — never modify the field directly before the retry call.
Approach
  • 1Null-safe retryCount in constructor: this.retryCount = retryCount != null ? retryCount : 0;
  • 2Guard blank stage: if (String.isBlank(targetStage)) return;
  • 3Skip already-correct records: if (opp.StageName != targetStage) { ... toUpdate.add(opp); }
  • 4Re-enqueue: System.enqueueJob(new OpportunityStageUpdateQueueable(opportunityIds, targetStage, retryCount + 1));

Practice All 3 Problems Free

Write and run real Apex code right in your browser — instant pass/fail feedback, best-practice linting, and governor limit monitoring. No Salesforce org needed.

Create Free Account → Explore All Problems
More Practice Topics
About ApexArena

ApexArena is a free, browser-based Salesforce Apex coding practice platform covering every major topic tested on the Salesforce Platform Developer I (PD1) and Platform Developer II (PD2) certification exams. All problems run directly in your browser with instant pass/fail feedback, best-practice linting (SOQL in loops, DML in loops, empty catch blocks), and governor limit monitoring — no Salesforce Developer Edition org required.

Related tutorials: Apex Triggers · SOQL · Batch Apex · Interview Q&A · Governor Limits