An Apex annotation modifies the way a method or class is used, similar to annotations in Java.
Annotations are defined with an initial @ symbol, followed by the appropriate keyword. To add an annotation to a method, specify it immediately before the method or class definition. For example:
global class MyClass {@futurePublic static void myMethod(String a) { //long-running Apex code } }
Use the future annotation to identify methods that are executed asynchronously. When you specify future, the method executes when Salesforce.com 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 script, 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.
To make a method in a class execute asynchronously, define the method with the future annotation. For example:
global class MyFutureClass {@future static void myMethod(String a, Integer i) {System.debug('Method called with: ' + a + ' and ' + i); //do callout, other long running code } }
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 }
You can specify (callout=false) to prevent a method from making callouts.
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.
Methods with the future annotation have the following limits:
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.
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 getContentAsPDF PageReference methods cannot be used in methods with the future annotation.
For more information about callouts, see Invoking Callouts Using Apex.
Classes and methods defined as isTest must be declared as private. For example:
@isTest private class MyTest { // Methods for testing }
Classes defined as isTest cannot be interfaces or enums.
A class defined as isTest can only be invoked using the Force.comrunTests()API call, or from the Salesforce.com user interface (using the Run Tests button). You cannot call it from another class or trigger.
Use the deprecated annotation to identify methods, classes, exceptions, enums, interfaces, or variables that can no longer be referenced in subsequent releases of the managed package in which they reside. This is useful when you are refactoring code in managed packages as the requirements evolve. New subscribers cannot see the deprecated elements, while the elements continue to function for existing subscribers and API integrations.
The following code snippet shows a deprecated method. The same syntax can be used to deprecate classes, exceptions, enums, interfaces, or variables.
@deprecated // This method is deprecated. Use myOptimizedMethod(String a, String b) instead. public void myMethod(String a) { }
Note the following rules when deprecating Apex identifiers:
For more information about package versions, see Developing Apex in Managed Packages.