Apex Class

Chain Queueable Apex in Salesforce

Chain Queueable Apex refers to the practice of enqueuing multiple queueable jobs sequentially. This chaining mechanism allows you to execute a series of apex code one after another, maintaining a clear order and dependency between them.

This is particularly useful for scenarios where you need to perform multiple actions in sequence, such as processing records or interacting with external systems.

Illustrative representation about Chain Queuable Apex Methods in Salesforce

Use Cases and Examples

Let's say you have a custom object called CustomObject__c, and you want to create new records and then update them sequentially.

public class RecordCreator implements Queueable {
        public void execute(QueueableContext context) {
            // Step 1: Create a new record
            CustomObject__c newRecord = new CustomObject__c(Name = 'New Record');
            insert newRecord;
            // Chain to Step 2 (RecordUpdater)
            System.enqueueJob(new RecordUpdater(newRecord.Id));
public class RecordUpdater implements Queueable {
        private Id recordId;
        public RecordUpdater(Id id) {
            recordId = id;
        public void execute(QueueableContext context) {
            // Step 2: Update the created record
            CustomObject__c recordToUpdate = [SELECT Id, Name FROM CustomObject__c WHERE Id = :recordId LIMIT 1];
            if (recordToUpdate != null) {
                recordToUpdate.Name = 'Updated Record';
                update recordToUpdate;
                System.debug('Record updated: ' + recordToUpdate.Name);

Keep in mind that this example focuses on chaining jobs for creating and updating records. In real-world scenarios, you would typically have more complex logic and error handling.

Enqueue the initial job using System.enqueueJob() to start creating and updating the records

RecordCreator initialJob = new RecordCreator();

Calling an External System

Let's consider an example where you want to integrate Salesforce with a payment gateway. Here's how you might structure your queueable chain:

  1. The first queueable class initiates the payment request and stores the necessary data in Salesforce.

    public class PaymentRequestQueueable implements Queueable {
            public void execute(QueueableContext context) {
                // Initiate payment request and store data
                // Enqueue the next job for monitoring payment status
                System.enqueueJob(new PaymentStatusMonitorQueueable());
  2. The second job monitors the payment status with the external system and updates the Salesforce record accordingly.

    public class PaymentStatusMonitorQueueable implements Queueable {
            public void execute(QueueableContext context) {
                // Monitor payment status and update Salesforce record

Governor Limits of Queueable Apex

There are some Queueable Limits Salesforce to consider before start coding your class:

Best Practices

To make the most of Chain Queueable Apex, follow these best practices: