Updated: Mar 31, 2020
An asynchronous process is a process or function that executes a task "in the background" without the user having to wait for the task to finish.
In other words, it is used to run processes in a separate thread, at a later time. Or process that doesn't need user interaction (button click, giving input).
For example: I am a manager working on some project. Now in same project two more tasks are given, first is to create a document and a presentation(ppt) for a project which are independent. What I will do is to give these two tasks to my subordinates to complete. In this way I don't have to wait for anyone. This is called Asynchronous processing.
Why Asynchronous processing?
Asynchronous processing, 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.
By allowing some features to execute when resources become available at some point in the future, resources can be managed and scaled quickly. This allows the platform to handle more jobs using parallel processing.
3. Higher Limits
Asynchronous processes are started in a new thread, with higher governor and execution limits. And to be honest, doesn’t everyone want higher governor and execution limits?
Ways to achieve Asynchronous processing : -
1. Future Methods
2. Batch Apex
3. Queueable Apex
4. Scheduled Apex
In this article, I will elaborate future methods.
Method will work like a normal method but in addition it will get executed in future(some time later) if the resources are not available right now. That's why it is called future method.
1.You can perform mixed DML operation on setup and non setup objects in one transaction.
2.Do not block the user from performing other operations.
3.Providing higher governor and execution limits for the process.
When to use :-
1. For executing long-running operations, such as callouts to external Web services or any operation.
2. Operations you want to run in their own thread.
Future Method Syntax
Future methods must be static methods, and can only return a void type. Future methods can’t take standard or custom objects as arguments. Any method that have @future annotation salesforce will understand that this method has to be run asynchronously.
In the below example, I am sending an accountId to future method(updateAccount) and this will run in background to update the field value.
1. A future call cannot make another future call.
2.The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types.
3.There is no certain order of execution.
4.Can't take objects as arguments.
5. There is a limit of 10 future calls per Apex transaction.
How to resolve 'You have uncommitted work pending. Please commit or rollback before calling out' error :
We often find this type of error. To resolve this error use future method and follow the below sequence while doing transaction. How you can implement that I will explain in separate blog very soon.