You need to sign in to do that
Don't have an account?
Naveen 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; } }
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
HTML
<template>
<lightning-card >
<h2> Subscribe Blogs</h2>
<lightning-input class="first" type="email" name="txtFname" onchange={handleChange}></lightning-input>
<lightning-button variant="brand" label="Subscribe" title="Submit" onclick={saveRecord} class="slds-m-left_x-small" style="border-radius: 7vw !important;"></lightning-button>
</lightning-card>
</template>
JS
import { LightningElement , api ,track} from 'lwc';
import email_field from '@salesforce/schema/Subscriber__c.Email__c'
import saveRegRecord from '@salesforce/apex/subscribe.createRecord';
export default class Subscribe extends LightningElement {
@track regRecord = {
fName : email_field
};
email;
handleChange(event) {
this.email = event.target.value;
}
saveRecord(event)
{
saveRegRecord({email : this.email})
.then(result => {
// Clear the user enter values
console.log(this.regRecord)
this.conrec = {};
window.console.log('result ===> '+result);
alert('You have Succesfully Subscribed to us');
})
.catch(error => { this.error = error.message;
console.log('error==>'+this.error);
alert('record not inserted');
});
}
}
Apex
public with sharing class subscribe {
@AuraEnabled
public static void createRecord (string email){
if(email!=null){
insert new Subscriber__c(email__c = email);
}
}
}