• USA : +1 973 910 5725
  • INDIA: +91 905 291 3388
  • info@tekslate.com
  • Login

Annotations in Salesforce

Annotations in Salesforce

  • 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.

EX:-

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

Apex supports the following annotations

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

Deprecated Annotations

  • 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) {}

Note:

Unmanaged packages cannot contain code that uses the deprecated keyword.

We can not remove the deprecated annotation to undeprecated something in apex after we have released a package version where that item in apex is deprecated.

Note:

Unmanaged package editable by developer and installer cannot be upgraded. In managed package certain components are locked and changes are not allowed.

Future Annotations

  • 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.

EX: 

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

Note

  • 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 get method name or set method name methods, not in the constructor.
  • The parameters specified must be primitive datatypes, arrays of primitive datatypes, or collection of primitive datatypes.
Interested in Salesforce? Enroll now FREE demo on Salesforce Training.

Is test Annotations

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

Note:

Classes defined with the istest annotation don’t count against organization limit of 2MB for all apex code individual methods defined with the istest annotation do count against organization limits.

Classes and methods defined as istest can be either Private or Public classes defined as istest must be top level classes.

The following is the example of a public test class that contains two test methods.

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

Is Test(see All Date= true) Annotation

For Apex code using the IsTest(see All Date= true) Annotation to grant test classes and individual test methods access to all data in the organization, including pre-existing data that the test did not create.

Test methods doesn’t have access by default to pre-existing data in the Organization If a test class is defined with the IsTest(See All Data= True) Annotation, this annotation applies to all its test methods whether the test methods are defined with the @IsTest annotation or the test Method Keyword.

The following example shows how to define a test class with the IsTest(seeAllDate=true) annotation all the test methods in this class have access to all data in the organization.

@isTest( SeeAllDate = true)
 Public class classNameTest {
 Static testmethod void method1(){
 // Apex code
 }
 }

Read-only Annotation

The @ReadOnly annotation allows you to perform unrestricted queries against the Force.com database. All other limits still apply. It’s important to note that this annotation, while removing the limit of the number of returned rows for a request, blocks you from performing the following operations within the request: DML operations, calls to System.schedule, calls to methods annotated with @future, and sending emails.

The @ReadOnly annotation is available for Web services and the Schedulable interface. To use the @ReadOnly annotation, the top level request must be in the schedule execution or the Web service invocation. For example, if a Visualforce page calls a Web service that contains the @ReadOnly annotation, the request fails because Visualforce is the top level request, not the Web service.

Remote action Annotation

The RemoteAction annotation provides support for Apex methods used in Visualforce to be called via JavaScript. This process is often referred to as JavaScript remoting.

Methods with the RemoteAction annotation must be static and either global or public.

A simple JavaScript remoting invocation takes the following form.

[ namespace. ]controller.method(
         [ parameters..., ]
         callbackFunction,
         [configuratio]
);

You might like

Loops in Salesforce

Salesforce Tutorials

Salesforce Interview Questions

Summary
Review Date
Reviewed Item
Useful Annotations To Read
Author Rating
5

“At TekSlate, we are trying to create high quality tutorials and articles, if you think any information is incorrect or want to add anything to the article, please feel free to get in touch with us at info@tekslate.com, we will update the article in 24 hours.”

0 Responses on Annotations in Salesforce"

Leave a Message

Your email address will not be published. Required fields are marked *

Site Disclaimer, Copyright © 2016 - All Rights Reserved.