Created
February 6, 2020 12:31
-
-
Save saicharanreddyk/af1a3eeccdbe505e576e4420089e5ffd to your computer and use it in GitHub Desktop.
Lghtning - How to take sObject as a return type into the component?
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
In the below example we are passing sObject data i.e. acc the helper from there to controller fom there to component. | |
In the component when we initialized the attribute we didn't sepecify default as default="{'sObjectType':'Account'}" | |
because we are getting the acc data from Apex and we are sure that we are getting Account data only hence we didn't initialized. | |
<aura:handler name="init" value="{!this}" action="{!c.callMe}" /> -- Same as constructer. In this example we have only shown one record | |
if we want to display multiple records either we need to go for data table / iterator. | |
Component | |
************************************************************************************************************ | |
<aura:component controller="Example_Four"> | |
<aura:attribute name="acc" type="Account" /> | |
<aura:handler name="init" value="{!this}" action="{!c.callMe}" /> | |
<lightning:card iconName="standard:account"> | |
<aura:set attribute="title"> | |
<h1> Account</h1> | |
<h1>{!v.acc.Name}</h1> | |
</aura:set> | |
<aura:set attribute="actions"> | |
<lightning:Button label="Save" /> | |
<lightning:Button label="Cancel" /> | |
</aura:set> | |
<lightning:layout> | |
<lightning:layoutItem size="2" padding="around-small" > | |
Name : <br/>{!v.acc.Name} | |
</lightning:layoutItem> | |
<lightning:layoutItem size="2" padding="around-small" > | |
Phone :<br/> {!v.acc.Phone} | |
</lightning:layoutItem> | |
<lightning:layoutItem size="2" padding="around-small" > | |
Industry :<br/> {!v.acc.Industry} | |
</lightning:layoutItem> | |
<lightning:layoutItem size="2" padding="around-small" > | |
Rating :<br/> {!v.acc.Rating} | |
</lightning:layoutItem> | |
</lightning:layout> | |
</lightning:card> | |
</aura:component> | |
************************************************************************************************************ | |
Controller | |
************************************************************************************************************ | |
({ | |
callMe : function(component, event, helper) { | |
helper.callHelper(component); | |
} | |
}) | |
************************************************************************************************************ | |
Helper | |
************************************************************************************************************ | |
({ | |
callHelper : function(component) { | |
var action=component.get("c.invokeAcc"); | |
action.setCallback(this,function(response){ | |
var state=response.getState(); | |
if(state==='SUCCESS'){ | |
var acc=response.getReturnValue(); | |
console.log(acc); | |
component.set("v.acc",acc); | |
} | |
}); | |
$A.enqueueAction(action); | |
} | |
}) | |
************************************************************************************************************ | |
Apex Class | |
************************************************************************************************************ | |
public class Example_Four { | |
@AuraEnabled | |
public static Account invokeAcc(){ | |
Account acc =[select id,Name,Phone,Industry,Rating from Account limit 1]; | |
return acc; | |
} | |
} | |
************************************************************************************************************ |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment