Sub topic 1
What @AuraEnabled does and why components need it
A Lightning web component runs in the user's browser. Apex runs on Salesforce servers. They are separate runtimes, and a component cannot call an Apex method by name the way one Apex class calls another. The @AuraEnabled annotation is what lets the two sides talk. This chapter explains what it does, what it does not do, and how a call travels from a component to Apex and back. Later chapters cover method rules, caching, call styles, errors, security, and tests in detail.
Prerequisites
This chapter assumes you already know:
- Basic Apex syntax: classes, static methods, SOQL queries, and DML statements such as
insert. - Basic Lightning Web Components: a component's HTML template, its JavaScript class, and decorators such as
@api. - Salesforce objects, fields, and 15 or 18 character record IDs.
You do not need to know how @wire or refreshApex work internally. This chapter shows one example of each and explains what you need to read it.
The problem: a boundary between browser and server
Apex can query and change data. JavaScript in a component can show data and respond to clicks. To build a useful component, you often need both. For example, a component might list the contacts of the current account, or create a contact when the user clicks Save.
The browser cannot run Apex directly. It must send a request to the server, which runs the Apex and sends a response back. Salesforce does not let the browser call any Apex it likes. That would expose every public method in your org to the internet. Instead, a method must be explicitly marked as callable from the client. @AuraEnabled is that mark.
What the annotation does
@AuraEnabled is an Apex annotation placed directly above a method. It has three effects.
- It registers the method as a client-callable action. The Lightning framework can now route a request from a component to this method.
- It lets a component import the method. In a Lightning web component, you write
import getContacts from '@salesforce/apex/ContactController.getContacts';. Without the annotation, this import fails when the component is saved or deployed. - It controls what the client may see. On a class member such as a field of a wrapper class,
@AuraEnableddecides whether that member is included in the data sent to the browser. Members without it are left out.
The name is historical. Aura was the first component framework on Salesforce, and the annotation was named for it. Lightning web components reuse the same server-call mechanism, so the same annotation serves both.
A first example, end to end
Here is a minimal Apex controller with one annotated method.
public with sharing class ContactController {
@AuraEnabled(cacheable=true)
public static List<Contact> getContacts(Id accountId) {
return [
SELECT Id, Name, Email
FROM Contact
WHERE AccountId = :accountId
WITH USER_MODE
ORDER BY Name
LIMIT 50
];
}
}The method is public static, takes an account ID, and returns a list of contacts. The annotation is what makes it reachable from a component. The cacheable=true option and the WITH USER_MODE clause are explained in the sections below.
Now the component that uses it:
import { LightningElement, api, wire } from 'lwc';
import getContacts from '@salesforce/apex/ContactController.getContacts';
export default class ContactList extends LightningElement {
@api recordId;
@wire(getContacts, { accountId: '$recordId' })
contacts;
}Three things connect the two files:
- The import path has the form
@salesforce/apex/ClassName.methodName. The class and method names must match exactly. - The parameter object
{ accountId: '$recordId' }uses the Apex parameter name as its key. The leading$tells the framework thatrecordIdis a reactive property, so the call runs again whenever its value changes. - The result lands in the
contactsproperty. Because it is a wired property, it holds an object withdataanderrorfields.
What happens during a call
Between the click or page load and the data on screen, several steps happen. Knowing them helps when you debug.
In words:
- The component asks for
ContactController.getContactswith the parameters as JSON. - The framework looks up the method. If it is not annotated, or the user's profile or permission sets do not grant access to the class, the call fails.
- The framework converts the JSON parameters into Apex types and runs the method.
- The return value is converted into JSON. Only
@AuraEnabledmembers of returned classes are included. - The component receives the data, or an error object if the method threw an exception.
This is a network round trip. It is slower than reading a property, which is one reason the later chapters on caching and on Lightning Data Service matter.
The two forms of the annotation
You will use two forms most of the time.
| Form | Meaning | Typical use |
|---|---|---|
| `@AuraEnabled` | Callable from the client. May perform DML. Results are not cached. | Creating, updating, or deleting records. Reads that must always be fresh. |
| `@AuraEnabled(cacheable=true)` | Callable from the client. Must not perform DML. Results can be cached on the client. | Read-only queries, and anything used with `@wire`. |
A cacheable method promises the framework that it only reads. That promise is what allows the client to reuse a previous result instead of making a new request. If a cacheable method tries to change data, the call fails. Chapters on cacheable methods and refreshApex cover the details and the stale-data problem that comes with caching.
Another option, continuation=true, is for long-running callouts to external services. It is outside the scope of this tutorial.
What the annotation does not do
Several misunderstandings cause bugs and security gaps.
- It does not enforce security. The annotation makes a method reachable. It says nothing about which records or fields the caller may see. That is decided by your class declaration (
with sharing) and by your query (WITH USER_MODE). A method that omits both can leak data to users who should not see it. - It does not validate input. Parameters come from the browser, and a user can send any value for them. Treat every parameter as untrusted input, as you would in a web service.
- It is not required for Apex-to-Apex calls. Other Apex code can call the method like any static method. The annotation matters only for the client boundary.
- It does not make the class itself visible. The user still needs access to the Apex class through a profile or a permission set. This is a common cause of "works for me as admin, fails for the user" reports.
Common errors
These are the problems you will most likely meet in your first week with the annotation.
| Symptom | Likely cause |
|---|---|
| The component fails to save or deploy with an import error | The method is missing `@AuraEnabled`, or the class or method name in the import path is misspelled. |
| The call works for admins but fails for other users | The user lacks access to the Apex class. Add it to a permission set. |
| A wired call never reruns after a change | The parameter was passed without the `$` prefix, so it is not reactive. |
| A cacheable method fails when it writes data | Cacheable methods must be read-only. Remove `cacheable=true` and call the method imperatively. |
| A parameter arrives as `null` in Apex | The key in the JavaScript parameter object does not match the Apex parameter name. |
Choosing whether you need it at all
Not every read or write needs a custom Apex method. Lightning Data Service and the lightning-record-form family of components can load and save single records with no Apex at all, and they handle caching and permissions for you. Reach for @AuraEnabled when you need logic the platform components cannot give you, such as queries across several objects, complex filters, custom validation, or multi-record operations. A later chapter compares the options in detail.
Summary of the model
- Apex runs on the server, components run in the browser, and every call between them is a network request.
@AuraEnabledmarks a static method as callable from components, and marks which members of a returned object the browser may see.cacheable=truemeans read-only and cacheable. Omitting it allows DML and disables client caching.- The annotation controls reachability only. Sharing, field-level security, and input checks are your responsibility.
Exercise
A teammate adds @AuraEnabled to a public static method that queries all Opportunity records with no with sharing keyword and no WITH USER_MODE. The component works in testing. Name two reasons this is still a problem.
Show answer
First, the annotation only makes the method reachable, so it does not apply the user's sharing rules or permissions. Without with sharing and WITH USER_MODE, the method can return records and fields the calling user should not see. Second, the user still needs access to the Apex class, so a component that works for an admin may fail for ordinary users until the class is added to their profile or a permission set. The method also trusts whatever parameters the browser sends, so inputs should be validated.
Exercise
Given the import import getContacts from '@salesforce/apex/ContactController.getContacts';, what two pieces of information does the path contain, and what happens if the Apex method has no @AuraEnabled annotation?
Show answer
The path contains the Apex class name (ContactController) and the method name (getContacts), separated by a dot after @salesforce/apex/. If the method has no @AuraEnabled annotation, the framework does not expose it to the client, so the import fails and the component cannot be saved or deployed with that reference.