Sub topic 8
Writing Apex for site components
This chapter assumes you can build a basic Lightning web component and write Apex and SOQL. You should also know how sharing and profiles work, and you should have a component exposed in Experience Builder. The goal is an Apex controller that returns only the data the current site user may see.
Why the controller matters
A site component calls Apex over the network. A visitor can call any @AuraEnabled method your component can call, with any arguments. Apex is therefore a trust boundary, and the controller has to enforce access. It can't rely on the component to do that.
A controller for the case list
The CaseList component from the earlier chapters wires a method named getMyCases. Here is the controller:
public with sharing class CaseListController {
@AuraEnabled(cacheable=true)
public static List<Case> getMyCases(Integer maxRows) {
return [
SELECT Id, CaseNumber, Subject, Status
FROM Case
WITH USER_MODE
ORDER BY CreatedDate DESC
LIMIT :maxRows
];
}
}Each part of this class does a specific job:
with sharingmakes the query respect record-level sharing rules for the running user.WITH USER_MODEalso enforces object permissions and field-level security. If the site user can't readSubject, the query throws an exception.@AuraEnabledmakes the method callable from JavaScript.cacheable=trueallows@wireto call the method and lets the client cache the result.LIMIT :maxRowsuses a bind variable, so the value can't change the query structure.
Cacheable versus non-cacheable methods
Cacheable methods must not change data. The platform rejects DML inside them. Use cacheable=true for reads that you call with @wire. For anything that inserts, updates, or deletes, leave the flag off and call the method imperatively.
| Need | Annotation | How JavaScript calls it |
|---|---|---|
| Read data, reactive to properties | `@AuraEnabled(cacheable=true)` | `@wire` |
| Read data on a click | `@AuraEnabled(cacheable=true)` | Imperative call |
| Create or change records | `@AuraEnabled` | Imperative call |
How a request flows
Common errors
- Missing class access. The site user's profile, or the guest user profile, needs access to the Apex class. Without it, the call fails with an "insufficient access" error. Grant access in the profile or in a permission set assigned to the user.
- Null or oversized arguments.
LIMIT :maxRowsfails ifmaxRowsis null. Validate and cap the value before you query, for exampleMath.min(maxRows == null ? 5 : maxRows, 50). - Using
without sharingto fix an access error. This hides the problem and exposes records. Fix the sharing rule or permission instead. - Stale data after an update. Cached results don't refresh on their own after an imperative DML call. Use
refreshApexon the wired result.
Exercise
A site component calls a method annotated @AuraEnabled(cacheable=true) that inserts a record. What happens, and how do you fix it?
Show answer
The platform rejects DML in a cacheable method, so the call fails. Remove cacheable=true from the method and call it imperatively from JavaScript, because wire adapters require cacheable methods.
Exercise
Name two things WITH USER_MODE enforces that with sharing alone does not.
Show answer
Object-level permissions and field-level security. with sharing applies only record-level sharing rules.