What is Asynchronous Apex?
Asynchronous Apex is the process which runs in the background without the user having to wait for the process to finish.
We use asynchronous apex mostly when callouts to external systems is required, code needs to be run at some particular time, higher Governor Limits are required for some operation.
Key benefits of Asynchronous Apex in Salesforce include:
- User efficiency
- Scalability
- Higher Governor Limits
Asynchronous apex can be implemented using different techniques which includes Future Methods, Batch Apex, Queueable Apex and Scheduled Apex. Overview of the common scenarios where these can be used in Salesforce:
Asynchronous Process Type | Examples |
---|---|
Future Methods | 1. Callouts to external Web services 2. Operations which can run in their own thread 3. To prevent the mixed DML error |
Batch Apex | 1. When processing is required on lots of records 2. Jobs that need larger query results |
Queueable Apex | 1. Processing on Non-primitive data types 2. Chaining of jobs 3. To start a long-running operation and get an ID for it |
Scheduled Apex | When a process needs to be run at some specific time |