Future Annotation
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).
1) Methods with the future annotation must be static
methods, and can only return a void type.
2) 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.
3) 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.
A benefit of using future methods is that
some governor limits are higher, such as SOQL query limits and heap
size limits.
To make a method in a class execute asynchronously, define the
method with the future annotation.
For example:
global class FutureClass { @future public static void myFutureMethod() { // Perform some operations } }
Specify (callout=true) to allow callouts in a future method. Specify (callout=false) to prevent a method
from making callouts.
The following snippet shows how to specify that a method executes
a callout:
@future (callout=true) public static void doCalloutFromFuture() { //Add code to perform callout }
Methods with the future annotation have the following limits:
1) No more than 50 method calls per Apex invocation.
2) 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. This limit is for your entire organization and is shared with all
asynchronous Apex: Batch Apex, Queueable Apex, scheduled Apex, and future methods.
The licenses that count toward this limit are full Salesforce user licenses or
Force.com App Subscription user licenses.
Testing Future Methods
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.
For our example, this is how the test class looks.
@isTest
private class MixedDMLFutureTest {
@isTest static void test1() {
User thisUser = [SELECT Id FROM User WHERE Id = :UserInfo.getUserId()];
// System.runAs() allows mixed DML operations in test context
System.runAs(thisUser) {
// startTest/stopTest block to run future method synchronously
Test.startTest();
MixedDMLFuture.useFutureMethod();
Test.stopTest();
}
// The future method will run after Test.stopTest();
// Verify account is inserted
Account[] accts = [SELECT Id from Account WHERE Name='Acme'];
System.assertEquals(1, accts.size());
// Verify user is inserted
User[] users = [SELECT Id from User where username='mruiz@awcomputing.com'];
System.assertEquals(1, users.size());
}
}
Comments
Post a Comment