0

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"
                      recordId="{!v.recordId}"
                      targetFields="{!v.acc}"
                      fields="external_id__c, Employer__r.external_id__c"
                      recordUpdated="{!c.recordLoaded}"
                      />
    ......
</aura:component>

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.

1 Answer 1

2

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.

1
  • Thank you. Always helpful. I was about to answer my own question but your reply was already here. Best, May 3, 2019 at 20:57

You must log in to answer this question.

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