Asynchronous apex

views
image-1

Annotations in Salesforce


Annotations are known as modifiers in Apex or as well in Java. An annotation modifies the way of any class and method and provides different functionality for a specific situation. Annotations are defined with the help of @ special symbol at the starting of any class or method. In Salesforce there are several kinds of annotations are available for different types of situations.


Ex.  

global class MyClass {

     @annotaion

     Public static void myMethod(String a)

     {

          //long-running Apex code

     }

}

 

All the main kinds of annotations are given below-

  • Apex Rest Annotation
  • AuraEnabled Annotation
  • Deprecated Annotation
  • Future Annotation
  • InvocableMethod Annotation 
  • InvocableVariable Annotation 
  • isTest Annotation
  • JSONAccess Annotation
  • NamespaceAccessible Annotation
  • ReadOnly Annotation
  • RemoteAction Annotation
  • SuppressWarnings Annotation  
  • TestVisible Annotation
  • @ReadOnly
  • @RestResource 
  • @HttpDelete
  • @HttpGet
  • @HttpPatch
  • @HttpPost
  • @HttpPut

Apex Rest Annotations:-

Apex REST Annotations are very helpful at the time of making rest callouts from any third-party system or API. Without using rest callout it is not possible to make RESTFul web services. So when we modify any class with these annotations then it is work as a RESTFul web service. Apex REST Annotations are further divided into many categories they are given below:-

 

AuraEnabled Annotations:-

AuraEnabled annotation plays an important role when we call any method from our lighting web component. It enables server-side and client-side access to our methods. But here one important thing is that with the help of this annotation we will be capable to modify only our methods, not our class. Also with the help of this annotation, we are able to improve our performance by using cachable true at the time or deceleration.

Ex. AuraEnabled (cacheable = true)

 

Future Annotations:-

When you decorate any method using the future annotation, your method will execute when required resources are available. The main use of this annotation is that, making asynchronous Web Service Callout to an external service. Once the process is coming in progress then no additional processing can occur. When you modify any method with future annotation, then your method must be static and the return type is void. And all primitive data types parameters are allowed only Objects and subjects are not allowed as a parameter.

Ex. global class MyFutureClass {

@future 

  static void getOutputMethod(String a, Integer i) {

    System.debug(‘Your expession is: ‘ + a + ‘ and ‘ + i);

    // Perform long-running code

  }

}

JsonAccess Annotations:-

For controlling the instance of an Apex class, we use @JsonAccess Annotation at Apex Class level. In this kind of annotation, there are two types of parameters are available one is serializable and another is deserializable. These parameters enforce the context in which the apex allows serialization and deserialization.

Here some predefined values are available for these parameters they are given below:-

never

sameNamespace

smaePackage

always

InvocableMethod Annotation:-

The word invocable means, not changeable. So when we modify and method with InvocableMethod Annotation then the method executes as an invocable action. Invocable methods have dynamic Input and Output and support describe calls. Invocable Method are working with the primitive data type, specific sObject data type, and generic sObject data type. Only one method in a class is decorated with the invocable method that is working.

InvocableVariable Annotations:-

So when you identify variables with the InvocableVariable Annotations, then your variables are used by InvocableMethod. These variables are used as input or output variables to an invocable method, you can annotate individual class member variables to make them available to the method.

Ex. @InvocableVariable(label=‘yourLabel’ description=‘yourDescription’ required=(true | false))

 

All these modifiers are optional.

isTest Annotation:-

Testing of our developed application is also an important part of development.  Before decorating any class or method with isTest annotations make sure that only these methods contain only code for testing. IsTest annotations work with several modifiers within parentheses and for separating each modifier we use blank space.

Ex. @isTest

private class MyTestClass {

 // Methods for testing

   @isTest static void test1() {

      // Implement test code

   }

   @isTest static void test2() {

      // Implement test code

   }

}

NamespaceAccessible Annotation:-

For making public apex in a package available to another package, we use @namespaceAccessible. If we do not use this annotation then Apex classes, methods, interfaces, etc are defined in one package are not accessible to the other package. If we use declared any piece of code is as global then it is always available across all namespaces and does not need any kind of annotation.

 

ReadOnly Annotations:-

The @ReadOnly comment permits you to perform less prohibitive questions against the Lightning Platform data set by expanding the constraint of the quantity of returned lines for a solicitation to 100,000. Any remaining cutoff points actually apply. The explanation hinders the accompanying activities inside the solicitation: DML tasks calls to System, schedule, and enqueued asynchronous Apex occupations.

RemoteAction Annotation:-

When we want to use any Apex method in Visualforce Page, then decorate these methods with RemoteAction annotations. We are calling these Apex methods via JavaScript.

Ex. <apex:page controller=”MyController” extension=”MyExtension”>

SupressWarning Annotation:-

Actually, @SupressWarnings annotation does nothing in the Apex program but is used for providing essential information to the third-party integrated tools. Hence it is also important at the time of integration.

TestSetup Annotations:-

At the time of testing, we need some prerequiste(records) for testing. So when we decorate any methods with @testSetup annotations it used for creating record for tetsing. One important thing we also keep in mind, at the time of declaration test class do not accept any argument and no return type.

TestVisible Annotations:-

TestVisible annotation is responsible for accessing private and protected variables and methods of one test class to another test class. This kind of annotations does not change the visiblity of member if accessed by non-test classes.

Ex. public class TestVisibleExample {

    // Private member variable

    @TestVisible private static String recordName = ‘Name’;

    // Private method

    @TestVisible private static void updateData(String name) {

        // Do something

    }

}

 

Comments

Avatar

Leave a Reply

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