ASYNCHRONOUS APEX IN SALESFORCE: OPTIMIZING OPERATIONS
Asynchronous Apex allows background processing without users having to wait for tasks to complete. It is typically used for operations that require higher limits, long-running tasks, or external system callouts. The key benefits include:
- User Efficiency
- Scalability
- Higher Limits
1. Batch Apex
Batch Apex is ideal for processing large data sets that would exceed normal processing limits. It processes records in batches asynchronously, making it suitable for tasks like data cleansing and archiving.
Batch Apex Syntax:
Start Method: Defines the scope of records to process, either using a Database.QueryLocator for SOQL queries (up to 50 million records) or an iterable for custom scopes
global (Database.QueryLocator | Iterable<sObject>) start(Database.BatchableContext bc) {}Execute Method: Processes each batch of records.
global void execute(Database.BatchableContext BC, List<sObject>){}Finish Method: Called after all batches are processed, often used for post-processing or sending confirmation emails.
global void finish(Database.BatchableContext BC){}
2. Future Methods
Future Methods allow long-running tasks, such as web service callouts, to run asynchronously. These methods are ideal for operations that need to be processed independently or involve multiple DML operations.
Future Method Syntax:
- Must be static and return void.
- Parameters must be primitive types, collections, or arrays. Custom or standard objects are not allowed as parameters.
global class SomeClass { @future(callout=true) public static void someFutureMethod(List<Id> recordIds) { // Process records asynchronously } }
- Ensure future methods are fast and efficient.
- Bundle web service callouts to avoid multiple future method calls.
- Future methods cannot call other future methods and may not execute in the order they were called.
- There is a limit of 50 future calls per Apex invocation.
3. Queueable Apex
Queueable Apex combines the simplicity of future methods with the power of Batch Apex, allowing non-primitive arguments and offering better monitoring. You can submit a queueable job using System.enqueueJob().
Queueable Apex Benefits:
- Supports non-primitive data types (e.g., sObjects).
- Jobs can be monitored using the job ID returned by System.enqueueJob().
- Allows chaining jobs, where a new job is started after a previous one is completed.
Queueable Syntax:
Chaining Jobs:
- Chain jobs by submitting a new job from the execute method.
- Only one child job can exist for each parent job, and you can add up to 50 jobs in a single transaction.
public class ChainingExample implements Queueable { public void execute(QueueableContext context) { System.enqueueJob(new NextJob()); } }
Conclusion
Asynchronous Apex offers multiple tools—Batch Apex, Future Methods, and Queueable Apex—to handle large-scale processing, long-running operations, and job chaining efficiently. Each has its specific use cases, and understanding when to use them will help optimize Salesforce operations.
Contact Us
We would love to hear from you Please feel free to send us a message via the form