A future method runs in the background, asynchronously. You can call a future method for executing long-running operations, such ascallouts to external Web services or any operation you’d like to run in its own thread, on its own time. You can also make use of future methods to isolate DML operations on different sObject types to prevent the mixed DML error. 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. A benefit of using future methods is that some governor limits are higher, such as SOQL query limits and heap size limits
global class FutureClass
{
@future
public static void myFutureMethod()
{
// Perform some operations
}
}
NOTE :-
1) Methods with the future annotation must be static methods 2) can only return a void type 3) The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types 4) Methods with the future annotation cannot take sObjects or objects as arguments. 5) You can invoke future methods the same way you invoke any other method. However, a future method can’t invoke another future method 6) No more than 50 method calls per Apex invocation 7) Asynchronous calls, such as @future or executeBatch, called in a startTest, stopTest block, do not count against your limits for the number of queued jobs 8) The maximum number of future method invocations per a 24-hour period is 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater 9) To test methods defined with the future annotation, call the class containing the method in a startTest(), stopTest() code block. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously
IMP:- The reason why sObjects can’t be passed as arguments to future methods is because the sObject might change between the time you call the method and the time it executes. In this case, the future method will get the old sObject values and might overwrite them. To work with sObjects that already exist in the database, pass the sObject ID instead (or collection of IDs) and use the ID to perform a query for the most up-to-date record. The following example shows how to do so with a list of IDs
FUTURE METHOD CONSIDERATIONS
Remember that any method using the future annotation requires special consideration because the method does not necessarily execute in the same order it is called.
Methods with the future annotation cannot be used in Visualforce controllers in either getMethodName or setMethodName methods, nor in the constructor.
You cannot call a method annotated with future from a method that also has the future annotation. Nor can you call a trigger from an annotated method that calls another annotated method.
The getContent and getContentAsPDFPageReference methods cannot be used in methods with the future annotation
As you are new in salesforce developer Forum some more information for you keep this community clean. It's a Best pratice to close every question after marking the Best Answer ( select Best Comment which helped you ) if your issue / question will resolve. So that if some one has the same issue that post can help other also.
As a common practice, if your question is answered, please choose 1 best answer. Additionaly you can give every answer a thumb up if that answer is helpful to you.
Hope this help you
https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_invoking_future_methods.htm
If this helps, Dont forget to Mark the answer.
Regards,
Anil
1) http://amitsalesforce.blogspot.in/2015/02/future-methods-in-salesforce.html
A future method runs in the background, asynchronously. You can call a future method for executing long-running operations, such ascallouts to external Web services or any operation you’d like to run in its own thread, on its own time. You can also make use of future methods to isolate DML operations on different sObject types to prevent the mixed DML error. 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. A benefit of using future methods is that some governor limits are higher, such as SOQL query limits and heap size limits
NOTE :-
1) Methods with the future annotation must be static methods
2) can only return a void type
3) The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types
4) Methods with the future annotation cannot take sObjects or objects as arguments.
5) You can invoke future methods the same way you invoke any other method. However, a future method can’t invoke another future method
6) No more than 50 method calls per Apex invocation
7) Asynchronous calls, such as @future or executeBatch, called in a startTest, stopTest block, do not count against your limits for the number of queued jobs
8) The maximum number of future method invocations per a 24-hour period is 250,000 or the number of user licenses in your organization multiplied by 200, whichever is greater
9) To test methods defined with the future annotation, call the class containing the method in a startTest(), stopTest() code block. All asynchronous calls made after the startTest method are collected by the system. When stopTest is executed, all asynchronous processes are run synchronously
IMP:-
The reason why sObjects can’t be passed as arguments to future methods is because the sObject might change between the time you call the method and the time it executes. In this case, the future method will get the old sObject values and might overwrite them. To work with sObjects that already exist in the database, pass the sObject ID instead (or collection of IDs) and use the ID to perform a query for the most up-to-date record. The following example shows how to do so with a list of IDs
FUTURE METHOD CONSIDERATIONS
Let us know if this will help you
Thanks
Amit Chaudhary
It's a Best pratice to close every question after marking the Best Answer ( select Best Comment which helped you ) if your issue / question will resolve. So that if some one has the same issue that post can help other also.
As a common practice, if your question is answered, please choose 1 best answer.
Additionaly you can give every answer a thumb up if that answer is helpful to you.