What is @AuraEnabled?
The @AuraEnabled annotation in Apex marks Apex methods and properties so they can be called from Lightning components — both Aura components and Lightning Web Components (LWC). Without this annotation, the Lightning framework cannot access Apex code from client-side JavaScript.
Where it’s used
Use @AuraEnabled for:
- Server-side Apex methods invoked from Aura components (controllers/helpers).
- Apex methods called from LWC using
@wireor imperative calls. - Exposing Apex properties to component markup when required.
Basic example
Expose a simple static Apex method so LWC or Aura can fetch data:
public with sharing class AccountService {
@AuraEnabled(cacheable=true)
public static List<Account> getAccounts() {
return [SELECT Id, Name FROM Account LIMIT 50];
}
}
Key points & SEO keywords
Static methods only: Methods annotated with @AuraEnabled must be static when called from client-side code. This is an Apex/LWC requirement.
cacheable=true for read-only calls: Add cacheable=true for methods that only return data (no DML). This enables client-side caching, improves performance, and allows usage with LWC @wire. Example: @AuraEnabled(cacheable=true).
Return types and parameters: Supported parameter and return types include primitives, sObjects, lists, maps, and Apex custom types that are serializable to JSON. Avoid complex transient state.
Security & sharing: Apex class-level with sharing or without sharing applies. Always enforce CRUD/FLS checks when exposing sensitive data to Lightning components to prevent overexposure.
Examples: imperative vs wired call in LWC
Imperative call from LWC JavaScript:
import getAccounts from '@salesforce/apex/AccountService.getAccounts';
getAccounts()
.then(result => { /* handle result */ })
.catch(error => { /* handle error */ });
Wired call (requires cacheable=true):
import { LightningElement, wire } from 'lwc';
import getAccounts from '@salesforce/apex/AccountService.getAccounts';
export default class AccountList extends LightningElement {
@wire(getAccounts) accounts;
}
Best practices
- Use
cacheable=truefor read-only Apex to leverage caching and reduce server calls. - Keep methods focused: one responsibility, limited parameters, and clear return types.
- Perform server-side validation and CRUD/FLS checks even if client-side validation exists.
- Avoid returning large datasets to the client; implement pagination or selective queries.
- Limit the number of
@AuraEnabledmethods to maintain maintainability and security review clarity.
When not to use @AuraEnabled
Do not annotate methods that are internal utility methods meant only for server-side Apex—only annotate when client-side Lightning code needs direct access.
Summary
@AuraEnabled is the bridge between Apex and Lightning (Aura + LWC). It enables secure, performant server calls from client-side components—especially when combined with cacheable=true and proper security checks. Mastering @AuraEnabled usage is essential for building scalable Lightning applications.








Leave a Reply