In this blog, we will discuss about the future method in Salesforce with an example. We will also discuss how it’s different from the queueable apex.
It operates in the background asynchronously, running in a separate thread whenever resources are available, and it helps in preventing mixed DML errors.
Use @future annotations to declare a method as the future method. It helps us to run long-running operations and prevents us from hitting the governor limit.
How to use the Future method?
Firstly, we have to annotate the method as @future. It must be static and return void. Only take primitive type parameters like Collection of Ids and an array of primitive datatypes.
To do a callout we have to use one extra parameter i.e. (callout=true) while annotating it.
Want to Learn Salesforce Flows? Checkout our Salesforce Flow Course
Let’s understand the Future method in Salesforce with an example.
When the opportunity is updated to closed won, we have to send the mail to its account owner informing them about the opportunity details.
The above snippet is a trigger which executes when the opportunity stage name is updated to closed won. Here we execute it after the update event.
The above code is the handler for the trigger from which we are checking the old and new values if they have been updated and stored the IDs in the OpportunityIds set that we are passing in the future method.
We can also directly call a future method from the trigger but there may be chances of hitting an error if insert will happen from batch class as we cannot call Future from Batch.
Above is the method which is declared as future and here we are using it to send the mail to the account owner. Hence this logic can be run in the background so we have annotated it with @future.
As we can we have one opportunity related to Edge Communication having status Id. Decision Makers. Let’s update it’s stage to closed won.
In the logs, we can see our future method executed successfully.
As a result, we also got the emails. Below is the snapshot of our mail generated.
Advantages of using the Future method
- The governor limit will be higher in terms of query and heap size limit.
- Can be used for long-running operations.
- Consider future methods for a small set of data.
Limitations of future method
- sObject can’t be passed in the parameter of the future method.
- Cannot process large sets of data. Instead use Batch Apex.
- Do not return the Job Id to monitor the jobs.
Queueable Vs Future
- We cannot call one future method from another. As a result, chaining is not possible in future. However, we can queue one queueable from another.
- Future don’t return the Job Id which makes it difficult to monitor. On the other hand queueable returns the job Id.
- Future only supports primitive datatypes but queueable supports both primitive and non primitive datatypes.
- Unlike queueable, future can’t be scheduled to run at a specific time.
Also Read – Queueable Apex in Salesforce with examples
1. What is a mixed DML error?
This type of error occurs when we try to perform DML operation on setup and nonsetup objects in a single transaction
2. Can we call the future from Batch apex?
No, we cannot call future methods from batch apex. As they operate in different way. Mixing these can lead to unexpected behaviour.
3. How many future methods can be called from a single transaction?
We can invoke 50 future methods in a single transaction.
4. Why sObject is not allowed in future as an argument?
As it is an asynchronous process, the sObject can change between the time you call the method and the time it executes. It may be possible that the argument will receive the older version and can overwrite them.
In this blog, we discussed the Futue method in Salesforce with examples, all the advantages, and limitations, and how it is different from queueable apex.