+ Start a Discussion
Ertyq MrskErtyq Mrsk 

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.



 
ShirishaShirisha (Salesforce Developers) 
Hi Ertyq,

Greetings!

It is difficult for us to narrow down the code you have given above.However,below are the possibilities that can cause the issue:

>>assignment number in text format to numeric variable
>>assignment of incorrect Id field, for example you’re trying to use GUID in native salesforce ID field

So,please check the code where you have assigned the value to the field since you have an idea of your implementation.

Kindly let me know if it helps you and close your query by marking it as best answer so that it can help others in the future.

Warm Regards,
Shirisha Pathuri
Ertyq MrskErtyq Mrsk
Hi Shirisha,

Thanks for your immediate response! But could you please elaborate more what you mean by the 2nd cause? 

>>assignment of incorrect Id field, for example you’re trying to use GUID in native salesforce ID field

Can you give me a sample scenario where such thing occurs?
 
ShirishaShirisha (Salesforce Developers) 
Yes,if you have field which of Id type and you are trying to assign the value/variable which is of String type which may cause this issue.

Thank you!
Ertyq MrskErtyq Mrsk
I think there is a naming issue as I modified the field name, error has disappeared. But upon insertion, lookup field does not have any values when I check the record detail page. Console.log returns the selected record id, though. I modified the customLookup.js to this:
 
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();
        }
    }
}