Thursday, September 6, 2018

How to use an attribute of type custom object in a lightning component?

An attribute can have a type corresponding to a custom object.


<aura:attribute name="empobj" type="employees__c[]"/>  

Let's take an example of an array of custom object employees.

STEP 1: Create a Lightning Component.

<aura:component controller="Employeeobjcontroller">  

    <aura:attribute name="empobj" type="employees__c[]"/>  

    <lightning:button label="Fetch Employee details" onclick="{!c.getemp}"/>

    <aura:iteration var="acc" items="{!v.empobj}">  

        <p>{!acc.Name} </p>



STEP 2: 
Create a Javascript controller.


    getemp: function(component){

        var action = component.get('c.getempdetails');

        action.setCallback(this, function(response){

            var state = response.getState();

            if (state === "SUCCESS") {

                component.set("v.empobj", response.getReturnValue()); 






Step 3: Create an Apex controller.

public class Employeeobjcontroller{

    public static List<employees__c> getempdetails() {

        List<employees__c> Acc =

                [SELECT Id, Name  FROM employees__c];

        return acc;

STEP 4: Create lightning application.

Create lightning application.
<aura:application extends="force:slds">