[SalesForce] How to pass a record for Lightning component Attribute – Novice question

A lightning component: helloPlayground

<aura:component>
    <aura:attribute name="messages" type="String"/>
    <p>Message of the day: {!v.message}</p>
</aura:component>

I can pass the attribute value in a lightning App like:

<aura:application >
    <c:helloPlayground messages="['You look nice today.']"/>
</aura:application>

2nd Lightning Component: helloObjectAttrComponent

<aura:component >
    <aura:attribute name="item" type="Camping_Item__c" required="true"/>
        <lightning:formattedNumber value="{!v.item.Quantity__c}" style="decimal"/>
        <lightning:formattedNumber value="{!v.item.Price__c}" style="Currency"/>
</aura:component>

How do I pass the record in the lightning App below like the way I passed String? Is this even possible or the records can be only passed through apex controller?

<aura:application >
    <c: helloObjectAttrComponent xxxxxx />
</aura:application>

Best Answer

You do it the exact same way:

<c:helloObjectAttrComponent item="{!v.record}" />

You can initialize a record in the same manner as most other objects:

<aura:attribute name="record" type="Camping_Item__c" 
    default="{ 'sobjectType': 'Camping_Item__c', 'Quantity__c': 5, 'Price__c': 4.99 }" />

Note that if you pass in the value directly, as you did in the first example:

<c:helloObjectAttrComponent item="{ 'sobjectType': 'Camping_Item__c' }" />

Then the object would be immutable (can't be edited).

Related Topic