1

Upon inserting some sample record, I noticed that only the Name field has been inserted, while Object A lookup field does not have any value. I checked log via browser's developer console and id of the inserted record is recorded. But when I visit the record's detail page on Salesforce, the lookup field does not have any value at all. How can I insert the selected record from the lookup field?

Here are the codes that I am stuck with:

customLookup.html

<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

import { LightningElement, api } from 'lwc';

export default class CustomLookup extends LightningElement {
    @api childObjectApiName = 'Custom_Object__c'; 
    @api targetFieldApiName = 'ObjectA__c'; 

    @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();
        }
    }
}

customObjLWC.html

<!--portion of main page-->
<div>                   
    <lightning-input type="text" 
        name="CustomObjectName" 
        label="Custom Object Name"
        value={custObjRecord.Name} 
        onchange={handleCustObjNameChange}>
    </lightning-input>
</div> 

<div>
    <c-custom-lookup field-label="Object A" 
        child-object-api-name='Custom_Object__c'
        target-field-api-name='ObjectA__c'
        onvalueselected={handleObjectAChange}>
    </c-custom-lookup>

</div>

customObjLWC.js

import { LightningElement, track, wire } from 'lwc';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
import CUSTOM_OBJECT from '@salesforce/schema/Custom_Object__c';

import createCustomObj from '@salesforce/apex/CustomObjectController.createCustomObj';
    
    export default class customObjLWC extends LightningElement {

    @track custObjRecord = {Custom_Object__c:""};

    handleCustObjNameChange(event) {
        this.custObjRecord.Name = event.detail.value;
        console.log('Custom Object Name' + this.custObjRecord.Name);
    }

    handleObjectAChange(event) {
        this.custObjRecord.ObjectA__c = event.detail;
        console.log('Object A' + this.custObjRecord.ObjectA__c);
    }

    validateLookupField() {
        this.template.querySelector('c-custom-lookup').isValid();
    }


    createRecord() {

        createCustomObj({
            newCustObj: this.custObjRecord
        })
        .then(result => {
            
            this.custObjRecord = {};
            

            this.dispatchEvent(new ShowToastEvent({
                title: 'Success!!',
                message: 'Custom Object Record Created Successfully!!',
                variant: 'success'
            }),);
        })
        .catch(error => {
            this.error = error;
        });
    }   
}

CustomObjectController.cls

public with sharing class CustomObjectController {

    @AuraEnabled
    public static Custom_Object__c createCustomObj(Custom_Object__c newCustObj) {
       insert newCustObj;
       return newCustObj;
    }
}

Would appreciate if anyone could help me on this. Thanks in advance!

0

Use event.detail.value[0] to get the recordId of the selected record. Value of lookup field is an array.

handleChange(event) {
    
    const selectedEvent = new CustomEvent('valueselected', {
        detail: event.detail.value[0]
    });
   
    this.dispatchEvent(selectedEvent);
}
| improve this answer | |
  • I modified the customLookup.js, but there is still no lookup field value when I checked the record detail page upon creation. – Ertyq Jul 3 at 4:59
  • Have you got the id in co console.log? – rahul gawale Jul 3 at 5:10
  • Yes, id is still displaying in console.log – Ertyq Jul 3 at 5:13
  • also check the fls for that field – rahul gawale Jul 3 at 5:19
  • 1
    Field is visible to all profiles, and value is inserting when I use Salesforce standard creation page. – Ertyq Jul 3 at 5:55
0

Is the lookup part of the default page layout for the profile accessing your LWC? There seems to be a "feature" or Record Edit Form that Lookup fields do not work correctly unless they are on the page layout, See https://help.salesforce.com/articleView?id=000349125&language=en_US&mode=1&type=1

update: It is also being tracked as a Known Issue since Winter 19, status "In Review": https://trailblazer.salesforce.com/issues_view?id=a1p3A000001RWq7QAG

| improve this answer | |

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.