Thursday, September 6, 2018

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

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


<aura:attribute name="AccountObj" type="Account" description="Standard object type attribute"/>

Let's take an example of an Array of the account object.

Step 1:Create Lightning Component.

<aura:component controller="Accountobjcontroller">  
    <aura:attribute name="Accountobj" type="Account[]"/>    
    <lightning:button label="Fetch Accounts" onclick="{!c.getaccount}"/>
    <aura:iteration var="acc" items="{!v.Accountobj}">
        <p>{!acc.Name} </p>

Step2: Create a Javascript controller.


    getaccount: function(component){

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

  //Calling controller method

        action.setCallback(this, function(response){

            var state = response.getState();

            if (state === "SUCCESS") {

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

//Setting value in account object type attribute






Step 3: Create an Apex controller.

public class Accountobjcontroller{

    public static List<Account> getaccounts() {

        List<Account> Acc =

                [SELECT Id, Name  FROM Account];

        return acc;

Step 4: Create Lightning Application

<aura:application extends="force:slds">

Component Attributes  in salesforce lightning

No comments:

Post a Comment