Retrieve a Contact List
Follow Along with Trail Together
Want to follow along with an expert as you work through this step? Take a look at this video, part of the Trail Together series.
(This clip starts at the 8:00 minute mark, in case you want to rewind and watch the beginning of the step again.)
Retrieve the Contacts
Now add an event handler that calls a JavaScript function to retrieve data from Salesforce.
- In the Developer Console, click the MyContactList.cmp tab.
- In the button panel on the right, click Controller.
- In the body of the myAction JavaScript function, add the following code, beginning on a new line:This Value Provider can return a reference to a client- or server-side method. You use it to call methods from the server, as well as to call methods in the myComponentController.js file. There are other Value Providers as well; please check the documentation above to learn more about those.
var action = component.get("c.getContacts"); action.setParams({ recordId: component.get("v.recordId") }); action.setCallback(this, function(data) { component.set("v.Contacts", data.getReturnValue()); }); $A.enqueueAction(action);
The JavaScript function calls thegetContacts
method using the value providerc.
asc.getContacts
to access the Apex controller that you created earlier, passing therecordId
of the current Account. It then populates the attribute namedContacts
with the results. - Select File | Save.
- Click the MyContactList.cmp tab and add the following markup below the last
aura:attribute
tag:<aura:handler name="init" value="{!this}" action="{!c.myAction}" />
Anaura:handler
listens for a specific event and then executes the function identified by the action attribute in the component's controller file. Thename
attribute identifies the event, in this caseinit
which is the event that fires as the component is loaded onto the page. - Select File | Save.