Here's All you need to Know About Asynchronous Apex in Salesforce
Apex offers multiple ways to run your Apex code asynchronously. This can save you time and help you keep your code more organized.
Asynchronous apex is used to run process in a separate thread at later time.
It is a process or function that executes a test in the background without the user having to wait for the task to finish.
Benefits of Asynchronous Apex
- User efficiency
- Resources efficiency
- Scalability
- Fairness of processing
- Transactional capabilities
- Avoid governor limits
- Mixed DMLS
- Long-running operation and many more
Types of Asynchronous Apex
Asynchronous apex comes with four different types. Depending on the use case, the user can identify which type suits the requirements
- Future method
- Batch Apex
- Queueable apex
- Schedule apex
Don't forget to check out: Invoke Apex Actions from Flow | Salesforce Flow Guide
Future Apex
Future apex is used to run the process in a separate thread at a later time when system resources become available.
- Methods are annotated with @future annotation.
- It uses for calls out to external web services.
- Operation you want to run in their own thread.
- Isolating DML operation on different sObjects type to prevent the mixed DML error.
Syntax
- Must be static.
- Only return a void type.
- A parameter must be a primitive data type.
- An array of primitive data types.
- Don't take standard or custom object as argument.
- Common pattern is to pass the method a list of record Ids that you want to process asynchronously.
Batch Apex
Batch Apex in Salesforce is an important tool used to process large amounts of records without exceeding normal platform limits.
- To use batch Apex, write an Apex class that implements the Salesforce-provided interface Database.Batchable and then invoke the class programmatically.
- Execute codes in small batches at a time
- Batch apex is monitorable and abortable
- Batch Class always be global
Every batch class must have three methods
- Start method
- Execute method
- Finish method
The Start and Finish method only executes once
Execute method executes equal to the number of batches
Queueable Class
- A class and method that can be added to the queue to be executed
- Queueable Class is monitorable and abortable.
- Queueable Class is launched by calling System.Enqueue job(cls) With an instance of the class
- It returns an async apex Job ID
- Queueable Class is chainable
- The difference between Queueable Apex and Future methods is –
- Queueable Apex supports job-chaining
- It supports non-primitive data types like sObjects or custom objects.
- Maximum 50 jobs can be added to the queue in a single transaction.
- Only one child job can exist for each parent job.
- There is no limit on the dept of the chained jobs.
Check out an amazing Salesforce video tutorial here: Crash Course on Apex Triggers Salesforce | Complete Guide with Real Time Scenarios
Scheduable Class
- Schedulable Class is a global class that implements this schedulable interface
- It Includes an execute method
- Can be scheduled by calling system. Schedule (‘job name’, cron exp., cls)
- Return cron trigger ID
- CAN also scheduled via Salesforce UI
- When a class is scheduled, a CronTrigger object is created.
- The getTriggerId method can be used to return the ID of a CronTrigger record.
- If the class is scheduled from Trigger, the Governor limit should be checked.
- The additional processing should be done in a separate class outside of the execution method.
- You can have up to 100 scheduled Apex jobs at a time.
Differences Between Different Classes
Type | Overview | Common Scenarios |
Future Methods | Run in their own thread, and do not start until resources are available. | Web service callout. |
Batch Apex | Run large jobs that would exceed normal processing limits. | Data cleansing or archiving of records. |
Queueable Apex | Similar to future methods, but provide additional job chaining and allow more complex data types to be used. | Performing sequential processing operations with external Web services. |
Scheduled Apex | Schedule Apex to run at a specified time. | Daily or weekly tasks. |
Responses