seamless force

Salesforce Architecture, Development, AI, and Engineering Without the Noise

Queueable Apex Classes

Salesforce provides multiple ways to execute asynchronous processes, and Queueable Apex is one of the most powerful and flexible options. While future methods provide basic asynchronous execution, Queueable Apex offers greater control, support for complex logic, and the ability to chain jobs.

In this blog post, we’ll explore how to use Queueable Apex, its advantages over future methods, and real-world examples of when to use it.


What is Queueable Apex?

Queueable Apex is an asynchronous processing framework that allows developers to execute complex logic in the background. It provides enhanced functionality over future methods, including:

  • Support for complex data types (such as sObjects and custom classes).
  • Chaining multiple jobs for sequential execution.
  • Job monitoring via the Apex Jobs UI.

Syntax:

public class MyQueueableJob implements Queueable {
    public void execute(QueueableContext context) {
        // Async logic here
    }
}

To enqueue a job, use:

System.enqueueJob(new MyQueueableJob());

Key Differences: Queueable Apex vs. Future Methods

FeatureFuture MethodsQueueable Apex
Return Values❌ No✅ Yes (via chaining)
Supports Callouts✅ Yes✅ Yes
Supports Chaining❌ No✅ Yes
Allows Complex Logic❌ Limited✅ More Control
Supports SObjects as Parameters❌ No✅ Yes
Debugging & Monitoring❌ No✅ Yes (via Apex Jobs)

Queueable Apex is a more advanced and scalable approach, making it the preferred choice over future methods for most asynchronous operations.


How to Use Queueable Apex

Example 1: Updating a Large Number of Records

If you need to process a large number of records asynchronously, Queueable Apex allows more flexibility:

public class UpdateContactsQueueable implements Queueable {
    private List<Id> contactIds;
    
    public UpdateContactsQueueable(List<Id> contactIds) {
        this.contactIds = contactIds;
    }
    
    public void execute(QueueableContext context) {
        List<Contact> contactsToUpdate = [SELECT Id, LastName FROM Contact WHERE Id IN :contactIds];
        for (Contact c : contactsToUpdate) {
            c.LastName = 'Updated';
        }
        update contactsToUpdate;
    }
}

Usage:

List<Id> contactIds = new List<Id>{'003XXXXXXXXXXXX', '003YYYYYYYYYYYY'};
System.enqueueJob(new UpdateContactsQueueable(contactIds));

Example 2: Making a Callout from a Queueable Job

Unlike future methods, Queueable Apex allows better handling of API callouts:

public class CalloutQueueable implements Queueable, Database.AllowsCallouts {
    public void execute(QueueableContext context) {
        Http http = new Http();
        HttpRequest request = new HttpRequest();
        request.setEndpoint('https://api.example.com/data');
        request.setMethod('GET');
        HttpResponse response = http.send(request);
        System.debug(response.getBody());
    }
}

Usage:

System.enqueueJob(new CalloutQueueable());

Example 3: Chaining Queueable Jobs

One of the biggest advantages of Queueable Apex is the ability to chain jobs for sequential execution:

public class FirstJob implements Queueable {
    public void execute(QueueableContext context) {
        System.debug('First job executed');
        System.enqueueJob(new SecondJob());
    }
}

public class SecondJob implements Queueable {
    public void execute(QueueableContext context) {
        System.debug('Second job executed');
    }
}

Usage:

System.enqueueJob(new FirstJob());

This ensures that SecondJob runs only after FirstJob has completed.


Best Practices for Using Queueable Apex

  • Use for complex logic that requires multiple steps or dependent processes.
  • Leverage job chaining for structured execution of asynchronous tasks.
  • Limit job depth (Salesforce allows a max of 5 chained jobs per transaction).
  • Monitor execution in the Apex Jobs UI for tracking failures or debugging.
  • Prefer Queueable over future methods for better debugging, monitoring, and flexibility.

Conclusion

Queueable Apex is a powerful alternative to future methods, offering enhanced capabilities such as job chaining, support for sObjects, and better debugging. While future methods are still useful for simple async tasks, Queueable Apex should be the go-to choice for more complex and scalable processing in Salesforce.

Leave a Reply

Your email address will not be published. Required fields are marked *