Mindmajix

Annotation in SalesForce

Annotation in SalesForce

ANNOTATIONS

  • Annotations are used to give additional information to the compiler at the runtime
  • 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

For eg:

                             global class My Class {

                                  @ future

                             Public static void my Method (string a)

                             {

                                // long – running Apex code

                             }

                             }

→ Apex supports the following annotations:

  1. Deprecated
  2. Future
  3. Is Test
  4. Read only
  5. 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 you are refactoring code in managing packages as the requirement evolve.

  • New subscribers cannot see the deprecated elements, while the elements continue to function for existing subscribers and API integrations

                    Eg:

                         @deprecated

                             Public void my method (string a) {

                                        }

  • Unmanaged packages cannot contain code that uses the deprecated keyword
  • When something in Apex, or when a custom object is deprecated, all global access modifies that reference the deprecated identifier must also be deprecated. Any global method that uses the deprecated type in its signature, must also be deprecated
  • A deprecated item, such as a method or a class , can still be referenced internally by the package developer
  • Web service methods and variables can’t be deprecated
  • You can’t remove the deprecated annotation to undeprecate something in apex after you’ve released a package version where that item in Apex is deprecated
  • You can deprecate an enum , but you cannot deprecate individual enum values.

FUTURE ANNOTATION

  • Use the future annotation to identify a method that is executed asynchronously. When you specify future, the method executes when Salesforce has available resources
  • For e.g. you can use the future annotation when making an asynchronous Web service callout to an external service. Without the annotation, the web- service callout is made from the same thread that is executing the Apex code and no additional processing can occur until the callout is complete (synchronous processing)
  • 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

For eg:

    global class My future class {

    @ future

    static void My method (string a, integer i) {

    System. Debug (‘Method called with: ‘+a+’ and ‘+i);

    // do callout

    }

    }

  • How to specify if a method executes a callout:

              @ future (callout = true )

              public static void do callout from future () {

                      // Add code to perform callout

    }

  • You can specify (callout = false) to prevent a method from making callouts
  • It is used to make call outs
  • One common call out is Web service
  • All Web services are callouts

IS TEST ANNOTATION

  • Use the isTest annotation to define classes and individual methods that only contain code used for testing your application
  • The isTest annotation is similar to creating methods declared as a test method
  • Classes defined with the isTest annotation don’t count against your org limit of 2 MB for all apex code individual methods defined
  • Classes and methods defined as isTest can be either private or public..Classes defined as isTest must be top-level classes

Eg:

                      @ isTest

                      Private class My Class {

                      @ isTest  static void test 1 () {

                      }

                      @ isTest static void test 2 () {

                      }

                      }

  Eg:

 A Public test class that contains utility methods for test data creation:

                      @ isTest

                      public class TestUtil {

                      public static void create Test Accounts () {

                      }

                      public static void create Test Contacts () {

                      }

READ ONLY ANNOTATION

  • The @ReadOnly annotation allows you to perform unrestricted queries against the Force.com database , whereas 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, call to the System .schedule, calls to methods annotated with @future, and sending emails
  • The @read only annotation is available for web services and the schedulable interface. To use the @Read Only annotation, the top level request must be in the schedule execution or the Web service invocation
  • For e.g. if a VF(Visual Force) page calls a web service that contains the @Read-Only annotation, the request fails because VF is the top level request , not the web service
  • VF pages can call controller methods with the @Read Only annotation, and these methods will run with some relaxed restrictions.To increase other VF- specific limits, such as the size of a collection that can be used by an iteration component like < apex: page Block Table >, you can set the Read Only attribute on the <apex:page> tag to true.

@REMOTE ACTION ANNOTATION

  • It provides support for Apex  methods used in VF to be called via Java Script, this process is often referred to as Java Script Remoting
  • Methods with the Remote Action annotation must be both global and static
  • To use Java Script Remoting in a VF page, you add the request as a Java Script invocation, which must take the following form:

              [<Namespace>]<Controller>. <Method>(

               [params….,]

              <(Call back function>(result,event) {

               // callback function logic

               }, {escape: true});

  • The namespace is your organization’s name , this is required only if the class comes from an installed package
  • The Controller is the name of your Apex controller
  • Method is the name of the Apex method you have been calling
  • Params are the comma – separated list of parameters that your method holds
  • Call back function is the name of the Java Script function that will handle the response from the controller call back function and receives the status of the method call and the produces result as parameters
  • Escape specifies whether your Apex methods , response must be escaped (by default, true) or not (false)
  • In your controller, your Apex method declaration is processed with the @rotation annotation like this

              @remote action

              Global static string gets an item ID (string, object name) (….)

Enroll For Instructor Led Live SalesForce Training at Mindmajix

0 Responses on Annotation in SalesForce"

Leave a Message

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

Copy Rights Reserved © Mindmajix.com All rights reserved. Disclaimer.
Course Adviser

Fill your details, course adviser will reach you.