Hello friends, In this tutorial, we are going to learn about @future annotation in salesforce. Basically a future method for executing long-running operations in Salesforce that helps you to perform such task that take more time to execute.
Governor Limits:
I hope you would like this blog. If you have any confusion please comment for help.
After Completing this blog, you will able to understand:
- What is the future method in salesforce?
- How to create future annotation method in apex class?
- How to run long-running process asynchronously in salesforce?
So Let’s start
What is the future method or @future annotation in Salesforce?
A future method runs in the background, asynchronously. When you specify future, the method executes when Salesforce has available resources. You can call a future method for executing long-running operations, such as callouts to external Web services or any operation you’d like to run in its own thread, on its own time. In Salesforce, there are several methods to run the process asynchronously. @future annotation is one of the ways. It runs the process in a separate thread. You can also make use of future methods to isolate DML operations on different sObject types to prevent the mixed DML error. Methods with the future annotation must be static methods, and can only return a void type. Each future method is queued and executes when system resources become available. That way, the execution of your code doesn’t have to wait for the completion of a long-running operation.
To define a future method with future annotation, as follows:
global class FutureClass{ @future public static void myFutureMethod(){ // Perform some operations } }
Benefits of the future method:
- Some governor limits are higher, like SOQL query and heap size limits.
- Prevent mixed DML error.
- The process runs in the background, asynchronously.
Point to remember:
- A future method must be a static method.
- It only returns void type.
- The future method can’t take object and sObjects as parameters.
- Parameters must be primitive data types like arrays of primitive data types, or collections of primitives data types.
- Future methods can’t invoke another future method.
- A future method may or may not be executing in the same order as it is called.
- Future methods can be called from a trigger.
- Future methods cannot be used in Visualforce page controller or constructor of a controller.
- The maximum number of future method invocations per a 24-hour period is 250,000
- No more than 200 method calls per Salesforce.com license per 24 hours
- A maximum number of methods with future annotation allowed per Apex invocation is 50.
To allow callouts in a future method, specify (callout=true). The default is (callout=false), This prevents a method from making callouts.
global class FutureClass{ @future(callout=true) public static void getAccounts(String acctName){ // Perform a callout to an external service } }
Limit Modifiers
Some limit modifiers that are supported to @future annotation methods are:
- @future(limits='2xHeap') - Heap size limit is doubled (24 MB).
- @future(limits='3xHeap') - Heap size limit is tripled (36 MB).
- @future(limits='2xCPU') - CPU timeout is doubled (120,000 miliseconds) .
- @future(limits='3xCPU') - CPU timeout is tripled (180,000 milliseconds).
- @future(limits='2xSOQL') - Number of SOQL queries limit is doubled (400).
- @future(limits='3xSOQL') - Number of SOQL queries limit is tripled (600).
- @future(limits='2xDML') - Number of DML statements limit is doubled (300).
- @future(limits='3xDML') - Number of DML statements limit is tripled (450).
- @future(limits='2xDMLRows') - Number of records that were processed as a result of DML operations is doubled (20,000). Includes Approval.process and Database.emptyRecycleBin operations.
- @future(limits='3xDMLRows') - Number of records that were processed as a result of DML operations is tripled (30,000). Includes Approval.process and Database.emptyRecycleBin operations.
Note: You can specify only one
higher limit per future method.
See also:
- @TestSetup method in Salesforce
- @TestVisible annotation in Salesforce
- With Sharing and Without Sharing in Salesforce
I hope you would like this blog. If you have any confusion please comment for help.
3 Comments
Limits anotations is possible today?
ReplyDeleteGood to read this blog! I have enrolled into salesforce learning and happy to read this topic, now its more clear to me, I read this topic currently in my class. Thanks for sharing!
ReplyDeletethank you anna
DeletePost a Comment