I have recently added a new column in a lwc, 'reported due date', editable, in a lightning datatable. I also created a validation rule, which is called in the save event, but when i click on save i get the error 'Sorry to interrupt'. With the VR commented, i dont get any error. If i comment the if condition, i dont get that error anymore. Can someone please tell me what is wrong?
The validation rule code is this:
let hasErrors = false;
let selectedRow = event.detail.selectedRows;
console.log('tabledata2: ', JSON.stringify(this.data2));
if (this.data2.length > 0) {
const firstRow = this.data2[0];
firstRow.invoiceReportedDueDate = '2023-06-01';
console.log('First selected row:', JSON.stringify(firstRow));
// Convert date strings to Date objects
var invoiceReportedDueDate = new Date(firstRow.invoiceReportedDueDate);
var dueDate = new Date(firstRow.dueDate);
// Calculate the difference in milliseconds
var dif = Math.abs(invoiceReportedDueDate - dueDate);
// Calculate the difference in days
var d = dif / (1000 * 3600 * 24);
// Log specific properties from the first selected row
console.log('Color :', JSON.stringify(firstRow.invoiceDunningLevelIcon));
console.log('DueDate:', JSON.stringify(firstRow.dueDate));
console.log('ReportedDueDate:', JSON.stringify(firstRow.invoiceReportedDueDate));
console.log('dif', dif);
console.log('days', d);
}
console.log('draft Value: ', event.detail.draftValues);
const editedRow = [];
for(let i = 0; i < selectedRow.length; i++){
console.log('Reported Due Date', selectedRow[i].invoiceReportedDueDate );
console.log('Due Date', selectedRow[i].dueDate);
console.log('Color1 : ', selectedRow[i].invoiceDunningLevelIcon);
console.log('reportedDueDate : ', selectedRow[i].invoiceReportedDueDate);
if((selectedRows[0].invoiceDunningLevelIcon.includes("dunning-level-green") || selectedRows[0].invoiceDunningLevelIcon.includes("dunning-level-blue")) && d > 30 ){
this.dispatchEvent(
new ShowToastEvent({
title: 'Error',
message: 'Nu ai voie',
variant: 'error'
})
);
}
hasErrors = true;
}
return hasErrors;
}
and the save event:
saveHandleAction(event) {
// Validation
let selectedRow = event.detail.selectedRows;
// Validate the data
this.validationRule(event);
// Save the data if there are no validation errors
try {
this.inputElement = event.detail.draftValues;
console.log('Input Event.DraftValues', event.detail.draftValues);
console.log('Input element', this.inputElement.length);
this.draftValues = [];
console.log('This draftValue: ', this.draftValues);
this.dispatchEvent(
new ShowToastEvent({
title: 'Success',
message: 'Records Updated Successfully!!',
variant: 'success'
})
);
} catch (error) {
console.error('Update error:', error);
this.dispatchEvent(
new ShowToastEvent({
title: 'Error',
message: 'An Error Occured!!',
variant: 'error'
})
);
}
}