5

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>
11

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?

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.

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.

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), and layoutType if you want to replicate a page layout (e.g. get all the fields that you'd see in the standard layout), along with mode to determine the appropriate mode (either read or edit mode). You must specify either fields or layoutType.

  • If I specify fields with just ` Name ` in it. Then would I not be able to access other fields like ` billingstate ` etc. Would be able to display the field value using ` targetfield ` attribute if I just specify ` Name ` in the ` fields `. – SfdcBat Nov 29 '17 at 16:36
  • @SfdcBat Correct. Only the fields specified in fields can be accessed if you use that attribute. If you use layoutType, you can access all the fields on the page layout (but only those fields). – sfdcfox Nov 29 '17 at 16:38
  • Gotcha! Does salesforce takes care of the FLS? I built a edit page cmp with certain fields and put it on the account record page. If the user does not have edit permission on 1 field will the user be able to save the record? – SfdcBat Nov 29 '17 at 16:41
  • @SfdcBat The docs don't say explicitly, but given salesforce.com's focus on security, I would be surprised if it didn't honor FLS. – sfdcfox Nov 29 '17 at 16:45
8

Question 1:

targetFields attribute is a simplied view of fields to be referenced where as targetRecord consists of fields, childRelationships, apiName, id, recordTypeInfo.

If you just want to access the fields using lightning data service, better to go with targetFields.

Syntax: {! v.targetFields.Name}

Ofcourse, you can also access the fields using targetRecord but the syntax is a bit different as it has other keys as well.

Syntax: {! v.targetRecord.fields.Name.value}, where fields represents you are accessing the fields schema.

If you want to access recordTypeInfo replace fields with recordTypeInfo

So syntax would be {! v.targetRecord.recordTypeInfo.Name.value}

Question 2:

The fields that Lightning data service has to query is based on two attributes i.e fields or layoutType.

  • If you specify the layoutType attribute as FULL, it queries the fields that are mapped on the record page layout.
  • If you specify the layoutType attribute as COMPACT, it queries the fields mapped on compact layout (primary).
  • If the field that you are looking is not exposed on any of these layouts but you need to access the fields by lightning data service, then there is an option to specify individual fields using fields attribute.
  • If the field is not added in any of these layouts and fields attribute, then that field will not be accessible through lightning data service. (I think it returns null, *not sure)

So either of the attributes (fields or layouttype) must be specified.

  • Good explanation! – SfdcBat Nov 29 '17 at 16:48
  • @SfdcBat Thank you – brahmaji tammana Nov 29 '17 at 17:19
  • 1
    Easy to understand and complete answer. Deserve to be the correct one – Ruwantha May 30 at 8:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.