# Can't get force:recordData to work

I am trying to convert my Classic custom Javascript buttons to Lightning Experience actions buttons. Salesforce has a service to do this automatically, but the generated code is not very good. In my case it's outright wrong. But it is a starting point.

This is my component, I have added the force:recordData and the implements parts to the generated component:

<aura:component controller="OpportunityUtil" implements="force:hasRecordId" extends="c:LCC_GenericLightningComponent" >
<aura:handler event="c:LCC_GenericApplicationEvent" action="{!c.apexExecute}"/>
<aura:set attribute="partially" value="false"></aura:set>
<aura:attribute name="showAlert" type="Boolean" default="false"/>
<aura:attribute name="alertText" type="String"/>
<aura:attribute name="simpleContactRecord" type="Object"/>
<aura:attribute name="recordLoadError" type="String"/>

<force:recordData aura:id="recordLoader"
recordId="{!v.sObjectInfo.Id}"
layoutType="FULL"
targetError="{!v.recordLoadError}"
targetFields="{!v.simpleContactRecord}"
recordUpdated="{!c.handleRecordLoaded}"
/>

<div class="slds-scrollable slds-p-around_medium slds-text-heading_small" id="modal-content-id-1">
<aura:if isTrue="{!v.showAlert}">
<p class="slds-hyphenate" style="height: 6.75rem;">{!v.alertText}</p>
<aura:set attribute="else">
<div style="height: 6.75rem;">
<div role="status" class="slds-spinner slds-spinner_large slds-spinner_brand">
<span class="slds-assistive-text">Loading</span>
<div class="slds-spinner__dot-a"></div>
<div class="slds-spinner__dot-b"></div>
</div>
</div>
</aura:set>
</aura:if>
<br/>
</div>
<div class="slds-modal__footer">
<lightning:button class="slds-button_brand" onclick="{!c.accept}" label="Accept"/>
</div>


This is my controller, I have changed and simplified it compared to the generated code:

({
apexExecute : function(component, event, helper) {
},

accept : function(component, event, helper) {
},

handleRecordLoaded : function(component, event, helper) {
console.log('inside handleRecordLoaded');
}
})


The function handleRecordLoaded is never called. My Javascript experience is limited, so it's probably something simple, I think and hope.

I have found an explanation for the use of targetFields and targetRecord here

• You are correct that this code is outright wrong, however I didnt understand what you are trying to achieve, can you explain IN DETAIL what are you trying to do when record action button is clicked - you can add this in question? – salesforce-sas Oct 1 '19 at 5:17
• @salesforce-sas The code that I show in the question is heavily modified from what Salesforce had generated. Mainly stripped to get to the core of the problem. – Sander de Jong Oct 1 '19 at 6:22

## 1 Answer

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

By implementing "force:hasRecordId", the component automatically inherits the record ID. You don't even need to explicitly have the "recordId" attribute named, although you can.

Looking at the example in the link, you really just do the following to load a record the aura component lives on. This is really just an exact copy of the example in the link. I noticed you're not using recordId in your component which I'd be interested in seeing if that's the culprit of the issues you're having.

<aura:attribute name="simpleRecord" type="Object"/>
<aura:attribute name="recordError" type="String"/>

<force:recordData aura:id="recordLoader"
recordId="{!v.recordId}"
targetFields="{!v.simpleRecord}"
targetError="{!v.recordError}"
recordUpdated="{!c.handleRecordUpdated}"
/>


You had some different variable names for targetFields, targetError, and recordUpdated but it seems you have the correct type set. The only issue I can see is what you're passing in the recordId

• I have tried targetFields as well as layoutType, but no combination every worked. I don't get any feedback from the Developer Console of which (combinations of) options are valid. – Sander de Jong Sep 24 '19 at 14:49
• If you simplify the code (in a new test component) and just have the recordData and handleRecordLoaded I think that would be the quickest way to debug. I'd be interested in seeing if it works if you simply pass the recordId of the object you're on. That way, you'll know the structure is right in regards to recordData cause it doesn't seem to successfully load right now. I've personally never passed an ID to recordData that wasn't the object the component was on so that's why I recommend going to a simpler version. – Kris Goncalves Sep 24 '19 at 15:39
• Thanks for the advise. I have simplified my code and updated my question. It still doesn't work though. I have no idea how I can debug this stuff, the only thing I have is the browser console log. Developing for Lightning sure is harder than doing Apex programming. – Sander de Jong Sep 25 '19 at 8:16
• What're you trying to do with this component? What is it on? I see it looks like you're trying to load a contact record, but are you on a contact record? – Kris Goncalves Sep 29 '19 at 19:51
• Yes, I am on a Contact record. In Classic, the page layout has a custom JS button that first does a couple of JS checks, then it calls Apex code and then it redirects to the URL that the Apex method returns. – Sander de Jong Sep 30 '19 at 13:09