Blog

Annotation in SalesForce

  • (4.0)
  •   |   617 Ratings

Annotation

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.

  1. 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) {  }
     
  2. Unmanaged packages cannot contain code that uses the deprecated keyword
  3. 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
  4. A deprecated item, such as a method or a class , can still be referenced internally by the package developer
  5. Web service methods and variables can’t be deprecated
  6. 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
  7. You can deprecate an enum , but you cannot deprecate individual enum values.

Future Annotation

1. Use the future annotation to identify a method that is executed asynchronously. When you specify future, the method executes when Salesforce has available resources.

Enroll For Instructor Led Live SalesForce Training at Mindmajix.

2. 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)

3. Methods with the future annotation must be static methods, and can only return a void type

4. 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
    }
    }

5. How to specify if a method executes a callout:

                   @ future (callout = true )
                    public static void do callout from future ()
                    {
                        // Add code to perform callout
                     }

6. You can specify (callout = false) to prevent a method from making callouts

7. It is used to make call outs

8. One common call out is Web service

9. All Web services are callouts

Checkout SalesForce Sample Resumes

Is Test Annotation

1. Use the isTest annotation to define classes and individual methods that only contain code used for testing your application

2. The isTest annotation is similar to creating methods declared as a test method

3. Classes defined with the isTest annotation don’t count against your org limit of 2 MB for all apex code individual methods defined

4. 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 () {
            }

Checkout Salesforce Interview Questions

Read Only Annotation

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

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

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

4. 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 tag to true.

Checkout SalesForce Tutorial

@Remote Action Annotation

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

2. Methods with the Remote Action annotation must be both global and static

3. To use Java Script Remoting in a VF page, you add the request as a Java Script invocation, which must take the following form:

               []. (
              [params….,]
              <(Call back function>(result,event) {
               // callback function logic
               }, {escape: true});

4. The namespace is your organization’s name , this is required only if the class comes from an installed package

5. The Controller is the name of your Apex controller

6. Method is the name of the Apex method you have been calling

7. Params are the comma – separated list of parameters that your method holds

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

9. Escape specifies whether your Apex methods , response must be escaped (by default, true) or not (false)

10. 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) (….)

Related Articles:
Shallow copy and Deep Copy in Cloud Computing - Salesforce
SalesForce Integration and Environment in Cloud Computing

Mindmajix offers different Salesforce certification training according to your desire with hands-on experience on Salesforce concepts

Salesforce Administration Training Salesforce Lightning Training
Salesforce Advanced Developer Training Salesforce Developer Training
Salesforce IoT Training Salesforce App Builder Certification Training
Salesforce AppExchange Training Salesforce Service Cloud Training
and many more..  

Popular Courses in 2018

Get Updates on Tech posts, Interview & Certification questions and training schedules