I am trying to using lightning/uiRecordApi in lightning-datatable to display a list of record and to inline edit some fields and save. In the draftValues I am not getting the Id of the record but I am getting Id as row-0,row-1. Draft values:
[ { "ID_Act_Bill_Qty__c": "7", "Id": "row-0" }, { "ID_Act_Bill_Qty__c": "6", "Id": "row-1" } ] I used Id for key-field="Id" not the id. Below is my code.
<lightning-datatable key-field="Id" data={records} columns={columns} onsave={handleSave} draft-values={draftValues} > </lightning-datatable> @wire(getProducts, { invoiceId: "$recordId" })productList({error,data}){ if(data){ this.records = data; this.error = undefined; // to show product name let currentData = []; data.forEach((row)=> { let rowData = {}; rowData.ID_Act_Bill_Qty__c = row.ID_Act_Bill_Qty__c; rowData.ID_Quantity_Invoice__c = row.ID_Quantity_Invoice__c; rowData.ID_Net_Value_Bill_Item__c = row.ID_Net_Value_Bill_Item__c; rowData.ID_Remarks__c = row.ID_Remarks__c; if (row.ID_Product__c) { console.log('product Data found'); rowData.ProductCode = row.ID_Product__r.ProductCode; rowData.Description = row.ID_Product__r.Description; } currentData.push(rowData); }); this.records = currentData; console.log('records - '+JSON.stringify(this.records)); }; }; async handleSave(event) { // Convert datatable draft values into record objects console.log('draft values -'+JSON.stringify(event.detail.draftValues)); const records = event.detail.draftValues.slice().map((draftValue) => { const fields = Object.assign({}, draftValue); console.log('fields - '+JSON.stringify(fields)); return { fields }; }); // Clear all datatable draft values this.draftValues = []; try { // Update all records in parallel thanks to the UI API console.log('before update record'); console.log('records update record'+JSON.stringify(records)); const recordUpdatePromises = records.map((record) => updateRecord(record)); console.log('after update record'); await Promise.all(recordUpdatePromises); // Report success with a toast this.dispatchEvent( new ShowToastEvent({ title: "Success", message: "Iventory updated", variant: "success" }) ); // Display fresh data in the datatable await refreshApex(this.records); } catch (error) { this.dispatchEvent( new ShowToastEvent({ title: "Error updating or reloading Inventory", message: error.body, variant: "error" }) ); } }
this.records) passed to the datatable come from? Could you please edit the question adding that code?