Sub topic 1
Exposing Apex to LWC with @AuraEnabled(cacheable=true)
Prerequisites
This chapter assumes you already know the following:
- Basic Apex syntax and SOQL, including bind variables such as
:accountId. - How a Lightning Web Component is structured: an HTML template, a JavaScript class, and
@apiproperties. - Basic JavaScript, including ES modules.
- Access to a Salesforce org or scratch org where you can deploy Apex.
Why Apex needs an annotation
A Lightning Web Component runs in the browser. Apex runs on the Salesforce server. A component cannot call an Apex method unless the method is explicitly exposed to it.
The @AuraEnabled annotation does that. It marks a method as callable from client-side code. Adding cacheable=true goes one step further. It tells the platform that the method only reads data, so the client may store its result and reuse it.
The @wire decorator requires cacheable=true. A method without it can still be called imperatively, but it cannot be wired. Later chapters cover both approaches.
A complete example
This method returns the contacts for 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
];
}
}Each part of this method has a purpose:
| Part | Purpose |
|---|---|
| `public with sharing class` | Applies the running user's record-sharing rules to the query. |
| `@AuraEnabled(cacheable=true)` | Exposes the method to LWC and allows the result to be cached. |
| `public static` | Exposed methods must be static and public or global. |
| `Id accountId` | A parameter the component supplies when it calls the method. |
| `WITH USER_MODE` | Enforces the user's object permissions and field-level security. |
| `LIMIT 50` | Keeps the response small, which matters for something sent to the browser. |
What cacheable=true requires
A cacheable method must not change data. The platform does not let a cacheable method perform DML (insert, update, delete). Calling one from a cacheable method fails at runtime with an error such as "Too many DML statements" or an uncommitted-work style error, depending on the operation. Do not rely on side effects in these methods.
This restriction exists because the client may return a stored result and skip the server entirely. If the method wrote data, those writes would sometimes not happen.
A method that creates or updates records should use @AuraEnabled without cacheable=true. You then call it imperatively.
public with sharing class ContactWriter {
@AuraEnabled
public static void renameContact(Id contactId, String lastName) {
Contact c = new Contact(Id = contactId, LastName = lastName);
update as user c;
}
}How the call flows
Common mistakes
- Missing
static. An exposed method that is not static does not work with LWC. - Omitting
cacheable=trueand then using@wire. The component fails with an error saying the method is not cacheable. - Returning too much. Everything returned is sent to the browser. Select only the fields the component needs and limit the row count.
- Skipping security.
@AuraEnableddoes not check permissions for you. Usewith sharingandWITH USER_MODE(orWITH SECURITY_ENFORCED).
Check your understanding
Exercise
A teammate writes @AuraEnabled(cacheable=true) on a method that updates a Contact record, and then wires it in a component. What goes wrong, and how should the method be changed?
Show answer
A cacheable method must not perform DML, and the client may return a stored result without running the method at all, so the update would fail or be skipped. Remove cacheable=true and call the method imperatively. Reading data stays in a separate cacheable method that the component can wire.
Exercise
Which two keywords or clauses in the getContacts example enforce record sharing and field-level security respectively?
Show answer
with sharing on the class enforces record sharing. WITH USER_MODE in the SOQL query enforces object permissions and field-level security.