import { LightningElement, track } from 'lwc';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { createRecord } from 'lightning/uiRecordApi';
import { reduceErrors } from 'c/ldsUtils';
import OPPORTUNITY_OBJECT from '@salesforce/schema/Opportunity';
import NAME_FIELD from '@salesforce/schema/Opportunity.Name';
import CloseDate_FIELD from '@salesforce/schema/Opportunity.CloseDate';
import StageName_FIELD from '@salesforce/schema/Opportunity.StageName';
export default class LdsCreateOpportunity extends LightningElement {
@track opportunityId;
// errorInfo = undefined;
name = '';
closeDate = new Date('December 17, 2019 03:24:00');
stageName = 'Prospecting';
// @track error;
handleNameChange(event) {
this.opportunityId = undefined;
this.name = event.target.value;
}
createOpportunity() {
const fields = {};
fields[NAME_FIELD.fieldApiName] = this.name;
fields[CloseDate_FIELD.fieldApiName] = this.closeDate;
// fields[StageName_FIELD.fieldApiName] = this.stageName; // - this breaks the save by omitting the required stage name field
const recordInput = { apiName: OPPORTUNITY_OBJECT.objectApiName, fields };
createRecord(recordInput)
.then(opportunity => {
this.opportunityId = opportunity.id;
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Opportunity created. Message goes here!',
variant: 'success'
})
);
})
.catch(error => {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error creating record',
message:'Here is the error:' + error.body + ' reduce: ' + reduceErrors(error).join(', ') + 'status:' + error.status + ' ;status text' + error.statustext,
variant: 'error'
}
)
);
});
}
}
Found another way to debug this - needed to enable Debug mode in Salesforce and also needed to use Chrome Dev Tools.Here is the link: LWC Debugging (https://developer.salesforce.com/docs/component-library/documentation/lwc/lwc.debug_mode_enable). Still am surprised that the actual error that shows is just status:400 undefined