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
bretondevbretondev 

force:recordData returning null

Hello

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.
 
Best Answer chosen by bretondev
Maharajan CMaharajan C
Hi Brenton,

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

Alain CabonAlain Cabon
Hello,

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
 
bretondevbretondev
Hi Alain
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
Ravi Dutt SharmaRavi Dutt Sharma
Can you try to remove the recordId attribute from your component. Ideally the recordId should be provided by force:hasRecordId interface. I think the attribute is conflicting with the standard attribute.
bretondevbretondev
I made some progress on my research but still I do not have a solution.
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.
bretondevbretondev
Basically I need help on how to access force:recordData only after component initialization.
Alain CabonAlain Cabon
Hello,

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?
Maharajan CMaharajan C
Hi Brenton,

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
This was selected as the best answer
bretondevbretondev
Thanks. It works. But official documentation is quite poor about this tag. Anyway, solved