Salesforce Asynchronous Apex Part 1
Contents
[NOTE] Updated March 28, 2020. This article may have outdated content or subject matter.
Learning Objectives
After completing this article, you’ll be able to:
Prologue
Ahoy mates, Hope everyone is safe during this Corona Virus pandemic, stay strong. It’s all about the good and bad bits of asynchronous programming on the Salesforce platform. Lets first understand what it means for a block of code or program to execute asynchronously. Asynchronous programming allows multiple executions to happen at the same time. When you start an action, your program continues to run. When the action finishes, the program is informed and gets access to the result returned, this can be a success or a failure. In laymen terms, think of going to a restaurant and buying your favorite $10,000 Lasagne 😥. This restaurant is very popular hence a lot of customers to serve, so you select your Lasagne then submit your order, but Lasagne takes a long time, therefore you of to wait before get your treat. You move outside the line, so another customer can order there serving as well, without blocking subsequent orders. In a nutshell, asynchronous Apex is used to run processes in a separate thread, at a later time.
Use Cases
You’ll typically use Asynchronous Apex for callouts to external systems, operations that require higher limits, and code that needs to run at a certain time.
Benefits
The key benefits of asynchronous processing include but not limited to the following:
- User efficiency, you are provided with the means to offload long running operations to another thread. Hence not slowing down the UI, with asynchronous processing the user can get on with their work, the processing can be done in the background and the user can see the results at their convenience.
- Scalability, by allowing program to execute on another thread, your allowing other applications to execute when system resources becomes available. Hence, providing the platform to execute additional jobs.
- More freedom, using asynchronous apex offloads work to a new thread, this means that you get a fresh batch of governor limits. For example, the number of SOQL queries is doubled from 100 to 200 queries when using asynchronous calls.
Multi-tenant Challenges
Multi-tenancy is an architecture in which a single instance of a software application serves multiple customers. Asynchronous programming, in Salesforce, presents some challenges:
- Processing Equality, making sure each customer in one instance gets fair use of the resources made available
- Fault Tolerance, how do we ensure async requests are not lost in processing or transit due to hardware or software failure.
- Entry Insert, making sure async requests are successfully queued with the correct payload.
- Entry Remove, ensuring that enqueued requests for customers are dequeued correctly along with the right payload.
Resource Conservation measures
Processing in Apex is normally synchronous in nature, therefore Asynchronous processing has lower priority in processing. This is due to the fact that the queuing framework monitors system resources such as server memory and CPU usage and reduce asynchronous processing when thresholds are exceeded. Remember that the Salesforce platform is multi-tenant based, hence synchronous processing is always prioritized and enforced.
Async Features Available
There currently four asynchronous models available as of Saturday, 28 March 2020.
Future Methods
Allows you to execute Apex classes in their own thread. Suitable for callouts, or when we want to prevent the mixed DML error.
Batchable Apex
Was design to perform bulk processing in the background. Suitable for, Data cleansing or archiving of records.
Queueable Apex
Similar to future methods, but provide additional job chaining and allow more complex data types to be used. Suitable for sequential processing operations for web services.
Schedulable Apex
utilize this asynchronous feature when your want to submit timed operations at specific intervals. Suitable for recurring tasks, like mass updates or delete for new or existing environments.
Epilogue
That was short and straight to the point, the purpose for this article was to broadly introduce the different asynchronous features made available for your applications to leverage. We also discussed the essence of asynchronous processing, including the use cases, conservative measures to consider and how Salesforce increases the governor limits for such operations in the system. Thanks for stopping by 😃 and i hope you was able to learn something new or reinforce what you already knew. A través del aprendizaje continuo, ¡mejorará!
Author Remario Richards
Modified March 28, 2020