Use the
future annotation to identify methods that are executed asynchronously. When you specify
future, the method executes when Salesforce has available resources.
For example, you can use the future annotation when making an asynchronous Web service callout to an external service. Without the annotation, the Web service callout is made from the same thread that is executing the Apex code, and no additional processing can occur until the callout is complete (synchronous processing).
Methods with the
future annotation must be static methods, and can only return a void type. The specified parameters must be primitive data types, arrays of primitive data types, or collections of primitive data types. Methods with the
future annotation cannot take sObjects or objects as arguments.
To make a method in a class execute asynchronously, define the method with the future annotation. For example:
1 | global class MyFutureClass { |
4 | static void myMethod( String a, Integer i) { |
5 | System .debug( 'Method called with: ' + a + ' and ' + i); |
To allow callouts in a future method, specify (callout=true). The default is (callout=false), which prevents a method from making callouts.
The following snippet shows how to specify that a method executes a callout:
2 | public static void doCalloutFromFuture() { |
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.
No comments:
Post a Comment