I am trying to create record for an object using LWC. But whenever I am trying to paste the component on a page it is showing up a component error as shown below:
My code as shown below:
HTML ->
<template>
<lightning-card title="NEW TEACHER FORM">
<div class="slds-p-around_x-small">
<lightning-input label="Teacher Name" value={Name} onchange={handleNameChange}></lightning-input>
<lightning-input label="Teacher Salary" value={Salary} onchange={handleSalaryChange}></lightning-input>
<lightning-input label="Email" value={Email} onchange={handleEmailChange}></lightning-input>
<lightning-input label="Phone" value={Contact} onchange={handlePhoneChange}></lightning-input><br/>
<lightning-button label="Save" onclick={handleClick}></lightning-button>
</div>
</lightning-card>
</template>
JS ->
import { LightningElement, track } from 'lwc';
import TEACHER_OBJECT from '@salesforce/schema/Teacher__c';
import NAME_FIELD from '@salesforce/schema/Teacher__c.Name';
import PHONE_FIELD from '@salesforce/schema/Teacher__c.Contact_Number__c';
import EMAIL_FIELD from '@salesforce/schema/Teacher__c.Email__c';
import SALARY_FIELD from '@salesforce/schema/Teacher__c.Salary__c';
import newTeacher from '@salesforce/apex/createTeacher.newTeacher';
export default class CreateTeacherRecord extends LightningElement {
@track error;
@track teacherRec ={
NAME_FIELD:Name,
EMAIL_FIELD:Email,
PHONE_FIELD:Contact,
SALARY_FIELD:Salary}
handleNameChange(event){
this.teacherRec.Name = event.taget.value;
}
handleSalaryChange(event){
this.teacherRec.Salary = event.taget.value;
}
handleEmailChange(event){
this.teacherRec.Email = event.taget.value;
}
handlePhoneChange(event){
this.teacherRec.Contact = event.taget.value;
}
handleClick(event){
newTeacher({newTeacherRec:this.teacherRec})
.then(result=>{
this.teacherRec={};
})
.catch(error=>{
this.error = error.message;
});
}
}
XML ->
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>52.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
<target>lightning__AppPage</target>
<target>lightning__HomePage</target>
</targets>
</LightningComponentBundle>
APEX ->
public with sharing class createTeacher {
@AuraEnabled
public static Teacher__c newTeacher(Teacher__c newTeacherRec) {
newTeacherRec.Name = '';
insert newTeacherRec;
return newTeacherRec;
}
}
If I forcefully add the component on the page, then the page never reloads.