Jun 28, 2024
57 Views
Comments Off on Explore About Annotations In Salesforce
0 0

Explore About Annotations In Salesforce

Written by

Annotations are basically used to describe the role and behavior of method and the class introduced in Salesforce. Using Annotations in Salesforce is similar like using in Java. It has been defined by an initial (@) symbol followed by an appropriate keyword.

Here we have a list of description about the annotations used in Salesforce along with their examples :

@AuraEnabled : 

  • This annotation enables client- and server-side access to an Apex controller method.
  • This annotation makes methods available to your Lightning components.

Example :

global class DemoClass{
@AuraEnabled
Public static void demoMethod(String a)
{
//Running apex code
}
}

@Deprecated : Using this annotation one can identify methods, classes, exceptions, enums, interfaces, or variables that are not longer be referenced.

Example :

@deprecated
  global void demoMethod(String x) {   
}

Some Key Points :

  • Code of unmanaged package doesn’t contains deprecated keyword.
  • The methods and variables of webservice cannot be deprecated.
  • Enum can be deprecated but individual enum values cannot.
  • Interface can be deprecated but it’s individual methods cannot.
  • You cannot undeprecate something in Apex by removing the deprecated annotation, once you have released a package version where that item in Apex is deprecated.

@future annotations are used to identify the methods that executes asynchronously.

  • @future annotation methods must be static methods, and can only return a void type.
  • Methods with the future annotation cannot be used in the constructor and even not in the Visualforce controllers either in getMethodName or setMethodName methods.
  • You cannot call any future method from another future method.

Example :

global class DemoClass{
  @future 
  static void demoMethod(String x, Integer y) {
    System.debug('Method called with: ' + x + ' and ' + y);
    //long-running apex code
  }
}

@InvocableMethod annotation used to identify methods that can run as invocable actions.

  • The invocable method must be static and public or global, and its class must be an outer class.
  • Only one method in a class can have the InvocableMethod annotation.
  • Triggers can’t reference invocable methods.
  • Other annotations can’t be used with the InvocableMethod annotation.

Example :

with primitive data type

public class AccountQueryAction {
  @InvocableMethod(label='Get Account Names' description='Returns the list of account names corresponding to the specified account IDs.')
  public static List getAccountNames(List ids) {
    List accountNames = new List();
    List accounts = [SELECT Name FROM Account WHERE Id in :ids];
    for (Account account : accounts) {
      accountNames.add(account.Name);
    }
    return accountNames;
  }
}

with Sobject data type

public class AccountInsertAction {
  @InvocableMethod(label='Insert Accounts' description='Inserts the accounts specified and returns the IDs of the new accounts.')
  public static List insertAccounts(List accounts) {
    Database.SaveResult[] results = Database.insert(accounts);
    List accountIds = new List();
    for (Database.SaveResult result : results) {
      if (result.isSuccess()) {
        accountIds.add(result.getId());
      }
    }
    return accountIds;
  }
}

@InvocableVariable annotation use to represent variables which are further been used by invocable methods in a class. If someone is going to create his own custom class to provide input/output to its invocable method the best option for them to use @InvocableVariable annotation to make variables available for that method.

The three modifiers are available in @InvocableVariable annotation which are optional to use. we can use it as like below :

@InvocableVariable(label='yourLabel' description='yourDescription' required=(true | false))
  • label : Represents the label of variable. By default it is variable name.
  • description : Represents the description of variable. By default it is null.
  • required : For any required variable and if not specified then by default it is null. And, the value for output variables is ignored.

Thanks For Reading!!

Happy Salesforce!!

Article Categories:
Fashion
Priya Singh
https://thestarbiznews.com/

Expertise: Technology Trends, Startups, Digital Disruption Bio: Priya Singh is a tech-savvy millennial with a finger on the pulse of the ever-evolving digital landscape. A graduate of Stanford's Computer Science program and a former engineer at a Silicon Valley giant, Priya has a deep understanding of the technologies shaping our future. Her passion lies in demystifying complex tech jargon and exploring the disruptive potential of emerging trends, making her articles essential reading for anyone who wants to stay ahead of the curve. When not scouring the web for the latest tech tidbits, Priya enjoys traveling to off-the-beaten-path destinations and immersing herself in diverse cultures, always seeking new inspiration and insights.