Thursday, September 6, 2018

Custom object type attribute in Salesforce lightning

Attribute can have a type corresponding to custom object.

Syntax:

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

Let's take an example of custom object employee.

STEP 1: 
Create Lightning Component.

<aura:component controller="Employeeobjcontroller">  
//Apex controller:Employeeobjcontroller

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

     

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

    <aura:iteration var="acc" items="{!v.empobj}">  
// Iterator fetching value of custom object

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

    </aura:iteration>



</aura:component>

STEP 2:

Create Javascript controller.

({

    getemp: function(component){

        var action = component.get("c.getempdetails");
  //Calling controller method

        action.setCallback(this, function(response){

            var state = response.getState();

            if (state === "SUCCESS") {

                component.set("v.empobj", response.getReturnValue()); 
//Setting value in Custom object type attribute

            }

        });

     $A.enqueueAction(action);

    }


})


Step 3:

Create Apex controller:

public class Accountobjcontroller{

    @AuraEnabled
    
    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">
    <c:Firstlightningcomponent/>
</aura:application>



No comments:

Post a Comment