[SalesForce] Error: INVALID_TYPE_ON_FIELD_IN_RECORD, [FIELD]: value not of required type on lookup field when inserting record in Salesforce LWC

I've been getting the following error in apex debug logs when I try to insert a record containing a lookup field.

Insert failed. First exception on row 0; first error:
INVALID_TYPE_ON_FIELD_IN_RECORD, [FIELD]: value not of required type

In order for me to replicate the standard lookup field in a LWC page, I found a reusable custom lookup that can be referenced on the main page.

I am not sure why this keeps on occurring.

Meanwhile, following are the current codes I've been working on:

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
        });
       
        this.dispatchEvent(selectedEvent);
    }

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

customObjLWC.html

<!--portion of main page-->
<div>

    <c-custom-lookup field-label="ObjectA" 
        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:""};

    handleObjectAChange(event) {
        this.custObjRecord.ObjectA__c = event.detail;
    }

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

Hope someone could point me to the right direction and help me proceed with the insertion of record.

Best Answer

That import you are doing is basically a SObject describe - you don't want this for an insert, you really just want a plain JSON object here. So instead of:

 @track custObjRecord = CUSTOM_OBJECT;

Just make it:

 @track custObjRecord = {ObjectA__c:""};

Your save method should work find in this case.