Introduction
Asynchronous Apex in Salesforce is a true Savior from the Salesforce governor limits. When there are a limited number of resources available in a multi-tenant architecture. Certainly, it is hard to decide how to utilize resources to their best limits. So, what should you do when you want to process a huge amount of data together? Also, to avoid this issue we can maximize the resources utilization, the role of asynchronous apex came into the picture. Salesforce apex classes have introduced the concept of Salesforce asynchronous apex that provides us with a double of the governor limits for almost each software program.
Also, it is possible to perform up to 50 million operations on records using asynchronous apex Salesforce. There are multiple Asynchronous apex features in Salesforce and you have to decide which Salesforce asynchronous callout suits your business goals the most. Let us start our guide with asynchronous apex jobs and types of Asynchronous Apex Salesforce CRM. Meanwhile, you can give a quick go through in this in-demand online Salesforce training. – in short active sentences include transition words
What Is An Asynchronous Process?
An asynchronous process is job or function that doesn’t have direct interaction with user. Suppose you have implemented SFDC for a mobile company and the company has millions of customers using its services. Company decides to flag those customers as inactive that have not recharged in the last one year. For this you can create a nightly job (usually by a batch job) to do this activities. Here this job runs in the background and doesn’t have direct interaction with user.
Salesforce provides various way of implementing :
Asynchronous Apex (@future Apex, batch Apex, queueable Apex, scheduled Apex)
Bulk API jobs
Scheduled Reports
Dashboard refreshes
Out of these @future annotation gives developers an option to run a apex method asynchronously
The best part of use of this process is that you get increased limit on some of the governor limits. e.g. Total number of SOQL queries issued for each apex transaction is 200 .
You can learn more at this link
- Future Methods
- Queuable Jobs
- Scheduled Apex
- Batch Apex
Asynchronous Vs Synchronous
Ultimately, the choice comes down to operational dependencies. Do you want the start of an operation to be dependent on the completion of another operation, or do you want it to run independently?
Asynchronous is a non-blocking architecture, so the execution of one task isn’t dependent on another. Tasks can run simultaneously. Synchronous is a blocking architecture, so the execution of each operation is dependent on the completion of the one before it. Each task requires an answer before moving on to the next iteration.
The differences between asynchronous and synchronous include:
- Async is multi-thread, which means operations or programs can run in parallel. Sync is single-thread, so only one operation or program will run at a time.
- Async is non-blocking, which means it will send multiple requests to a server. Sync is blocking — it will only send the server one request at a time and will wait for that request to be answered by the server.
- Async increases throughput because multiple operations can run at the same time. Sync is slower and more methodical.
Differences aside, both methods both offer advantages, but for different stakeholders: Async for users and sync for developers.
Understand the Queue based framework first.
- Enqueue – The request gets put into the queue. Meanwhile, this could be an Apex batch request, @future Apex request or one of many others. The Salesforce application will enqueue requests along with the appropriate data to process that request.
- Persistence – The enqueued request gets persisted. Requests are particularly stored in persistent storage for failure recovery and to provide transactional capabilities.
- Dequeue – The enqueued request is removed from the the queue and processed. Transaction management in fact occurs in this step to assure messages are not lost if there is a processing failure.
Why To Use Asynchronous Apex In Salesforce?
Before understanding all Asynchronous Apex processes in Salesforce. Let’s understand why we need an Asynchronous process in Salesforce.
1. Callout from Trigger
Can we do the callout from Trigger? The answer is no. But we can do this with the help of Asynchronous Processing(@future method).
2. Mixed DML Operations
A mixed DML Operation Error comes when you try to perform DML operations on setup and non-setup objects in a single transaction. We can use Asynchronous Processing to resolve this issue.
3. Apex CPU Time Limit
Salesforce CPU time limits usage to 10 seconds for synchronous transactions and 60 seconds for asynchronous processing. The “Apex CPU time limit exceeded” error means your transaction is taking too long and can’t be completed. The solution to the problem is to Use Async Apex.
There are lots of other examples of using Asynchronous Apex in Salesforce is Large volume of data loads & transactions and etc. Let talk about all solution in details.
1. Using @Future Annotation
You can call a future method for executing long-running operations, such as callouts to external Web services or any operation you’d like to run in its own thread, on its own time. Check our Future method in the salesforce post to learn more about @future annotation.
2. Queueable Class
Queueable apex is hybrid class. Why you are calling it as hybrid class? Yes, I mean to say it is designed in a way by combining the batch apex and future apex. So, that means do we have to again write the start, execute, and finish methods like the batch. As I said it is a combination not the exact replica. No, you don’t need to write start and finish methods. All you need is execute method. How cool it is right.
Here are some key features of Queueable Class:
- Firstly, a class and method that can be added to the queue to be executed.
- It’s monitorable and abortable.
- It’s chain able.
- A public class that implements the Queueable interface
- Also, Includes an execute method that accepts only a Queueable Context parameter.
- Above all,the execute method can access instance properties for the class.
- Returns void.
- Launch by calling System.enqueueJob(cls) with an instance of the class.
- Returns an AsyncApexJob Id
Differences between Future and queueable apex
Check our Future Vs Queueable Apex post to learn about the Differences between Future and queueable apex.
3. Batch Class
A Batch Apex in Salesforce allows you to define a single job that can be broken up into manageable chunks that will be processed separately. Batch Apex is a global class that implements the Database. Hence, Batchable interface.
- Start method – identifies the scope (list of data to be processed)
- Execute method – processes a subset of the scoped records
- Finish method – does any post-job wrap-up.
Additional interfaces:
- Database.Stateful
- Database.AllowsCallouts
Some key points for batch job in Salesforce
- Launch by calling Database.executeBatch(cls) with an instance of the class and an optional scope size
- Default scope size is 200
- Max scope size is 2,000
- Returns an AsyncApexJobId
4. Schedulable Class
A global class that implements the Schedulable interface. It includes an execute method. You can schedule by calling System.Schedule('job name', cron expression, cls)
cron expression.
EXAMPLE OF SCHEDULABLE APEX CLASS.
public with sharing class accountcreate{
public void createaccont(){
Account accobj = new Account();
accobj.FirstName=’TestAccout’;
insert accobj;
}
}
SCHEDULABLE APEX
global class scheduledAcc implements Schedulable{
global void execute(SchedulableContext SC) {
accountcreate abc = new accountcreate();
abc.createaccount();
}
To conclude:
We hope this blog gives you a clear in detail idea on why to use an asynchronous apex in Salesforce and how it can be beneficial for your business. Altogether, the major benefits we noticed are more flexible governor limits in Salesforce asynchronous apex when compared to the synchronous apex code. The overall concept may be a little tough to understand at first glance but you will practice it quickly with continuous effort and dedication.
FAQs
1.What does it mean to be asynchronous?
Asynchronous, which is pronounced ay-SIHN-kro-nuhs and comes from the Greek words asyn-, which means “not with,” and chronos, which means “time,” is a broad term used to describe things or occurrences that are not time-coordinated.
2. What is Salesforce asynchronous?
First, let’s define the Asynchronous process and describe how Salesforce uses it. Requests that don’t complete at the same time, but rather one at a time are known as asynchronous processes.
3.What distinguishes synchronous communication from asynchronous communication?
What are the primary distinctions? Synchronous means that it occurs simultaneously. Asynchronous means not occurring simultaneously. Participants can receive quick feedback with synchronous learning. Learn more about Salesforce with this basic Salesforce tutorial guide.
4. When not to use asynchronous?
In addition, another situation when async may not be useful is when you have a single database server not utilizing connection pooling. If all requests hit the same database using a long running connection, it won’t make a difference if the calls are asynchronous or synchronous.
5. What is the importance of asynchronous work?
Asynchronous work allows employees to choose their own hours, as opposed to having to work the same schedule as others on their team. Basically, this flexibility helps with employee morale and establishing a work-life balance. Furthermore, it improves your company’s retention rate, which saves both time and money.
For More Blogs: Click Here