SalesforceBlue

Feel the rhythm of Salesforce

Apex

Apex Asynchronous Fundamentals

Before exploring Apex Asynchronous Fundamentals let’s understand more about synchronous transactions and see how both of them play a vital role in application development.

The synchronous transaction is like a relay race where members of a team take turns completing parts of the racecourse or performing a certain action. The catch here is they take turns and wait for one team member to finish their part. Once all the racers finish their individual part then the whole race can be marked as completed.

Similarly, in Apex for the synchronous transactions, each method waits for its turn to execute and return results back to the caller. The transaction is completed when all the methods execute sequentially as they were called. A user has to wait till the time the whole transaction completes.

This approach is good unless there is one resource-intensive method that does not return anything and takes significant time to execute making the user wait for a long. It won’t be a very good user experience.

If there is a way we can say, “Hey! you time-consuming method please execute in a separate transaction and make the user do something else instead of waiting for you.”

We have that way and are called Asynchronous Transactions.

Thus, If any given method has no results to be returned to the browser that method can be moved to a separate transaction or asynchronous transaction.

In a nutshell, Asynchronous transactions are used to run processes in a separate thread, at a later time.

There are several benefits of Asycnrhonous Transactions which are as follows :

Efficiency:

If you have a long operation for which the user may have to wait. We can call that long operation in a separate transaction if it has no immediate results to be shown on the browser.

Scalability:

It allows the process to execute when the resource becomes available.

Fresh Governor Limits:

For an Asynchronous Transaction, there are a fresh set of governor limits with more range for a few limits. For Example, we can do 200 SOQL queries. The Heap size limit becomes 12MB.

You can visit Governor Limits to explore all the Apex transaction limits and their respective values in asynchronous transactions.

There are certain ways to perform asynchronous transactions which are as follows:

Future Methods

Queuable Apex

Batch Apex

Scheduled Apex

Please visit each of them to see how we can call an asynchronous transaction as per the requirements.

Thank you for visiting SalesforceBlue.com
If you have any queries feel free to write down a comment below 🙂


Leave a Reply

Your email address will not be published. Required fields are marked *