For developers working with Salesforce, there are various ways to handle long-running processes or operations that exceed the platform’s synchronous processing limits. Three of these methods are Queueable Apex, Future Methods, and Batch Apex. This tutorial will delve deep into each, offering examples and use cases to aid understanding.
1. Future Method
Introduction:
Future methods run in the background, allowing developers to execute methods asynchronously. This is particularly useful for callouts after DML operations.
How to use:
- Annotate a method with
@future
. The method should be static and returnvoid
. - Future methods cannot take
sObjects
as parameters. Instead, they can take primitive data types, arrays, or collections of primitives.
Example:
public class FutureMethodExample { @future(callout=true) public static void makeCallout(String endpoint) { Http http = new Http(); HttpRequest request = new HttpRequest(); request.setEndpoint(endpoint); request.setMethod('GET'); HttpResponse response = http.send(request); // Handle response here } }
Limitations:
- No order of execution is guaranteed.
- You can’t invoke another future method from a future method.
- Limited to 50 future calls per Apex invocation.
2. Queueable Apex
Introduction:
Queueable Apex allows more flexibility than future methods. It’s used to run jobs asynchronously in a sequence. It also supports chaining, where one job can start another.
How to use:
- Implement the
Queueable
interface. - Use the
System.enqueueJob()
method to add the job to the queue.
Example:
public class QueueableExample implements Queueable { public void execute(QueueableContext context) { // Your logic here } } // To enqueue the job ID jobID = System.enqueueJob(new QueueableExample());
Chaining:
public class FirstJob implements Queueable { public void execute(QueueableContext context) { // Logic for the first job System.enqueueJob(new SecondJob()); } } public class SecondJob implements Queueable { public void execute(QueueableContext context) { // Logic for the second job } }
Limitations:
- Each execution context may have up to 50 chained jobs.
- Chained jobs count against the limit of five queued or active jobs for an org.
3. Batch Apex
Introduction:
Batch Apex is used for processing large sets of records asynchronously, by dividing them into smaller batches.
How to use:
- Implement the
Database.Batchable
interface. - This requires three methods:
start
,execute
, andfinish
.
Example:
public class BatchExample implements Database.Batchable<sObject> { public Database.QueryLocator start(Database.BatchableContext bc) { return Database.getQueryLocator('SELECT Id, Name FROM Account'); } public void execute(Database.BatchableContext bc, List<sObject> scope) { // Process each batch of records } public void finish(Database.BatchableContext bc) { // Code to execute after all batches are processed } } // To execute the batch job ID batchJobId = Database.executeBatch(new BatchExample(), 200);
Limitations:
- Batches can’t be chained like Queueable jobs.
- A developer should carefully choose batch sizes, depending on the operation.
Comparison
- Use Cases:
- Future: Quick, simple operations post DML. For instance, making a callout after a DML operation.
- Queueable: When you need more flexibility than future methods, and possibly chain jobs.
- Batch Apex: For processing large sets of data.
- Order of Execution:
- Future: Not guaranteed.
- Queueable: Sequential with chaining.
- Batch Apex: Processes in order, but each batch in
execute
is independent.
- Parameters:
- Future: Only primitive data types.
- Queueable: More flexibility than future methods.
- Batch Apex: Works on large sets of sObject records.
Selecting between Future, Queueable, and Batch Apex largely depends on the use case. For simple asynchronous tasks post DML, Future might suffice. For more complex jobs with a need for chaining, Queueable is better. And for bulk record processing, Batch Apex is the way to go.
Remember, always design with scalability in mind, and be aware of the governor limits associated with each approach.