function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
PatMcClellan__cPatMcClellan__c 

Force Lightning Data Service to load a specific record?

Cross-posting here from the Trailhead forum, where I'm working on the Lightning Components Framework SuperBadge. We're implementing LDS on a page, but NOT using force:hasRecordId. Instead, we're dynamically setting an aura:attribute, and then referencing it in the LDS parameter for recordId. I've been researching this for hours, but all the reference docs and Trailhead modules assume the case where you're using the implicit recordId on the page. No examples I can find where they're setting the recordId and then forcing the reload.

In this case, the id attribute is successfully being set, but LDS isn't loading the record. The error message says: The onBoatSelected handler in the BoatDetails controller must force Lightning Data Service to load the specified record, using logic in the controller.

In the controller, I'm using a reloadRecord command: component.find("service").reloadRecord() where "service" is the aura:id for the force:recordData element.

Is there some other command required to get the LDS to load the record?

Here's the component markup with the force:recordData info:
<aura:attribute name="id" type="String"/>
    <aura:attribute name="boat" type="Boat__c"/>
    <aura:attribute name="boatSimple" type="Boat__c"/>
    <aura:attribute name="recordError" type="String"/>

    <aura:handler event="c:BoatSelected" action="{!c.onBoatSelected}"/>

    <force:recordData aura:id="service"
                      recordId="{!v.id}"
                      mode="VIEW"
                      fields=  "Id,
                                Name,
                                Description__c,
                                Price__c, Length__c,
                                Contact__r.Name,
                                Contact__r.Email,
                                Contact__r.HomePhone,
                                BoatType__r.Name,
                                Picture__c"
                      targetRecord="{!v.boat}"
                      targetFields="{!v.boatSimple}"
                      targetError="{!v.recordError}"
                      recordUpdated="{!c.onRecordUpdated}" />
Am I screwing up something in the markup? 

 
AndrewTaylorAndrewTaylor
Hi Pat - what was the solution that you had for this? I'm having a similar issue trying to use force:recordData and the data not rendering.
AndrewTaylorAndrewTaylor
Thanks Pat!
Avinash AAvinash A
Hi Pat,

Even I am facing the similar issue,..i.e. Im not able to set the id attribute, and also the recoerd is not getting loaded...Could you please help me..
<aura:attribute name="boat" type="Boat__c"/>
    <aura:attribute name="id" type="Id"/>
   <force:recordData aura:id="service"
                      recordId="a0H7F00000BL8LQ"
                      mode="VIEW"
                      fields=  "Id,
                                Name,
                                Description__c,
                                Price__c,
                                Length__c,
                                Contact__r.Name,
                                Contact__r.Email,
                                Contact__r.HomePhone,
                                BoatType__r.Name,
                                Picture__c"
                      targetFields="{!v.boat}"
                      recordUpdated="{!c.onRecordUpdated}" />

This is my code snippet
Balagopal GBalagopal G

Will this LDS works other than Component Load or page Load.

For example : In a custom vf page, a Lightning App is loaded and then inside a component in  the same app, on a button click in that component , in modal (without component refresh or page load) CRUD operations are included.

Is it possible to use LDS in the above scenario or LDS will work only on Page Load?!.

Punit@forcePunit@force
In my code I wasn't having a targetRecord attribute in force:recordData but still I was getting this error. I changed the reload code in controller method from component.find("service").reloadRecord(true); to component.find("service").reloadRecord() and then challenge validator was ok with that. I know that this is an old thread but still might help 
Pankaj S.Pankaj S.
Thanks @Punit@force! It solved my issue.
ElDeibizElDeibiz
Thanks @Punit@force! You solved my issue too!
Biswajit SamantarayBiswajit Samantaray

I learned two things: 

1.  reloadRecord() method  ===>   component.find("service").reloadRecord();    ==> We have to use it on the change of the 'recordId' or 'fields' attribute of force:recordData tag. In this superbadge we use it right now in onBoatSelected() handler, but we can call it via any other handler called via like 'recordUpdated' attribute in force:recordData tag

User-added image

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/data_service_handling_record_change.htm

2. Why to use 'targetFields' over  'targetRecord'    ===> For example, for the Name field, v.targetRecord.fields.Name.value is equivalent to v.targetFields.Name. 

https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/data_service_load_record.htm

Using targetFields attribute over targetRecord attribute in force:recordData tag