In a Asynchronous call, the thread will not wait until it completes its tasks before proceeding to next. Instead it proceeds to next leaving it run in separate thread. In a Asynchronous call, the code runs in multiple threads which helps to do many tasks as background jobs.
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 |
Example:
@Future Methods: It is the basic Asynchronous apex in Salesforce and executes when salesforce has available resources.
Batch Apex: This Asynchronous Apex Salesforce feature is used to process multiple records for the job together where we process large number of records in batches/chunks.
Queueable Interface
Scheduled Apex
In a Synchronous call, the thread will wait until it completes its tasks before proceeding to next. In a Synchronous call, the code runs in single thread.
Synchronous execution takes place as a single transaction, and it doesn't wait for available resources. The entire Apex code runs in one go, and the thread waits for the task to be completed before moving on to the next task sequentially.
Example:
Trigger
Controller Extension
Custom Controller
Asynchronous | Synchronous |
Actions that will not block the transaction or Process | Quick and Immediate actions |
Duration is not priority | Transactions are immediate and serial |
Higher Governor limits | Normal Governor Limits |
0 Comments