Introduction
Annotations in Salesforce are powerful tools that allow developers to control how Apex code interacts with other parts of the platform and external systems. They simplify the development process by providing clear instructions to the Salesforce runtime on how to handle specific methods, properties, or classes. Whether it’s enabling server-side methods for Lightning components, running asynchronous processes, or integrating with APIs, annotations play a crucial role. Understanding these annotations is essential for writing clean, efficient, and scalable code. In this guide, we’ve explored the most commonly used annotations and their practical applications.
@AuraEnabled
This annotation makes Apex methods or properties accessible to Lightning components (Aura and LWC). It acts as a bridge between client-side JavaScript and server-side Apex.
Example:
@AuraEnabled
public static List<Account> getAccounts() {
return [SELECT Id, Name FROM Account];
}
See also: Understanding @AuraEnabled Annotation
@Future
Used to run methods asynchronously. Commonly used for long-running processes or operations that need to run in the background.
Example:
@Future
public static void processRecords(List<Id> recordIds) {
// Async processing logic
}
See also: Salesforce Apex Annotations
@InvocableMethod
Allows an Apex method to be called from declarative tools like Flow and Process Builder. Only one method per class can have this annotation.
Example:
@InvocableMethod
public static void updateAccounts(List<Account> accounts) {
update accounts;
}
@InvocableVariable
Used to define variables that can be set in Flow or Process Builder when using an @InvocableMethod.
Example:
public class AccountWrapper {
@InvocableVariable
public String accountName;
}
See also: Top 10 interviews questions on Salesforce Annotations with coding examples
@RemoteAction
Makes Apex methods accessible to Visualforce pages for server-side processing. Useful for AJAX calls in Visualforce.
Example:
@RemoteAction
public static String getAccountName(String accountId) {
return [SELECT Name FROM Account WHERE Id = :accountId].Name;
}
@RestResource
Used to define an Apex class as a RESTful web service. Enables external applications to interact with Salesforce.
Example:
@RestResource(urlMapping='/accounts/*')
global with sharing class AccountService {
@HttpGet
global static List<Account> getAccounts() {
return [SELECT Id, Name FROM Account];
}
}
@HttpGet
Used inside a @RestResource class to handle GET requests in a REST API.
Example:
@HttpGet
global static Account getAccountById() {
String accountId = RestContext.request.params.get('id');
return [SELECT Id, Name FROM Account WHERE Id = :accountId];
}
See also: Strings in Salesforce Apex
@HttpPost
Used inside a @RestResource class to handle POST requests in a REST API, typically for creating or updating records.
Example:
@HttpPost
global static String createAccount(String accountName) {
Account acc = new Account(Name = accountName);
insert acc;
return acc.Id;
}
@TestSetup
Allows the creation of reusable test data for multiple test methods in a test class. Helps to save SOQL query limits during testing.
Example:
@TestSetup
static void setupTestData() {
insert new Account(Name = 'Test Account');
}
@IsTest
Used to define an Apex class or method as a test. Ensures the method or class doesn't count against org limits.
Example:
@IsTest
private class AccountTest {
@IsTest
static void testGetAccounts() {
// Test logic
}
}
@TestVisible
Makes private methods or variables visible to test classes for testing purposes.
Example:
public class AccountService {
@TestVisible
private static Integer counter = 0;
}
See also: Salesforce apex programming examples
@WithSharing
Enforces the sharing rules of the current user when the Apex class executes.
Example:
public with sharing class AccountController {
public List<Account> getAccounts() {
return [SELECT Id, Name FROM Account];
}
}
@WithoutSharing
Ensures the class executes without enforcing sharing rules of the current user.
Example:
public without sharing class AccountController {
public List<Account> getAllAccounts() {
return [SELECT Id, Name FROM Account];
}
}
@deprecated
Marks a method or class as deprecated, indicating it should no longer be used and may be removed in future versions.
Example:
@Deprecated
public static void oldMethod() {
// Deprecated logic
}
See also: What is a Map class in Salesforce Apex?
@ReadOnly
Ensures the method executes in a read-only context, allowing for higher governor limits on queries.
Example:
@ReadOnly
public static List<Account> getLargeDataSet() {
return [SELECT Id, Name FROM Account];
}
Conclusion
Salesforce annotations are the backbone of many functionalities within the platform, bridging the gap between declarative and programmatic solutions. By leveraging annotations like @AuraEnabled, @Future, and @RestResource, developers can create robust, flexible applications tailored to business needs. Proper use of these annotations ensures code maintainability, security, and optimal performance. As Salesforce continues to evolve, mastering annotations remains a critical skill for any developer. With the knowledge of these tools, you can build smarter, more integrated Salesforce applications.
Top comments (0)