+ Start a Discussion
Lucia CheungLucia Cheung 

Quick Start: Lightning Components: Retrieve a Contact List

Hi,

Cannot save the Controller, please help.

https://trailhead.salesforce.com/projects/quickstart-lightning-components/steps/quickstart-lightning-components4
Tej PalTej Pal
Hi Lucia

Use below things:


MyContactListController.apxc​
public class MyContactListController {
    @AuraEnabled
    public static List<Contact> getContacts() {
        return [Select Id, Name, Email, Title, Phone From Contact];
    }
}
MyContactList.cmp
<aura:component controller="MyContactListController">
    //This event handler call the myAction client-side controller to handle initialization
    <aura:handler name="init" action="{!c.myAction}" value="{!this}" />
    //Make the list of contacts accessible to the component
    <aura:attribute name="contacts" type="Contact[]" />
    <ul>
        // iterate over the line list of contacts and display their names
<aura:iteration items="{!v.contacts}" var="contact">
	<li class="minli"> <h3>{!contact.Name}</h3> </li>
</aura:iteration>
	</ul> 
</aura:component>
MyContactListController.js
({
	myAction : function(component, event, helper) {
		var action = component.get("c.getContacts");
        action.setCallback(this, function(data) {
		component.set("v.contacts", data.getReturnValue());
});
		$A.enqueueAction(action);

	}
})
If this answers your question mark Best Answer it as solution and then hit Like!
Lucia CheungLucia Cheung
So, where to add:
<aura:handler name="init" value="{!this}" action="{!c.myAction}" />
 
Tej PalTej Pal
Create lightening component from developer console.
User-added image

If this answers your question mark Best Answer it as solution and then hit Like!
Suraj TripathiSuraj Tripathi

Hi Lucia Cheung,

Tej is already added.

User-added image

Lucia CheungLucia Cheung
Hi Tej and Suraj,

Modified but still got the error:
Challenge Not yet complete... here's what's wrong: 
The component does not include a 'aura:handler'
Yehiel ZAFRANY -Yehiel ZAFRANY -
Hi,

I have been able to complete the challenge successfully, however I don't see the contact list in the component!!
User-added image
Yehiel ZAFRANY -Yehiel ZAFRANY -
I found the solution: 

Instead of:

<lightning:datatable data="{! v.Contacts }" columns="{! v.Columns }" hideCheckboxColumn="true"/>
 
It should be:   (keyField is required)
<lightning:datatable data="{!v.Contacts }" columns="{!v.Columns }" hideCheckboxColumn="true" keyField="id"   />    
 
Swiderski, ErikSwiderski, Erik
This was exactly the problem. Adding keyField="id" worked for me.
cloud47cloud47
@YehielZafrany  Where is the code that relates to the lightning data table, please.  I can't find that in the trailhead components we were asked to create.
Siva N R ChamarthySiva N R Chamarthy
Hello Everyone, I am doing the steps given in the Lightning Component Developer Guide  Winter19  Quick Start Guide example. I dont see the Contact List not getting populated . I could see the debug log shows the Init has retrieved 5 records as I included of "LIMIT 5 " but still thecontact list on the Lightening Tab is empty .Can you please  let me know if it works 
Siva N R ChamarthySiva N R Chamarthy
Hello Lucia Cheung, This is an old thread, but am also getting a similar issue. did you manage to find a resolution?