Here are four points we are going to discuss about future methods:
What is Future Method ?
- As we know Future methods runs on background, asynchronously.
- Future method mostly used to run long running process in background for example callout any external web service or any process that runs on its own thread.
- Also we can utilise future method to solve mixed DML error in salesforce.
- By utilising future method salesforce governor limits are higher, such as SOQL query limits and heap size limits.
When to use future method ?
- Callouts to external Web services.
- If you want to run any process or operations in their own thread.
- Isolating DML transaction on different sObject types to prevent the mixed DML error.
Future method must use @future annotation and it must be static and void return type and parameter with only primitive type like Collection of Ids.
Lets understand future method by updating bulk case records in below example
Example of Future method in Salesforce :
global class FutureMethodExampleInSalesforce
{
@future
public static void processCaseData(List<ID> lstCaseIds)
{
List<Case> lstCaseToUpdate = new List<Case>();
/* Get case records on basis of Case Ids */
List<Case> lstCases = [SELECT Subject, Description FROM Case WHERE Id IN :lstCaseIds];
/* Write logic to update cases */
for(Case objCase : lstCases){
objCase.Subject = objCase.Subject + 'Updated Subject';
lstCaseToUpdate.add(objCase);
}
if(!lstCaseToUpdate.isEmpty())
update lstCaseToUpdate;
}
}
When does mixed DML error occur ?
Mixed DML error occurs when we perform DML operation on setup and nonsetup objects in a single transaction.
How to resolve Mixed DML error with Future Method ?
global class FutureMethodExampleInSalesforce
public static void exampleOfmixedDML()
{
Case objCase = new Case(Subject='Computer is not working');
insert objCase;
Profile objProfile = [SELECT Id FROM Profile WHERE Name='System Administrator'];
UserRole objRole = [SELECT Id FROM UserRole WHERE Name='Admin'];
User objUser= new User(alias = 'testSalesforceCRM', email='abc@salesforceCRM.com',
emailencodingkey='UTF-8', lastname='SalesforceCRM',
languagelocalekey='en_US',
localesidkey='en_US', profileid = objProfile.Id, userroleid = objRole.Id,
timezonesidkey='America/Los_Angeles',
username='abc@salesforceCRM.com');
insert objUser;
}
}
5 Limitations of future method in Salesforce :
- 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.
- Future Methods do not guarantee order of execution
- Future Methods do not support transaction control.
6 things to Remember while using future method :
Here are the few things we need to keep in mind while using future method in salesforce :
- Future method with the
@future
annotation must be static methods and this returns a void type only. - We can use max 50 future calls per Apex invocation.
- There is no guaranty future method would execute in the same order they were called.
- We can’t call a future method from another future method.
- Future methods can not be used in Visualforce controllers in
getMethodName()
,setMethodName()
, nor in the constructor. - Future method parameters/arguments must be primitive data types, arrays of primitive data types, or collections of primitive data types. However, future methods can’t take objects as parameter or arguments.
Future Method Performance Best Practices
As we know salesforce utilise queue-based framework for handling async process. Below best best practices can be followed to ensure salesforce org is efficiently using queue for this asynchronous processes.
- We must avoid to add large numbers of future methods to the Queue.
- We must ensure future methods execute as fast as possible.
- Test your future methods at scale.
- Consider using batch Apex instead of future methods to process large numbers of records.
If you would like to explore more follow salesforce documentation link of Future method in depth.
Thank you for the reading.