Sub topic 1
What @AuraEnabled Does and Why Components Need It
Prerequisites
This chapter assumes you can already:
- Write Apex classes, methods, SOQL queries and DML statements.
- Build a basic Lightning web component (LWC) with a template, a JavaScript class and decorators such as
@api. - Work with Salesforce objects, fields and record IDs.
The problem: the browser cannot call Apex directly
A Lightning component runs in the user's browser. Apex runs on the Salesforce server. The browser has no direct way to run an Apex method, and Salesforce does not expose every method to the client by default. That would let any script call any code in your org.
The @AuraEnabled annotation is how you opt a method in. It tells the platform that this method may be called from a Lightning component. Methods without it cannot be called from the client.
A minimal example
This controller exposes one method that returns the contacts of an account.
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
];
}
}An LWC then imports the method and connects it to a property.
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;
}The import path has the form @salesforce/apex/ClassName.methodName. The framework turns that path into a JavaScript function you can use with @wire or call directly.
What happens at runtime
When the component runs, the framework sends a request to the server, runs your Apex method, and serializes the return value to JSON. The component receives it as a plain JavaScript object or array.
Three consequences follow from this model:
- Parameters and return values cross a network boundary. They must be types the framework can serialize.
- Every call is a server round trip. That affects performance, so the choice of call style matters (later chapters cover this).
- The client controls the arguments. Treat them as untrusted input, the same as in any web API.
The annotation is also a security surface
Marking a method @AuraEnabled makes it reachable by any user who can run the component, and in some cases by users who can reach the class through other means. Two habits follow:
- Declare the class
with sharingso record sharing rules apply. - Enforce object and field permissions, for example with
WITH USER_MODEin SOQL, as the example above does.
Later chapters cover these in detail. For now, remember that exposing a method is a decision with security consequences.
The `cacheable` option
The annotation takes one optional parameter, cacheable. @AuraEnabled(cacheable=true) marks a method as read-only. Salesforce may serve its results from a client-side cache, and the method can be used with @wire. A cacheable method cannot perform DML. Without the parameter, the method is non-cacheable. It can modify data but cannot be used with @wire. The next chapters return to this choice.
Common errors
| Symptom | Likely cause |
|---|---|
| The import fails to resolve | Wrong class or method name, or the method lacks `@AuraEnabled` |
| An error says the method is not found or not accessible | The method is not `static`, or it is not `public` or `global` |
| `@wire` reports an error about the method | The method is not `cacheable=true` |
| A cacheable method fails when it runs | It performs DML, which is not allowed in cacheable methods |
| Data looks outdated after a save | A cached result is being reused (see the `refreshApex` chapter) |
Exercise
A component imports @salesforce/apex/ContactController.getContacts, but the build reports that the import cannot be resolved. Name two things to check in the Apex class.
Show answer
Check that the method has the @AuraEnabled annotation, and that it is a static method declared public or global. Also confirm that the class name and method name in the import path match the Apex exactly.