0% found this document useful (0 votes)
41 views5 pages

Sales

Uploaded by

oemail403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
41 views5 pages

Sales

Uploaded by

oemail403
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Asynchronous and Synchronous Processes in Salesforce

Introduction
In Salesforce, tasks can be processed either synchronously or asynchronously. Understanding
the difference between these two methods is crucial for optimizing performance and user
experience.

Synchronous Processes
Synchronous processes run immediately and require the user to wait until the task is completed.
These processes are straightforward but can lead to delays if the task is complex or involves
large data volumes.

Asynchronous Processes
Asynchronous processes run in the background, allowing users to continue their work without
waiting. These processes are ideal for handling long-running or resource-intensive tasks.

Advantages and Disadvantages


Criteria Synchronous Processes Asynchronous Processes

Advantages - Immediate results: Users receive - Improved performance: Tasks run in the
feedback right away background, freeing up the main system

- Simplicity: Easier to implement and - Better user experience: Users can continue
understand their work without waiting

- Transaction control: Easily managed - Scalability: Efficiently handles large volumes


within a single context of data and complex processes

- Resource optimization: Efficiently utilizes


system resources

Disadvantages - Performance impact: Long-running tasks - Delayed results: Users do not receive
can slow down the system immediate feedback

- User wait time: Users must wait for the - Complexity: More complex to implement and
task to complete manage

- Resource intensive: May consume - Error handling: Errors may be more


significant system resources, affecting challenging to debug and manage due to the
overall performance background processing

https://www.linkedin.com/in/iam-murali/
Asynchronous and Synchronous Processes in Salesforce

Synchronous Processes in Salesforce

When to Use Synchronous Processes

- Immediate Response Needed: When the result of the operation is needed right away.
- Simple Tasks: For tasks that complete quickly without impacting user experience.

Example:

Synchronous Apex Method

public class SynchronousExample {


public void updateAccounts(List<Id> accountIds) {
List<Account> accounts = [SELECT Id, Name FROM Account WHERE Id IN
:accountIds];
for(Account acc : accounts) {
acc.Name += ' - Synchronous Updated';
}
update accounts;
}
}

In this example, the `updateAccounts` method runs synchronously. The user must wait for the
method to finish updating the accounts before proceeding.

Asynchronous Processes in Salesforce

Why Use Asynchronous Processes?

1. Improved Performance: Tasks run in the background, freeing up the main system for other
activities.
2. Better User Experience: Users don't have to wait for long tasks to finish before they can
continue working.
3. Scalability: Helps manage large volumes of data and complex processes more efficiently.

Key Asynchronous Methods in Salesforce

1. Future Methods

Future Methods are used to run processes in the background. They are simple to implement
and are often used for integrating with external web services or performing large-scale data
operations.

https://www.linkedin.com/in/iam-murali/
Asynchronous and Synchronous Processes in Salesforce

Example:

public class FutureExample {


@future
public static void updateAccounts(Set<Id> accountIds) {
List<Account> accounts = [SELECT Id, Name FROM Account WHERE Id IN
:accountIds];
for(Account acc : accounts) {
acc.Name += ' - Updated';
}
update accounts;
}
}

In this example, the `updateAccounts` method is a future method that updates account names.
It runs in the background, so the user can continue working without waiting for the update to
complete.

2. Batch Apex
Batch Apex is used for processing large volumes of data. It splits the data into smaller chunks
and processes each chunk separately.
Example:
global class BatchExample implements Database.Batchable<sObject> {
global Database.QueryLocator start(Database.BatchableContext BC) {
return Database.getQueryLocator('SELECT Id, Name FROM Account');
}

global void execute(Database.BatchableContext BC, List<Account> scope) {


for(Account acc : scope) {
acc.Name += ' - Batch Updated';
}
update scope;
}
global void finish(Database.BatchableContext BC) {
// Final steps after batch execution
}
}

Here, the `BatchExample` class processes accounts in batches. The `start` method defines the
query, `execute` processes each batch, and `finish` handles final steps.

https://www.linkedin.com/in/iam-murali/
Asynchronous and Synchronous Processes in Salesforce

3. Queueable Apex

Queueable Apex allows you to chain jobs and pass complex data types to the job. It's more
flexible than future methods.

Example:
public class QueueableExample implements Queueable {
public void execute(QueueableContext context) {
List<Account> accounts = [SELECT Id, Name FROM Account];
for(Account acc : accounts) {
acc.Name += ' - Queue Updated';
}
update accounts;
}
}

In this example, `QueueableExample` implements the `Queueable` interface, which allows the
job to be queued and processed asynchronously.

4. Scheduled Apex

Scheduled Apex lets you schedule Apex classes to run at specific times.

Example:
global class ScheduledExample implements Schedulable {
global void execute(SchedulableContext sc) {
List<Account> accounts = [SELECT Id, Name FROM Account];
for(Account acc : accounts) {
acc.Name += ' - Scheduled Updated';
}
update accounts;
}
}
// Schedule the job:
ScheduledExample job = new ScheduledExample();
String sch = '0 0 12 * * ?'; // Schedule to run daily at noon
System.schedule('Daily Account Update', sch, job);

The `ScheduledExample` class runs at noon every day, updating account names.

https://www.linkedin.com/in/iam-murali/
Asynchronous and Synchronous Processes in Salesforce

Conclusion:

Understanding the difference between synchronous and asynchronous processes in Salesforce


is crucial for optimizing performance and user experience. Use synchronous processes for
simple, immediate tasks, and leverage asynchronous methods for long-running,
resource-intensive operations. By using Future Methods, Batch Apex, Queueable Apex, and
Scheduled Apex, you can handle complex tasks efficiently and improve the overall performance
of your Salesforce applications.

https://www.linkedin.com/in/iam-murali/

You might also like