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.
Apex supports the following annotations:
S.No | Apex Annotation Name | Description | Example |
1 | @isTest | Defines classes that only contain code used for testing your application. These classes don’t count against the total amount of Apex used by your organization. | @isTest private class MyTest { // Methods for testing } |
2 | @isTest( OnInstall=true) | Defines a test class or test method that executes on package installation | @isTest(OnInstall=true) private class TestClass { } |
3 | @isTest( SeeAllData=true) | Defines a test class or test method that has access to all data in the organization including pre-existing data that the test didn’t create. The default is false. | @isTest(SeeAllData=true) private class TestClass { } |
4 | @deprecated | Identifies methods, classes, exceptions, enums, interfaces, or variables that can no longer be referenced in subsequent releases of the managed package in which they reside
| @deprecated public void limitedShelfLife() { } |
5 | @readOnly | Defines methods that can perform queries unrestricted by the number of returned rows limit for a request
| @readOnly private void doQuery() { } |
6 | @remoteAction
| Defines Apex controller methods that JavaScript code can call from a Visualforce page via JavaScript remoting. The method must be static and either public or global. | @remoteAction global static String getId( String s) { } |
7 | @restResource | Identifies a class that is available as a REST resource. The class must be global. The urlMapping parameter is your resource’s name and is relative to https://instance. salesforce.com/services/ apexrest/.
| @restResource(urlMapping= ‘/Widget/*’) global with sharing class MyResource() { } |
8 | @httpGet, @httpPost, @httpPatch, @httpPut, @httpDelete
| Defines a REST method in a class annotated with @restResource that the runtime invokes when a client sends an HTTP GET, POST, PATCH, PUT, or DELETE respectively. The methods defined with any of these annotations must be global and static.
| @httpGet global static MyWidget__c doGet() { } @httpPost global static void doPost() { } @httpDelete global static void doDelete() { } |
9 | @future | Identifies methods that are executed asynchronously
| 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 } } |