Sub topic 2
Rules for AuraEnabled methods
This chapter assumes you can write basic Apex (classes, SOQL, DML) and that you know how a Lightning web component imports and calls a method. It covers the rules the platform enforces on a method before a component can call it, and the errors you get when you break them.
The rules at a glance
A method that a component calls must meet these requirements:
| Rule | What it means |
|---|---|
| Static | The method is declared `static`. The platform never creates an instance of your controller for the call. |
| Public or global | The method uses `public` or `global`. A `private` or `protected` method is not visible to the component. |
| Accessible class | The class is `public` or `global`, so the component can reach it. |
| Unique name | No other `@AuraEnabled` method in the class shares its name. Overloading is not supported for these methods. |
| Serializable data | Parameters and return values must be types the platform can convert to and from JSON. |
The diagram shows the order of checks you can apply when a call fails.
Static, and public or global
A component never instantiates your class. It asks the platform to run one named method, so the method must belong to the class itself, not to an object of it.
public with sharing class AccountController {
@AuraEnabled(cacheable=true)
public static List<Account> getRecentAccounts() {
return [
SELECT Id, Name
FROM Account
WITH USER_MODE
ORDER BY CreatedDate DESC
LIMIT 10
];
}
}Use public for code inside your own namespace. Use global only when you ship a managed package and need the method visible outside it. The class needs the same visibility as the method.
No overloading
Apex normally allows two methods with the same name and different parameters. For @AuraEnabled methods this fails, because the component identifies a method only by class and name. Parameters are matched by name, not by type or position. Give each variant its own name:
public with sharing class AccountSearchController {
@AuraEnabled(cacheable=true)
public static List<Account> findByName(String name) {
String pattern = '%' + String.escapeSingleQuotes(name) + '%';
return [
SELECT Id, Name
FROM Account
WHERE Name LIKE :pattern
WITH USER_MODE
LIMIT 20
];
}
@AuraEnabled(cacheable=true)
public static List<Account> findByIndustry(String industry) {
return [
SELECT Id, Name
FROM Account
WHERE Industry = :industry
WITH USER_MODE
LIMIT 20
];
}
}An overloaded pair such as two find methods would be ambiguous to the component, so use distinct names like the ones above.
Parameters are matched by name
The JavaScript side passes a single object. Each key must match an Apex parameter name exactly, including case. Order does not matter.
import { LightningElement } from 'lwc';
import findByName from '@salesforce/apex/AccountSearchController.findByName';
export default class AccountSearch extends LightningElement {
accounts;
async handleSearch() {
// The key "name" must match the Apex parameter "name".
this.accounts = await findByName({ name: 'Acme' });
}
}If the key is accountName, the Apex method receives null for name. This is a common cause of "my query returns nothing".
cacheable=true limits what a method may do
Adding cacheable=true lets the client cache the result and lets the method work with @wire. In return, the method must be read-only. A cacheable method that performs DML fails at runtime. Put writes in a separate non-cacheable method.
Common errors
- The component compiles, but the call reports that the method is not found. The method is probably not
static, or the class or method is notpublic. - Saving the Apex class fails with a message about duplicate or overloaded names. Rename one of the methods.
- A parameter arrives as
null. Check the key spelling in the JavaScript object. - A cacheable method throws when it runs. Look for DML, or for a call to code that performs DML.
Exercise
A teammate writes two methods named getAccounts in one controller. One takes String industry, the other takes Integer maxRows. Both are @AuraEnabled. What is the problem, and how do you fix it?
Show answer
Overloaded @AuraEnabled methods are not supported, because the component identifies a method by class and name only. Rename them, for example getAccountsByIndustry and getAccountsLimited, and update the imports in the components that call them.
Exercise
A component calls getContacts({ accountID: this.recordId }), but the Apex parameter is Id accountId. What does the method receive?
Show answer
It receives null for accountId. Parameter keys must match the Apex names exactly, including case, so the key should be accountId.