11

Is there a way we can get the value from force:RecordData on init ? I am getting the onclick event of a button.

<aura:handler name="init" value="{!this}" action="{!c.doInit}"/>
    <aura:attribute name="recordId" type="String"/> 
    <aura:attribute name="showButton" type="Boolean" default="false"/>
    <aura:attribute name="Opportunity" type="Object"/>   
    <aura:attribute name="parentOpp" type="Object"/>
    <aura:attribute name="oppLoadError" type="String"/>
    <force:recordData aura:id="oppRecordLoader"
                      recordId="{!v.recordId}"
                      fields="Id,
                              Name,
                              AccountId,
                              Cohort_Sem__r.End_date__c,
                              LeadSource,
                              Staff_Referral_Detail__c,
                              lead_source_detail__c"
                      targetRecord="{!v.Opportunity}"
                      targetFields="{!v.parentOpp}"
                      targetError="{!v.oppLoadError}"
                      />

JS:

doInit: function(component, event, helper) {           
         var opp = component.get("v.parentOpp");         
         console.log('Name Value: '+ opp.Name);
    }

Error:

Action failed: c:createIWSRecord$controller$doInit [Cannot read property 'Name' of null] quickActionHandlerHelper.js failed to create component - forceChatter:lightningComponent

0

2 Answers 2

23

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.

The official method is to use the recordUpdated event.

<aura:attribute name="record" type="Asset" />

<force:recordData layoutType="FULL"
                  recordId="{!v.recordId}"
                  targetFields="{!v.record}"
                  recordUpdated="{!c.recordUpdate}" />

({
    recordUpdate: function(component, event, helper) {
        alert(component.get("v.record").Name);
    }
})
3
  • You need to add mode for recordUpdated to work: recordUpdated="{!c.recordLoaded}" mode="VIEW" Commented Jul 15, 2022 at 9:26
  • @DaveRoberts The default mode is VIEW, so it is not necessary to specify it. You will, however, get an error if you specify an invalid mode. I just tested this to confirm the behavior, and it does indeed work here verbatim with no extra attributes.
    – sfdcfox
    Commented Jul 15, 2022 at 9:47
  • You're correct. I also confirmed it. I must have had something else wrong when I came to that conclusion. Commented Jul 18, 2022 at 10:40
-2

I think in Init your Data is not available, Instead of Init, use render handler. It seems to be working in render handler.

<aura:handler name="render" value="{!this}" action="{!c.doInit}"/>
1
  • This approach is not reliable: record loading is asynchronous and independent from the rendering cycle. The data may not be available at the first (or second or third) render. Use the recordUpdated event to be notified when the record is loaded. Commented Feb 2, 2018 at 7:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .