I created a datatable with inline editing but there is an error popups if I try to save it after editing. I open the console and it says "Apex update result: false". Can anyone help me why it returns false?
I appreciate any help you provide.
Apex Class
public class CarAppDataSoql {
@AuraEnabled(cacheable=TRUE)
public static List<Car__c> getCarAppData(String search)
{
String item = '%'+search+'%';
return [SELECT Id, Name, Car_Color__c, Car_Style__c, Brand__c FROM Car__c WHERE Name LIKE: item];
}
@AuraEnabled
public static boolean updaterecord(List<Car__c> carlist){
try {
update carlist;
return true;
} catch(Exception e) {
return false;
}
}
}
HTML
<lightning-input placeholder="Search Car" onkeyup={handleCarKeys}>
</lightning-input><br/>
<div style="height: 15rem;">
<lightning-datatable key-field="id" data={carRecords} columns={CarColumns}
show-row-number-column="false" hide-checkbox-column="false"
onsave={handleSave} draft-values={draftValues}>
</lightning-datatable><br>
<template if:true={error}>{error}</template>
</div>
js
import CarData from '@salesforce/apex/CarAppDataSoql.getCarAppData';
import updateList from '@salesforce/apex/CarAppDataSoql.updaterecord';
import { refreshApex } from '@salesforce/apex';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
searchCar = '';
@track carRecords;
@track CarColumns = getCarColumns;
@track error;
_datatableresp;
draftValues;
handleCarKeys(e)
{
this.searchCar = e.target.value;
}
@wire(CarData,{search : '$searchCar'})
getCarData(result){
this._datatableresp = result;
if(result.data){
this.carRecords = result.data;
}
else{
this.error = result.error;
}
}
handleSave(event){
this.draftValues = event.detail.draftValues;
alert('draf '+JSON.stringify(this.draftValues))
updateList({carList: this.draftValues}).then(result => {
console.log( JSON.stringify( "Apex update result: " + result ) )
if(result == true){
this.dispatchEvent(new ShowToastEvent({
title: 'Success!!',
message: 'successfully Account has been updated',
variant: 'success'
})
);
this.draftValues = []
return refreshApex(this._datatableresp);
} else {
this.dispatchEvent(
new ShowToastEvent({
title: 'Error!!',
message: 'something went wrong please try again',
variant: 'error'
})
);
}
})
}
AlertMessage
draf [{"Car_Color__c":"test","id":"row-0"}]