6

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

16

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);
    }
})
  • That worked. Though recordUpdated confusing:). – Ajay Feb 1 '18 at 20:25
-1

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}"/>
  • value is printing, however getting the error "Uncaught Action failed: c:createIWSRecord$controller$doInit [Cannot read property 'Name' of null]" and also value is printing twice in the console. – Ajay Feb 1 '18 at 16:25
  • have you removed Init handler?? – salesforce Developer Feb 1 '18 at 16:27
  • yes, replaced with render. – Ajay Feb 1 '18 at 16:27
  • 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. – Kevin Venkiteswaran Feb 2 '18 at 7:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.