Thursday, September 6, 2018

Custom object type attribute in Salesforce lightning

Attribute can have a type corresponding to custom object.


<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>




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






Step 3:

Create Apex controller:

public class Accountobjcontroller{

    public static List<employees__c> getempdetails() {

        List<employees__c> Acc =

                [SELECT Id, Name  FROM employees__c];

        return acc;

Create lightning application:

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

No comments:

Post a Comment