Annotation in SalesForce

Similar to Java annotations, an Apex annotation alters the way a function or class is utilized. This blog will help you learn more about Salesforce annotations as well as the types of annotations available.

Salesforce 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 Example:

global class My Class {
@ future
Public static void my Method (string a)
{
// long – running Apex code
}
}

Annotation in Salesforce - Table of Contents

If you want to enrich your career and become a professional in Salesforce CRM, then enroll in "Salesforce Online Training". This course will help you to achieve excellence in this domain.

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.

[ Check out How to Create Dashboard in Salesforce? ]

Future Annotation

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

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 Example:

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.

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.

Example:

@ isTest
        Private class My Class {
        @ isTest  static void test 1 () {
           }
        @ isTest static void test 2 () {
         }
 }

Example:

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

[ Related Article: Test Class in Salesforce ]

MindMajix Youtube Channel

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.
Check out Top Salesforce Interview Questions and Answers that help you grab high-paying jobs

@Remote Action Annotation

1. It provides support for Apex methods used in VF to be called via JavaScript, this process is often referred to as JavaScript Remoting.

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

3. To use JavaScript 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. The 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 JavaScript 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, the 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) (….)

Are you looking to get trained on Salesforce, we have the right course designed according to your needs. Our expert trainers help you gain the essential knowledge required for the latest industry needs. Join our Salesforce Certification Training program from your nearest city.

Salesforce Training ChennaiSalesforce Training DallasSalesforce Training BangaloreSalesforce Training Hyderabad, Salesforce Training Mumbai, Salesforce Training Delhi, Salesforce Training Noida, Salesforce Training New York, Salesforce Training Chicago, Salesforce Training Kolkata, Salesforce Training Gurgaon, Salesforce Training Pune.

These courses are equipped with Live Instructor-Led Training, Industry Use cases, and hands-on live projects. Additionally, you get access to Free Mock Interviews, Job and Certification Assistance by Certified Salesforce Trainers.

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 TrainingSalesforce Lightning Training
Salesforce Advanced Developer TrainingSalesforce Developer Training
Salesforce IoT TrainingSalesforce App Builder Certification Training
Salesforce AppExchange TrainingSalesforce Service Cloud Training
and many more..

Job Support Program

Online Work Support for your on-job roles.

jobservice

Our work-support plans provide precise options as per your project tasks. Whether you are a newbie or an experienced professional seeking assistance in completing project tasks, we are here with the following plans to meet your custom needs:

  • Pay Per Hour
  • Pay Per Week
  • Monthly
Learn MoreGet Job Support
Course Schedule
NameDates
Salesforce TrainingNov 23 to Dec 08View Details
Salesforce TrainingNov 26 to Dec 11View Details
Salesforce TrainingNov 30 to Dec 15View Details
Salesforce TrainingDec 03 to Dec 18View Details
Last updated: 29 Jul 2023
About Author

Arogyalokesh is a Technical Content Writer and manages content creation on various IT platforms at Mindmajix. He is dedicated to creating useful and engaging content on Salesforce, Blockchain, Docker, SQL Server, Tangle, Jira, and few other technologies. Get in touch with him on LinkedIn and Twitter.

read less
  1. Share:
Salesforce Articles