Advanced
25 min read
@AuraEnabled Methods
By the end of this lesson, you'll be able to:
- Mark an Apex method as callable from an LWC using @AuraEnabled
- Explain why the method must be public or global and static
- Recognize the annotation-based exposure pattern from Module 13
Prerequisites: "Why Apex Needs to Talk to the UI"
Marking a method as callable
public class AccountController {
@AuraEnabled
public static List<Account> getAccounts() {
return [SELECT Id, Name FROM Account];
}
}
public class AccountController {— the class.@AuraEnabled— exactly Module 13's annotation pattern — metadata telling the platform "this method may be called from a Lightning Web Component," without changing what the method's own code actually does.public static List<Account> getAccounts() {— the now-exposed method.return [SELECT Id, Name FROM Account];— unchanged logic.}— closes the method.}— closes the class.
Why public/global and static
@AuraEnabled
public static List<Account> getAccounts() { ... } // correct shape
@AuraEnabled
private List<Account> getAccounts() { ... } // will NOT work — private, and not static
@AuraEnabled— marks the method.public static List<Account> getAccounts() { ... }—publicandstatic— the correct shape.- (blank line)
@AuraEnabled— the same annotation.private List<Account> getAccounts() { ... }— will NOT work —private, and notstatic. An@AuraEnabledmethod must bepublic(orglobal, for methods exposed even outside the org) andstatic— the LWC framework calls it directly on the class, the same wayTaxCalculator.applyVat(...)was called directly in Module 10, never on a specific object instance.
Exposing a parameterized method
public class AccountController {
@AuraEnabled
public static List<Account> searchAccountsByName(String searchTerm) {
return [SELECT Id, Name FROM Account WHERE Name LIKE :('%' + searchTerm + '%')];
}
}
public class AccountController {— the class.@AuraEnabled— the exposure annotation.public static List<Account> searchAccountsByName(String searchTerm) {— parameters work exactly like any other method (Module 9).return [SELECT Id, Name FROM Account WHERE Name LIKE :('%' + searchTerm + '%')];— the LWC's JavaScript passes the argument when it calls into Apex, and everything from Module 20's bind-variable safety still applies unchanged.}— closes the method.}— closes the class.
Exercise
Add @AuraEnabled to expose this method to an LWC, correcting anything else needed to make it callable.
Show hint
It needs the annotation, and must be public and static.
@AuraEnabled Methods Quiz
My Notes
Log in to keep private notes on this lesson.
Questions about this lesson
No questions yet — be the first to ask.
Log in to ask a question about this lesson.
Summary
@AuraEnabled is the annotation that exposes an Apex method to be called from an LWC — the exact "attach metadata above a method" pattern Module 13 first introduced.