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.

Syntax:

<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>
    </aura:iteration>
</aura:component>


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


            }


        });


     $A.enqueueAction(action);


    }



})


Step 3: Create an Apex controller.

public class Accountobjcontroller{

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


Component Attributes  in salesforce lightning

No comments:

Post a Comment