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.

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:
  1. Some governor limits are higher, like SOQL query and heap size limits.
  2. Prevent mixed DML error.
  3. The process runs in the background, asynchronously.
Point to remember:
  1. A future method must be a static method.
  2. It only returns void type.
  3. The future method can’t take object and sObjects as parameters.
  4. Parameters must be primitive data types like arrays of primitive data types, or collections of primitives data types.
  5. Future methods can’t invoke another future method.
  6. A future method may or may not be executing in the same order as it is called.
  7. Future methods can be called from a trigger.
  8. Future methods cannot be used in Visualforce page controller or constructor of a controller.
Governor Limits:
  1. The maximum number of future method invocations per a 24-hour period is 250,000
  2. No more than 200 method calls per Salesforce.com license per 24 hours
  3. 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:
  1. @future(limits='2xHeap') - Heap size limit is doubled (24 MB).
  2. @future(limits='3xHeap') - Heap size limit is tripled (36 MB).
  3. @future(limits='2xCPU') - CPU timeout is doubled (120,000 miliseconds) .
  4. @future(limits='3xCPU') - CPU timeout is tripled (180,000 milliseconds).
  5. @future(limits='2xSOQL') - Number of SOQL queries limit is doubled (400).
  6. @future(limits='3xSOQL') - Number of SOQL queries limit is tripled (600).
  7. @future(limits='2xDML') - Number of DML statements limit is doubled (300).
  8. @future(limits='3xDML') - Number of DML statements limit is tripled (450).
  9. @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.
  10. @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:
Conclusion:
I hope you would like this blog. If you have any confusion please comment for help.