Below lightning component is from Lightning data service module
in trailhead
Questions 1: Why there are two object attributes used – record , simpleRecord
, when one attribute can serve the purpose. Is there any reason to use a different attribute in target field?
<aura:component implements="flexipage:availableForRecordHome,force:hasRecordId"> <!--inherit recordId attribute-->
<aura:attribute name="record" type="Object" description="The record object to be displayed"/>
<aura:attribute name="simpleRecord" type="Object" description="A simplified view record object to be displayed"/>
<aura:attribute name="recordError" type="String" description="An error message bound to force:recordData"/>
<force:recordData aura:id="record"
layoutType="FULL"
recordId="{!v.recordId}"
targetError="{!v.recordError}"
targetRecord="{!v.record}"
targetFields="{!v.simpleRecord}"
mode="VIEW"/>
<!-- Display a lightning card with details about the record -->
<div class="Record Details">
<lightning:card iconName="standard:account" title="{!v.simpleRecord.Name}" >
<div class="slds-p-horizontal--small">
<p class="slds-text-heading--small">
<lightning:formattedText title="Billing City" value="{!v.simpleRecord.BillingCity}" /></p>
<p class="slds-text-heading--small">
<lightning:formattedText title="Billing State" value="{!v.simpleRecord.BillingState}" /></p>
</div>
</lightning:card>
</div>
<!-- Display Lightning Data Service errors, if any -->
<aura:if isTrue="{!not(empty(v.recordError))}">
<div class="recordError">{!v.recordError}</div>
</aura:if>
</aura:component>
Question 2 – Record Data has an attribute fields="{!v.fieldsToQuery}"
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/aura_compref_force_recordData.htm
If fields
attribute left blank or unused would we unable to display it in the UI? I see the targetFields attribute is used to display field in the UI below. What is the significance of using Fields
– appreciate an example if possible.
<div class="Record Details">
<lightning:card iconName="standard:account" title="{!v.simpleRecord.Name}" >
<div class="slds-p-horizontal--small">
<p class="slds-text-heading--small">
<lightning:formattedText title="Billing City" value="{!v.simpleRecord.BillingCity}" /></p>
<p class="slds-text-heading--small">
<lightning:formattedText title="Billing State" value="{!v.simpleRecord.BillingState}" /></p>
</div>
</lightning:card>
</div>
Best Answer
They have different "shapes", where "simpleRecord" is a less complicated format. The "record" type was, I believe, introduced first, and it was more complicated, so this is an alternative method. You can use whichever you prefer. The original attribute is provided for backwards compatibility.
targetFields is the corresponding attribute to simpleRecord. targetFields is easier to use compared to targetRecord. You do not need to use both, just stick with one. I recommend targetFields.
You use
fields
if you want to query specific fields (e.g. to use in your controller), andlayoutType
if you want to replicate a page layout (e.g. get all the fields that you'd see in the standard layout), along withmode
to determine the appropriate mode (either read or edit mode). You must specify eitherfields
orlayoutType
.