0

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?

  • 1 more thing, I tried the same code with fixed recordId and API name(Lead) on Enquiry record Page. Then it's running fine. – VDahiya Oct 29 '18 at 6:59
0

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.

  • Hi @Keith, I have not encountered any cases yet in which the server is returning something and the alert message is not displayed. Also, I'm using alert just to pop-up the value returned from the server and will be removed from the final code. – VDahiya Oct 29 '18 at 10:08
  • @VDahiya I'm saying that I think that when your code goes wrong the server is not returning an array. Can you add to your question the exact output of the alert when you get the error? – Keith C Oct 29 '18 at 10:11
  • ["00QK000000B50FWMAZ","Lead"] alert output when error is thrown. – VDahiya Oct 29 '18 at 10:54
  • @VDahiya Yeah weird. Does $A.util.isArray(address) return true? Suggest using your browser's JavaScript debugger would make sense; see e.g. peterknolle.com/lightning-component-debugging-tips. – Keith C Oct 29 '18 at 11:03
  • Yeap $A.util.isArray(address) returned true in case of error also. – VDahiya Oct 29 '18 at 11:06
0

After trying different things and experiments I came to the solution of the problem. Here is what I did

Instead of using

<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>

I used this

<lightning:recordForm recordId="{!v.recordId}"
                              objectApiName="{!v.objName}"
                              fields="{!v.leadfield}"
                              mode="view"/>

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.