Salesforce Apex Annotations

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.

Example:-

Public class classname @ future
Public static void methodname (String Name)

Apex supports the following annotations

  •  @ Deprecated
  •  @ Future
  •  @ Is test
  •  @ Read-only
  • @ Remote action

Deprecated Annotation:-

 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 we are refactoring code (clearing the code) in managed packages.

The following code shows a deprecated method. The same syntax can be used to deprecate classes, exceptions, nums, interfaces or variables

@depreacated
Public void methodname (string a) {}

Future Annotation

  •  Use the future annotation to identify methods that are executed asynchronously. When we specify future, method executes when salesforce had available resources.
  •  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.

Example:-

Public class classname { @future
Static void methodname (String a, integer I) { // Apex code

 Methods with the future annotation cannot take subjects or objects as arguments.  Methods with the future annotation cannot be used in visual force controllers in
either getmethodname or setmethodname methods, not in the constructor. The parameters specified must be primitive data types, arrays of primitive
data types, or collection of primitive data types.

Istest Annotation

Use the istest annotation to define classes or individual methods that only contain code used for testing application. The istest annotation is similar to creating methods declared a testmethod

Example:-

@istest
Public class Testclassname {static testmethod void methodname 1( )
{
// Apex code }
Static testmethod void methodname 2 ( ) {
// Apex code
}
}