[SalesForce] Uncaught Action failed error with lightning:recordViewForm

I have a use case in which I need to the display the address fields(all custom fields with the same API name in both objects) of lead/ Account. Let's say if I'm on Enquiry object(custom object) then fetch the address details from lead else if I'm on Opportunity record page then fetch details from Accounts.

Here is my Component code

<aura:component controller="AESL_CommonUpdateAddressCtrl"
                implements="flexipage:availableForRecordHome,force:hasRecordId,force:appHostable">
    <aura:attribute name ="objName" type="String"/>
    <aura:attribute name="title" type="String"/>
    <aura:attribute name="objAddress" type="sObject"/>
    <aura:handler name="init" value="{!this}" action="{!c.init}"/>
    <Lightning:Card class="slds-card__body slds-card__body_inner" 
                    title="{!v.title}">

        <lightning:recordViewForm recordId="{!v.recordId}" objectApiName="{!v.objName}">
        <div class="slds-box">
            <span>These fields should not have a label</span>
            <lightning:outputField fieldName="AESL_State__c" />
            <lightning:outputField fieldName="AESL_City__c" />
        </div>
    </lightning:recordViewForm>
    </Lightning:Card>
</aura:component>

My helper's init method is

getAddress.setCallback(this,function(response){
            var state= response.getState();
            if(state==='SUCCESS'){
                var address = response.getReturnValue()
                if(!$A.util.isEmpty(address)){

                    alert(JSON.stringify(address));
                    component.set('v.recordId',address[0]);
                    component.set('v.objName',address[1]);
                    if(address[1] === 'Lead'){
                        component.set('v.title','Lead Address Information');
                    }
                    else{
                        component.set('v.title','Student Address Information');
                    }
                } 

It's giving an error

Uncaught Action failed:
lightning:recordViewForm$controller$handleRecordIdChange [Cannot read
property 'getList' of null] Callback failed:
apex://AESL_CommonUpdateAddressCtrl/ACTION$getObjectId

I tried to use breakpoints where the code is throwing this error. At
component.set('v.recordId',address[0]); line it get redirected to aura_prod.js
Please correct me if I'm doing something wrong? or is it possible to achieve the scenario with this approach?

Best Answer

Your call:

alert(JSON.stringify(address));

should be showing you that for some cases your server-side is not returning an array (i.e. the alert output does not start with the array token [).

Being not empty is not the same as being an array so this isn't too useful:

if(!$A.util.isEmpty(address)){

(You can see the implementation of the various $A.util functions in the open source for Util.js).

PS

Looking at the error message you quote more carefully, it looks like this code is working:

component.set('v.recordId',address[0]);

but the problem is arising in this component:

<lightning:recordViewForm recordId="{!v.recordId}"  objectApiName="{!v.objName}">

when the record Id changes. That probably leaves the component with recordId set to one object type and a objectApiName set to a different object type. It might be necessary to dynamically recreate the lightning:recordViewForm if you can't find an order to update those attributes that works.

Related Topic