Sub topic 7
Why updates need a non-cacheable method
This chapter explains why the Apex method that saves a change cannot be the same method you wire for reading. It assumes you can write @AuraEnabled methods with SOQL, wire an Apex method in a component, and use async/await.
The rule
Salesforce lets you mark an Apex method cacheable=true. That setting does two things:
- The platform may store the method's result in a client-side cache and serve it again without calling the server.
- The platform forbids the method from changing data. A cacheable method cannot run DML (insert, update, delete, and similar statements).
The second rule follows from the first. If a method may be skipped on later calls, then a DML statement inside it might silently never run. Salesforce prevents that by blocking DML in cacheable methods.
You also need cacheable=true for @wire. A wire can only call a cacheable method. So reads and writes end up in two separate methods.
Two methods, two jobs
The controller from the previous chapters shows the split.
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
];
}
@AuraEnabled
public static void updateEmail(Id contactId, String email) {
Contact c = new Contact(Id = contactId, Email = email);
update as user c;
}
}| Method | Cacheable | Can run DML | How LWC calls it |
|---|---|---|---|
| `getContacts` | Yes | No | `@wire`, or imperatively |
| `updateEmail` | No | Yes | Imperatively only |
updateEmail has no cacheable=true, so the platform always sends the call to the server. LWC cannot wire it. You import it and call it like a function that returns a promise.
What happens when you try anyway
If you add DML to a cacheable method, the problem shows up in two places.
- Saving the Apex class can fail with an error saying that DML is not allowed in a cacheable method. Platform behavior here can differ, so treat a compile or runtime error as the expected signal.
- If you instead remove
cacheable=truefrom the read method, the wire stops working. The component reports that the method is not cacheable and no data arrives.
Neither fix is to loosen the rule. Keep the read method cacheable and add a separate write method.
How the two methods connect
Calling updateEmail changes the record on the server. It does not touch the client cache that holds the getContacts result. The wire keeps serving the old value until something refreshes it. That gap is why you call refreshApex after a successful save. The next chapter walks through the full sequence.
Choosing the method type
Use this check when you write a new Apex method for a component.
- Does the method change any record? If yes, leave
cacheableoff. - Does it only read data, and do you want to use
@wire? If yes, setcacheable=true. - Does it read data but must always return the latest value? Leave
cacheableoff and call it imperatively.
Exercise
A teammate marks updateEmail with @AuraEnabled(cacheable=true) so they can wire it and avoid writing an imperative call. Give two reasons this fails or is wrong.
Show answer
First, a cacheable method cannot perform DML, so the update as user c statement is not allowed. Second, even if a result were returned, the platform may serve cached results instead of calling the server, so a write could be skipped. The correct design is a non-cacheable updateEmail called imperatively, followed by refreshApex on the wired read.
Exercise
You need a method that returns the current number of open cases and must never show a cached value. Which setting do you use, and how do you call it from the component?
Show answer
Leave cacheable off, import the method, and call it imperatively. Wiring requires cacheable=true, so a method that must always hit the server cannot be wired.