Hot Posts

Synchronous and Asynchronous in Salesforce

Synchronous and Asynchronous in Salesforce

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 TypeExamples
Future Methods1. Callouts to external Web services
2. Operations which can run in their own thread
3. To prevent the mixed DML error
Batch Apex1. When processing is required on lots of records
2. Jobs that need larger query results
Queueable Apex1. Processing on Non-primitive data types
2. Chaining of jobs
3. To start a long-running operation and get an ID for it
Scheduled ApexWhen 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

AsynchronousSynchronous
Actions that will not block the transaction or ProcessQuick and Immediate actions
Duration is not priorityTransactions are immediate and serial
Higher Governor limitsNormal Governor Limits

Post a Comment

0 Comments