Newer Version Available

This content describes an older version of this product. View Latest

AuraEnabled Annotation

The AuraEnabled annotation enables Lightning components to access Apex methods and properties.
The AuraEnabled annotation is overloaded, and is used for two separate and distinct purposes.
  • Use @AuraEnabled on Apex class static methods to make them accessible as remote controller actions in your Lightning components.
  • Use @AuraEnabled on Apex instance methods and properties to make them serializable when an instance of the class is returned as data from a server-side action.
  • Don’t mix-and-match these different uses of @AuraEnabled in the same Apex class.
  • Only static @AuraEnabled Apex methods can be called from client-side code. Visualforce-style instance properties and getter/setter methods aren’t available. Use client-side component attributes instead.
  • You can't use the @NamespaceAccessible Apex annotation for an @AuraEnabled Apex method referenced from an Aura component.

Important

Component Security

In Apex, every method that is annotated @AuraEnabled should be treated as a web service interface. That is, the developer should assume that an attacker can call this method with any parameter, even if the developer's client-side code does not invoke the method or invokes it using only sanitized parameters. For more information, see the Secure Coding Guide.

Caching Method Results

To improve runtime performance, set @AuraEnabled(cacheable=true) to cache the method results on the client. To set cacheable=true, a method must only get data. It can’t mutate data.

Marking a method as storable (cacheable) improves your component’s performance by quickly showing cached data from client-side storage without waiting for a server trip. If the cached data is stale, the framework retrieves the latest data from the server. Caching is especially beneficial for users on high latency, slow, or unreliable connections such as 3G networks.

To cache data returned from an Apex method for any component with an API version of 44.0 or higher, you must annotate the Apex method with @AuraEnabled(cacheable=true). For example:

1@AuraEnabled(cacheable=true)
2public static Account getAccount(Id accountId) {
3    // your code here
4}

Prior to API version 44.0, to cache data returned from an Apex method, you had to call setStorable() in JavaScript code on every action that called the Apex method. For API version of 44.0 or higher, you must mark the Apex method as storable (cacheable) and you can get rid of any setStorable() calls in JavaScript code. The Apex annotation approach is better because it centralizes your caching notation for a method in the Apex class.

Client-side storage is automatically configured in Lightning Experience and the Salesforce mobile app. A component shouldn’t assume a cache duration because it may change as we optimize the platform.

Note

Using Continuations

Use the Continuation class in Apex to make a long-running request to an external Web service.

Continuations use the @AuraEnabled annotation. Here are the rules for usage.

@AuraEnabled(continuation=true)
An Apex controller method that returns a continuation must be annotated with @AuraEnabled(continuation=true).
@AuraEnabled(continuation=true cacheable=true)
To cache the result of a continuation action, set cacheable=true on the annotation for the Apex callback method.

There’s a space, not a comma, between continuation=true cacheable=true.

Note