Join us to understand what Asynchronous Apex means and learn about techniques to use them on the Platform. Determine Why, Where & When to use these processes. Discuss various pros/cons of each of the techniques and take a look at some of the real-time examples. We will discuss using @future methods, Queueable, and Batachable Interfaces in Apex for Async processing.
This will be a fast-paced webinar that will cover a wide breadth of topics on Asynchronous Processes and you are sure to walk away with ideas & techniques for using these in your day-to-day job on the Platform.
What is an Asynchronous Process?
In technology terminology, Asynchronous operation means that a process operates independently of other processes.
Asynchronous vs Synchronous
What is a synchronous and asynchronous process in Salesforce? Let’s understand the difference between Asynchronous and Synchronous processes.
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 |
What is an Asynchronous Apex?
Asynchronous Apex is used to run process in a different thread. An asynchronous process is a process that executes a task “in the background” without the user having to wait for the task to finish. It help us to solve Governor Limits in Salesforce.
Asynchronous Process on the Salesforce Platform
- Ensure fairness of processing – Make sure every customer gets a fair chance at processing resources.
- Ensure transactional capabilities – Ensure equipment or software failures allow continued processing at reduced capacity and requests are persisted until completion.
Queue based framework
Understand the Queue based framework first.
- Enqueue – The request gets put into the queue. 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 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 occurs in this step to assure messages are not lost if there is a processing failure.
Type of Asynchronous Process in Salesforce
How many ways do we have in Salesforce for Aysnc processing?
- Schedule & Batch jobs
- Queues
- @future
- Change Data Capture
- Platform Events – Event Based
- Continuations (UI)
Why 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 a hybrid class. Why are you calling it a hybrid class? Yes, I mean to say it is designed 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 to execute the method. How cool it is right.
Here are some key features of Queueable Class:
- 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
- Includes an execute method that accepts only a Queueable Context parameter.
- 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.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.
Run at 30 minutes past midnight on Jan 1 every year
GeocodingSchedulable cls = new GeocodingSchedulable();
System.Schedule('Geocode on Jan 1', '0 30 0 1 1 ? *', cls);
// Run Every hours
System.Schedule('Geocode hourly', '0 0 * * * ? *', cls);
We have Platform Events and Change Data Capture for Asynchronous Processing as well.
Asynchronous Apex Video
Summary
Asynchronous Processing is a way to execute the process in the back-end or a different thread to save the CPU time limit in Salesforce. In Apex, we have a different way to execute code in Async, which is called Asynchronous Apex.
Need to get followup on recent posts.