51 lines
1.4 KiB
JavaScript
51 lines
1.4 KiB
JavaScript
import { LightningElement, track } from 'lwc';
|
|
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
|
|
import createContact from '@salesforce/apex/ContactCreator.createContact';
|
|
|
|
export default class CreateContact extends LightningElement {
|
|
@track contact = {
|
|
fname: '',
|
|
lname: '',
|
|
email: ''
|
|
};
|
|
|
|
handleChange(event) {
|
|
const field = event.target.name;
|
|
this.contact[field] = event.target.value;
|
|
}
|
|
|
|
handleSave() {
|
|
const { fname, lname, email } = this.contact;
|
|
console.log('debug',this.contact);
|
|
if(!fname || !lname || !email) {
|
|
this.showToast('Error', 'First Name, Last Name and Email cannot be empty', 'error');
|
|
} else {
|
|
createContact({ fname, lname, email })
|
|
.then(result => {
|
|
this.resetContact();
|
|
this.showToast('Success', 'Contact created', 'success');
|
|
})
|
|
.catch(error => {
|
|
this.showToast('Error creating record', 'A contact with this email already exists', 'error');
|
|
});
|
|
}
|
|
}
|
|
|
|
showToast(title, message, variant) {
|
|
this.dispatchEvent(
|
|
new ShowToastEvent({
|
|
title,
|
|
message,
|
|
variant
|
|
})
|
|
);
|
|
}
|
|
|
|
resetContact() {
|
|
this.contact = {
|
|
fname: '',
|
|
lname: '',
|
|
email: ''
|
|
};
|
|
}
|
|
} |