0

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.

2
  • 1
    You should strongly consider ditching custom Apex and using import { createRecord } from "lightning/uiRecordApi";.
    – Adrian Larson
    Jun 25 '20 at 17:12
  • 1
    @AdrianLarson Not yet sure about that as I am planning to handle bulk insert and other complex functions in future implementations. Still weighing on both Apex and Javascript's capabilities when handling larger transactions.
    – Ertyq
    Jun 25 '20 at 17:23
1

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.

1
  • In order for me to see the value, I checked the javascript log and it returns the id of the selected value from the custom lookup. But upon checking the debug logs, same error occurs. Noted on your comment about proper insertion, though.
    – Ertyq
    Jun 25 '20 at 18:52

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.