I am getting an error while loading my aura components due to some of the lookup fields does not have any value. In our data model we have a lookup to an Account from Account. It's setup as a customer belonging to a company (employer) which is a lookup. The customer may or may not have an employer associated.

I have a following code in a component in which I am getting an error.

<aura:component controller="CustomerAccountsController" implements="flexipage:availableForAllPageTypes,force:hasRecordId">

    <aura:attribute name="recordId" type="Id" />
    <aura:attribute name="acc" type="Account" />

    <force:recordData aura:id="recordLoader"
                      fields="external_id__c, Employer__r.external_id__c"

The Employer__c lookup may or may not be present for all the individual account record. While loading a component error like following:

Action failed: c:accountDetails$controller$recordLoaded [Cannot read property 'external_id__c' of null]

Any idea how I can conditionally say in fields that if Employer is null then dont look for Employer__r.external_id__c.


In your JavaScript, use the && operator:

var externalId = record.Employer__r && record.Employer__r.external_id__c;

Make sure that you use the proper case, as JavaScript is cAsE-sEnSiTiVe.

  • Thank you. Always helpful. I was about to answer my own question but your reply was already here. Best, – Json Bourne Shell May 3 '19 at 20:57

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.