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];
    }
}
  1. public class AccountController { — the class.
  2. @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.
  3. public static List<Account> getAccounts() { — the now-exposed method.
  4. return [SELECT Id, Name FROM Account]; — unchanged logic.
  5. } — closes the method.
  6. } — 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
  1. @AuraEnabled — marks the method.
  2. public static List<Account> getAccounts() { ... } — public and static — the correct shape.
  3. (blank line)
  4. @AuraEnabled — the same annotation.
  5. private List<Account> getAccounts() { ... } — will NOT work — private, and not static. An @AuraEnabled method must be public (or global, for methods exposed even outside the org) and static — the LWC framework calls it directly on the class, the same way TaxCalculator.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 + '%')];
    }
}
  1. public class AccountController { — the class.
  2. @AuraEnabled — the exposure annotation.
  3. public static List<Account> searchAccountsByName(String searchTerm) { — parameters work exactly like any other method (Module 9).
  4. 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.
  5. } — closes the method.
  6. } — 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.

Salesforce: ○ Not connected Connect
APEX

Execution Results

Run your code to see the result.

@AuraEnabled Methods Quiz

1. What two requirements must an @AuraEnabled method's signature satisfy?

Log in to submit the quiz and save your score.

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.