You need to sign in to do that
Don't have an account?
force:recordData returning null
Hello
I want to use force:recordData to load record infos, but currently it returns null.
record is null.
I want to use force:recordData to load record infos, but currently it returns null.
<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId" access="global" controller="AP_Constants"> <aura:handler name="init" value="{!this}" action="{!c.doInit}"/> <aura:attribute name="recordId" type="Id" /> <aura:attribute name="accountRecord" type="Object"/> <force:recordData aura:id="recordLoader" layoutType="FULL" recordId="{!v.recordId}" targetFields="{!v.accountRecord}" /> </aura:component>
({ doInit : function(component, event, helper) { var recid = component.get("v.recordId"); var record = component.get("v.accountRecord"); })Here when I debug var record = component.get("v.accountRecord");
record is null.
First you are correct in the init function the record won't load so you can't access any field values.
But we have one Trick:
recordUpdated functionlity in force:recordData by using this we can play the after data loaded in UI:
Sample code what i have done before:
Component:
<aura:attribute type="Object" name="record"/>
<aura:attribute name="accRecord" type="Object" />
<force:recordData aura:id="recordLoader"
recordId="{!v.recordId}"
fields="Id, Name, OwnerId"
targetRecord="{!v.record}"
targetError="{!v.recordError}"
targetFields="{!v.accRecord}"
recordUpdated="{!c.doInit}" />
Controller:
doInit: function(component, event, helper) {
var ownerId = component.get("v.accRecord").OwnerId;
var leadFlag = component.get("v.accRecord").Name;
console.log('@@@ OwnerId'+ownerId);
console.log('@@@ Name ' + Name);
}
=============================================
Your Code :
<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId" access="global" controller="AP_Constants">
<aura:attribute name="recordId" type="Id" />
<aura:attribute name="accountRecord" type="Object"/>
<force:recordData aura:id="recordLoader"
layoutType="FULL"
recordId="{!v.recordId}"
targetFields="{!v.accountRecord}"
recordUpdated="{!c.doInit}"
/>
</aura:component>
({
doInit : function(component, event, helper) {
var recid = component.get("v.accountRecord").Id;
var record = component.get("v.accountRecord");
console.log('@@@ Id'+ recid);
console.log('@@@ Record'+ record);
})
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj
All Answers
You must include your component into a Lex record page or use a custom action.
You have probably tested your component with an application like the code below :
<aura:application ><c:TestRecordData recordId='0015800001NtjP1AAJ' />
</aura:application>
but that doesn't work for <force:recordData> even if you pass the recordId as a parameter.
Loading a Record: The following example illustrates the essentials of loading a record using Lightning Data Service. This component can be added to a record home page in the Lightning App Builder, or as a custom action. The record ID is supplied by the implicit recordId attribute added by the force:hasRecordId interface.
https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/data_service_load_record.htm
I do use a custom action.
I have a Button on the Account record page that launches a Custom Action.
The Custom Action calls the Lightning Component.
The recordId populates well.
Only the force:recordData does not work.
IMHO it should work and I don't know what I am missing
I found out something that is not really documented in official doc.
According to this post:
https://salesforce.stackexchange.com/questions/206686/get-the-value-from-forcerecorddata-on-init-handler
force:recordData handles loading the record(s) asynchronously. As such, it will not be available during the init handler (there hasn't been a chance to run asynchronous code yet), and will likely not be available in the first render/afterRender cycle either.
That is why it's not working.
I have made a test with a custom action and that works really fine (perfect).
How do you create your custom action?
I cannot reproduce your problem and you don't have posted all your code so it is impossible to help you.
Did you try to change the value of the record id in your apex code?
First you are correct in the init function the record won't load so you can't access any field values.
But we have one Trick:
recordUpdated functionlity in force:recordData by using this we can play the after data loaded in UI:
Sample code what i have done before:
Component:
<aura:attribute type="Object" name="record"/>
<aura:attribute name="accRecord" type="Object" />
<force:recordData aura:id="recordLoader"
recordId="{!v.recordId}"
fields="Id, Name, OwnerId"
targetRecord="{!v.record}"
targetError="{!v.recordError}"
targetFields="{!v.accRecord}"
recordUpdated="{!c.doInit}" />
Controller:
doInit: function(component, event, helper) {
var ownerId = component.get("v.accRecord").OwnerId;
var leadFlag = component.get("v.accRecord").Name;
console.log('@@@ OwnerId'+ownerId);
console.log('@@@ Name ' + Name);
}
=============================================
Your Code :
<aura:component implements="force:lightningQuickActionWithoutHeader,force:hasRecordId" access="global" controller="AP_Constants">
<aura:attribute name="recordId" type="Id" />
<aura:attribute name="accountRecord" type="Object"/>
<force:recordData aura:id="recordLoader"
layoutType="FULL"
recordId="{!v.recordId}"
targetFields="{!v.accountRecord}"
recordUpdated="{!c.doInit}"
/>
</aura:component>
({
doInit : function(component, event, helper) {
var recid = component.get("v.accountRecord").Id;
var record = component.get("v.accountRecord");
console.log('@@@ Id'+ recid);
console.log('@@@ Record'+ record);
})
Can you please Let me know if it helps or not!!!
If it helps don't forget to mark this as a best answer!!!
Thanks,
Raj