You need to sign in to do that
Don't have an account?

how to insert record in contact Object using Lightning Web Components
Hi,
I am new to Web components and When ever i am trying to insert a record in Contact Object it was saving with only FirstName,Lastname,MiddleName but i am getting Input in Js but it was not going to ApexClass why?? Can i reason Why??
HTML File:
<template>
<lightning-card title="Insert Contact" icon-name="standard:contact">
<div class="slds-p-around_x-small">
<lightning-input label="FirstName" value={rec.FirstName} onchange={handleFirstNameChange}></lightning-input>
<lightning-input label="LastName" value={rec.LastName} onchange={handleLastNameChange}></lightning-input>
<lightning-input type="text" label="Physical" value={rec.Physical} onchange={handlePhysicalChange}></lightning-input><br/>
<lightning-button label="Save" onclick={handleClick}></lightning-button>
</div>
</lightning-card>
</template>
JS File:
import { LightningElement,track} from 'lwc';
// Importing Apex Class method
import createContact from '@salesforce/apex/insertContactApexWeb.saveAccountRecord';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
import FIRSTNAME_FIELD from '@salesforce/schema/Contact.FirstName__c';
import LASTNAME_FIELD from '@salesforce/schema/Contact.Last_Name__c';
import PHYSICAL_FIELD from '@salesforce/schema/Contact.Physical__c';
export default class InsertAddPersonContact extends LightningElement {
@track firstname = FIRSTNAME_FIELD;
@track lastname = LASTNAME_FIELD;
@track physical = PHYSICAL_FIELD;
rec = {
FirstName : this.firstname,
LastName : this.lastname,
Physical : this.physical
}
handleFirstNameChange(event) {
this.rec.FirstName = event.target.value;
window.console.log("FirstName", this.rec.FirstName);
}
handleLastNameChange(event) {
this.rec.LastName = event.target.value;
window.console.log("LastName", this.rec.LastName);
}
handlePhysicalChange(event) {
this.rec.PhysicalAttributes = event.target.value;
window.console.log("Physical", this.rec.Physical);
}
handleClick() {
createContact({ con : this.rec })
.then(result => {
this.message = result;
this.error = undefined;
if(this.message !== undefined) {
this.rec.Name = '';
this.rec.Industry = '';
this.rec.Phone = '';
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Account created',
variant: 'success',
}),
);
}
window.console.log(JSON.stringify(result));
window.console.log("result", this.message);
})
.catch(error => {
this.message = undefined;
this.error = error;
this.dispatchEvent(
new ShowToastEvent({
title: 'Error creating record',
message: error.body.message,
variant: 'error',
}),
);
window.console.log("error", JSON.stringify(this.error));
});
}
}
Apex Class
@AuraEnabled
public static void saveContactRecord(Contact con){
System.debug('acc--'+con);
try{
insert con;
}
catch(Exception ex) {
throw new AuraHandledException(ex.getMessage());
}
}
I am new to Web components and When ever i am trying to insert a record in Contact Object it was saving with only FirstName,Lastname,MiddleName but i am getting Input in Js but it was not going to ApexClass why?? Can i reason Why??
HTML File:
<template>
<lightning-card title="Insert Contact" icon-name="standard:contact">
<div class="slds-p-around_x-small">
<lightning-input label="FirstName" value={rec.FirstName} onchange={handleFirstNameChange}></lightning-input>
<lightning-input label="LastName" value={rec.LastName} onchange={handleLastNameChange}></lightning-input>
<lightning-input type="text" label="Physical" value={rec.Physical} onchange={handlePhysicalChange}></lightning-input><br/>
<lightning-button label="Save" onclick={handleClick}></lightning-button>
</div>
</lightning-card>
</template>
JS File:
import { LightningElement,track} from 'lwc';
// Importing Apex Class method
import createContact from '@salesforce/apex/insertContactApexWeb.saveAccountRecord';
import {ShowToastEvent} from 'lightning/platformShowToastEvent';
import FIRSTNAME_FIELD from '@salesforce/schema/Contact.FirstName__c';
import LASTNAME_FIELD from '@salesforce/schema/Contact.Last_Name__c';
import PHYSICAL_FIELD from '@salesforce/schema/Contact.Physical__c';
export default class InsertAddPersonContact extends LightningElement {
@track firstname = FIRSTNAME_FIELD;
@track lastname = LASTNAME_FIELD;
@track physical = PHYSICAL_FIELD;
rec = {
FirstName : this.firstname,
LastName : this.lastname,
Physical : this.physical
}
handleFirstNameChange(event) {
this.rec.FirstName = event.target.value;
window.console.log("FirstName", this.rec.FirstName);
}
handleLastNameChange(event) {
this.rec.LastName = event.target.value;
window.console.log("LastName", this.rec.LastName);
}
handlePhysicalChange(event) {
this.rec.PhysicalAttributes = event.target.value;
window.console.log("Physical", this.rec.Physical);
}
handleClick() {
createContact({ con : this.rec })
.then(result => {
this.message = result;
this.error = undefined;
if(this.message !== undefined) {
this.rec.Name = '';
this.rec.Industry = '';
this.rec.Phone = '';
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Account created',
variant: 'success',
}),
);
}
window.console.log(JSON.stringify(result));
window.console.log("result", this.message);
})
.catch(error => {
this.message = undefined;
this.error = error;
this.dispatchEvent(
new ShowToastEvent({
title: 'Error creating record',
message: error.body.message,
variant: 'error',
}),
);
window.console.log("error", JSON.stringify(this.error));
});
}
}
Apex Class
@AuraEnabled
public static void saveContactRecord(Contact con){
System.debug('acc--'+con);
try{
insert con;
}
catch(Exception ex) {
throw new AuraHandledException(ex.getMessage());
}
}
You are using import createContact from '@salesforce/apex/insertContactApexWeb.saveAccountRecord';
While your class method name is saveContactRecord