function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Ertyq MrskErtyq Mrsk 

Custom Text Field Value of Selected Account from Custom Lookup Not Returned in Salesforce LWC

I am implementing a custom lookup field in my LWC form. So far, the record is selected properly. I verify it via console.log statement so that I can know if it is really returning any value.

I also wanted to see the CustomTextField__c value of the selected account record, but every time I check the logs, it returns undefined value. I already assigned a variable that will hold its value, but same result occurs.

Meanwhile, below are the codes I have:

customLookup html file
<template>
    <lightning-record-edit-form object-api-name={childObjectApiName}>
        <label for="fieldid">{fieldLabel}</label>
        <lightning-input-field id="fieldid" required={required} variant="label-hidden" field-name={targetFieldApiName}
            value={value} onchange={handleChange} disabled={disabled}>
        </lightning-input-field>
    </lightning-record-edit-form>
</template>
customLookup js file
import { LightningElement, api } from 'lwc';

export default class CustomLookup extends LightningElement {
    @api childObjectApiName = 'Case'; 
    @api targetFieldApiName = 'AccountId'; 
    

    @api fieldLabel = 'Your field label here';
 
    @api disabled = false;
    @api value;

    @api required = false;

    handleChange(event) {
        const selectedEvent = new CustomEvent('valueselected', {
            detail: event.detail.value[0]
        });
    
        this.dispatchEvent(selectedEvent);
    }

    @api isValid() {
        if (this.required) {
            this.template.querySelector('lightning-input-field').reportValidity();
        }
    }
}
portion from custom form referencing custom lookup lwc html file
<div class="slds-col slds-size_1-of-2">
   <div class="slds-var-m-right_small slds-var-m-bottom_medium">
      <c-custom-lookup field-label="Account" 
            child-object-api-name='Case'
            target-field-api-name='AccountId'
            onvalueselected={handleAccountChange}>
      </c-custom-lookup>
   </div>
</div>
portion from custom form js file
@track selectedAccountId; 
@track selectedAccountCustomTextField;

handleAccountChange(event) {
    //this portion works, display expected id of selected record
    this.caseRec.AccountId = event.detail;
    this.selectedAccountId = event.detail;
    console.log('this.selectedAccountId' + this.selectedAccountId);


    //this portion returns undefined    
    this.selectedAccountCustomTextField__c = this.caseRec.Account.CustomTextField__c; 
    console.log('this.selectedAccountCustomTextField' + this.selectedAccountCustomTextField);
}



 
Saravana Bharathi 1Saravana Bharathi 1
Hi Ertyq,

On change of field value, event.detail, will hold only the record id, and in the javascript its not holding as object.
In order to get the field value of the account from account Id, you can get the field value through wired method in apex and pass the value as object.


import { LightningElement, api,wire } from 'lwc';
import getrecord from '@salesforce/apex/classname.methodname';
export default class CustomLookup extends LightningElement {
@api childObjectApiName = 'Case';
@api targetFieldApiName = 'AccountId';
@api fieldLabel = 'Your field label here';
@api disabled = false;
@api value;
@api required = false;

handleChange(event) {
getrecord({id:accountidvalue})
.then(result=>{
const selectedEvent = new CustomEvent('custevent', { detail: { accountid:accountidvalue, customtextfield__c:result } });//pass multiple parameter to get the value
this.dispatchEvent(selectedEvent);
}
});


@api isValid() {
if (this.required) {
this.template.querySelector('lightning-input-field').reportValidity();
}
}
}




@track selectedAccountId;
@track selectedAccountCustomTextField;

handleAccountChange(event) { //this portion works, display expected id of selected record
this.selectedAccountId = event.detail.accountid; //Access the account id
//this portion returns undefined
this.selectedAccountCustomTextField__c = event.detail.customtextfield__c; //access the custom text field value
console.log('this.selectedAccountCustomTextField' + this.selectedAccountCustomTextField);
}


Write an apex controller on <class name> with <methodname>, which returns the customtextfield value as string.

Try this and mark it as resolved. if it solves.

Thanks