Total Pageviews

Tuesday, December 6, 2011


An annotation's dictionary meaning is an explanatory note added to a text. Similarly in Apex an Annotation
qualifies a method,class, suggesting how it can be used, invoked or qualifies it by stating if its a test method or
not. There are some 5 keywords which are used in conjunction with @ to serve as annotations of method , class , etc. definition.
Syntax of annotation:-
@ {Keyword name}
All Annotations are defined by prefixing the annotation keywords with @ symbol.

Apex supports the following annotations:

Deprecated - suggest the class ,exceptions, enums, interfaces,variables or methods in question is no more in use or will not be distributed in futher releases if its part of a package.The deprecated components dont show up in future installation of a package but reside normally for older installation (in which the element was not
deprecated when installed)

Code Exampleusing deprecated  :-
  // This method is deprecated. Use myOptimizedMethod(String a, String b) instead.
  public void myMethod(String a) {

Some points to ponder about deprecated annotation:-

  • Unmanaged packages cannot contain code that uses the deprecated keyword.
  • When something in Apex, or when a custom object is deprecated, all global access modifiers that reference the deprecated identifier must also be deprecated. 
  • webService methods and variables cannot be deprecated.
  • You can deprecate an enum but you cannot deprecate individual enum values.
  • You can deprecate an interface but you cannot deprecate individual methods in an interface.
  • You can deprecate an abstract class but you cannot deprecate individual abstract methods in an abstract class.
  • You cannot remove the deprecated annotation to undeprecate something in Apex after you have released a package version where that item in Apex is deprecated.

Use of future annotation along with a method suggests that it can be called asynchronously.Methods with the future annotation must be static methods, and can only return a void type.

Example of future annotation:-

global class MyFutureClass {

  static void myAsyncMethod(String a, String b) {
    System.debug('Method called with: ' + a + ' and ' + b);
    //do callout, other long running code

Also one can additionally specify or qualify whether the method can make a callout or not by adding 

Points to be careful about with @future:-
1. The getContent and getContentAsPDFPageReference methods cannot be used in methods with the future annotation.
2. You cannot call a method annotated with future from a method that also has the future annotation.

This Annotation tells the system that code enclosed in a method or class is aimed at testing one's application or in other words giving coverage to classes and triggers.

Example IsTest annotation:-

private class MyTestClass {

   // Methods for testing
   @isTest static void test1() {
      // Implement test code creating , editing or deleting data

   @isTest static void test2() {
      // Implement test code

Things to be careful about IsTest annotation:-
1. Classes defined as isTest can't be interfaces or enums.
2.Methods of a public test class can only be called from a running test, that is, a test method or code invoked by a test method, and can't be called by a non-test request.

Also one can additionally specify or qualify whether the class/method will be executed during package installation by adding (OnInstall=true/false).

The @ReadOnly annotation allows us to execute unrestricted queries against the Force.comdatabase, but prevents us from making DMLoperations calls to class.schedule, asynchronous calls that is calls to methods qualified with @future and sending emails.This is used to work with large datasets without data manipulation.

Used in Javascript remoting.The RemoteAction annotation provides support for Apex methods used in Visualforce to be called via Javascript.

Things to bear in mind about RemoteAction annotation:-
1. Methods with the RemoteAction annotation must be global and static methods.

Basic syntax of remoting (RemoteAction) :-

global static String getItemId(String objectName) { ... }

Also we have Apex REST Annotations
Six new annotations have been added that enable you to expose an Apex class as a RESTful Web service