0

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"}]

enter image description here

3
  • 3
    When you ask for help with an error, you should include the error (in its entirety, without paraphrasing). Unless the issue is obvious, having the error message really helps narrow things down for the people with the knowledge to help you. You should edit your question to include that information. The name of the game here is to make it as easy for people to help you as you can. Any additional hoops you make people go through will reduce the number of people willing to help you.
    – Derek F
    Commented Jul 30, 2022 at 16:32
  • 1
    Please clarify your specific problem or provide additional details to highlight exactly what you need. As it's currently written, it's hard to tell exactly what you're asking.
    – Community Bot
    Commented Jul 30, 2022 at 18:20
  • Sorry for the lack of information. I just updated my question. Commented Jul 30, 2022 at 22:09

0

You must log in to answer this question.

Browse other questions tagged .