+ Start a Discussion
Naveen SanaNaveen Sana 

how to insert a new record into custom object in LWC

I want to insert all the input field values of a template into custom fields of an object in LWC. Could anyone please give me a solution. Below is the code which I have written so far. But I come up with this error(insert failed. First exception on row 0; first error: INVALID_TYPE_ON_FIELD_IN_RECORD, First_Name: value not of required type: {fieldApiName=First_Name__c, objectApiName=Registration__c}: [First_Name__c])
 
    HTML:
    <lightning-input type="text" name="txtFname" label="Enter First Name" onchange={handleChange} value={regRecord.fName}></lightning-input>
      <lightning-button variant="success" label="Submit" title="Submit" onclick={saveRecord} class="slds-m-left_x-small"></lightning-button>
    
    Javascript:
    
    import FirstName_FIELD from '@salesforce/schema/Registration__c.First_Name__c';
    import saveRegRecord from '@salesforce/apex/regClass.createRecord';
    
    @track regRecord = {
            fName : FirstName_FIELD
        };
    
        handleChange(event) {
            const field = event.target.name;
            if (field === 'txtFname') {
                console.log(event.target.value);
                this.regRecord.fName = event.target.value;            
            } 
        }
    
        saveRecord(event)
        {   
            saveRegRecord({reg : this.regRecord})
            .then(result => {
                // Clear the user enter values
                console.log(this.regRecord)
                this.conrec = {};            
                window.console.log('result ===> '+result);
                alert('record inserted');
            })
            .catch(error => {
                this.error = error.message;
                console.log('error==>'+this.error);
                alert('record not inserted');
            });
        }

    Apex:
    @AuraEnabled
        public static void createRecord (Registration__c reg){  
                if(reg != null){
                    insert reg;
            } 
        }

 
Siva SakthiSiva Sakthi
Hi Naveen,
     In every custom object (Registration__c - Your Custom Object ) Name field is required. Include the Name filed or any other required fields into your code UI level to insert. Hope this helps you. Please mark this answer as best so that others facing the same issue will find this information useful.

Thanks
Siva