Annotations in Salesforce

Ratings:
(4)
Views:0
Banner-Img
  • Share this blog:

Table of Contents

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.

EX:

Public class classname @ future Public static void method name (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 a managed package, certain components are locked, and changes are not allowed.

Future Annotations

  • Use future annotation to identify methods that are executed asynchronously. When we specify the future, the 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 the 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 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 data types, arrays of primitive data types, or collection of primitive datatypes.
Inclined to build a profession as SalesForce Developer? Then here is the blog post on, explore SalesForce Training

Is test Annotations

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

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 don'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

 

You liked the article?

Like : 0

Vote for difficulty

Current difficulty (Avg): Medium

Recommended Courses

1/15

About Author
Authorlogo
Name
TekSlate
Author Bio

TekSlate is the best online training provider in delivering world-class IT skills to individuals and corporates from all parts of the globe. We are proven experts in accumulating every need of an IT skills upgrade aspirant and have delivered excellent services. We aim to bring you all the essentials to learn and master new technologies in the market with our articles, blogs, and videos. Build your career success with us, enhancing most in-demand skills in the market.