Sub topic 13
Calling the same methods from Aura components
This chapter assumes you have read the earlier chapters. You should know the rules for @AuraEnabled methods, the difference between cacheable and non-cacheable methods, and how errors travel as AuraHandledException. You should also be able to read basic Aura markup and JavaScript.
Same annotation, different caller
@AuraEnabled is not specific to Lightning Web Components (LWC). Aura components use the same annotation, and the same ContactController class works for both. You change nothing in the Apex.
The differences are on the client side:
- Aura has no
@wire. Every call is made by hand, and you build and enqueue an action yourself. - Aura components name the controller in markup instead of importing the method.
- Results and errors arrive in a callback, not a promise.
Connecting a component to the class
Set the controller attribute on aura:component. Every @AuraEnabled method in that class then becomes reachable from the component's JavaScript controller.
<aura:component controller="ContactController"
implements="flexipage:availableForRecordHome,force:hasRecordId">
<aura:attribute name="contacts" type="Contact[]" />
<aura:attribute name="error" type="String" />
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
<aura:iteration items="{!v.contacts}" var="c">
<p>{!c.Name}</p>
</aura:iteration>
<aura:if isTrue="{!v.error}">
<p>{!v.error}</p>
</aura:if>
</aura:component>The force:hasRecordId interface fills v.recordId on a record page.
Making the call
The client controller looks up the method with component.get("c.methodName"). Parameters go in setParams, and the keys must match the Apex parameter names exactly.
({
doInit: function (component, event, helper) {
var action = component.get("c.getContacts");
action.setParams({ accountId: component.get("v.recordId") });
action.setCallback(this, function (response) {
var state = response.getState();
if (state === "SUCCESS") {
component.set("v.contacts", response.getReturnValue());
component.set("v.error", "");
} else if (state === "ERROR") {
var errors = response.getError();
var message = errors && errors[0] && errors[0].message
? errors[0].message
: "Unknown error";
component.set("v.error", message);
}
});
$A.enqueueAction(action);
}
})Nothing is sent until $A.enqueueAction(action) runs. The framework may batch several queued actions into one server request.
Comparing the two clients
| Concern | LWC | Aura |
|---|---|---|
| Reference to the method | `import ... from '@salesforce/apex/...'` | `component.get("c.getContacts")` |
| Declarative read | `@wire` | Not available |
| Imperative call | Promise with `await` | `setCallback` plus `$A.enqueueAction` |
| Error message | `error.body.message` | `response.getError()[0].message` |
| Client caching | Handled by the wire service | Call `action.setStorable()` |
| Refresh after a change | `refreshApex` | Enqueue the action again |
Caching in Aura
Aura does not cache results by default. If you want a client-side cache, call action.setStorable() before enqueueing. This requires the method to be @AuraEnabled(cacheable=true), which means it must not perform DML. Aura has no refreshApex. To get fresh data after a change, enqueue a new action. Be aware that a storable action can return a cached value first and then call the callback again when the server response differs.
For writes such as createContact, use the same pattern without setStorable(). Read the AuraHandledException message from errors[0].message.
Common mistakes
- Mismatched parameter names.
setParams({ id: ... })fails when the Apex parameter isaccountId. The call returns an error and the method does not run. - Forgetting
enqueueAction. The action is built but never sent, so the callback never fires. - Calling a DML method as storable. A method that writes data must not use
cacheable=true, so it cannot be made storable. - Skipping the ERROR branch. Without it, failures are silent.
Exercise
An Aura component calls component.get("c.createContact") and passes setParams({ name: 'Smith', accountId: id }). The Apex signature is createContact(String lastName, Id accountId). What goes wrong, and how do you fix it?
Show answer
The key name does not match the Apex parameter lastName, so the method is not called with the intended value and the action fails. Change the key to lastName: setParams({ lastName: 'Smith', accountId: id }).