JSON deserialize is not working.
however, data is coming from LWC in DATA parameter
@AuraEnabled public static string saveProductLineItems(Object data) { system.debug('LINE 18 data:'+ data); List<ProductLineItemWrapper> wrappers = (List<ProductLineItemWrapper>) JSON.deserialize( JSON.serialize(data), List<ProductLineItemWrapper>.class ); system.debug('LINE 23 data:'+ data); system.debug('LINE 24 wrappers:'+ wrappers); List<Product_Line_Item__c> spliToUpdate = new List<Product_Line_Item__c>(); for (ProductLineItemWrapper wrapper : wrappers) { if (wrapper != null && wrapper.oli != null) { spliToUpdate.add(wrapper.oli); } } if (!spliToUpdate.isEmpty()) { update spliToUpdate; return 'SUCCESS'; } return 'FAILED'; } Debug logs -
LINE 18 data:({quantity=533, recordId=a14C40000066JFpIAQ}) LINE 23 data:({quantity=533, recordId=a14C40000066JFpIAQ}) LINE 24 wrappers:(SampleProductLineItemWrapper:[oli=null, productName=null, quantity=533, recordId=a14C40000066JFpIAQ, status=null) please advise
LWC
import { LightningElement, api, wire, track } from 'lwc'; import { ShowToastEvent } from 'lightning/platformShowToastEvent'; import { NavigationMixin } from 'lightning/navigation'; import { refreshApex } from '@salesforce/apex'; import { updateRecord } from "lightning/uiRecordApi"; import getProductLineItems from '@salesforce/apex/SampleProductLineItemController.getSampleProductLineItems'; import saveProductLineItems from '@salesforce/apex/ProductLineItemController.saveProductLineItems'; const columns= [ { label: 'Product Name', fieldName: 'productName' }, { label: 'Sample Quantity', fieldName: 'quantity', editable: true }, ] export default class ProductLineItemList extends NavigationMixin(LightningElement) { columns = columns; data =[]; saveDraftValues = []; @api recordId; @track isShowModal = false; @track rowCount = 0; @wire(getProductLineItems, { opportunityId: '$recordId' }) data(result){ console.log("LINE 24 result:"+JSON.stringify(result)); if(result.error){ this.data = undefined; }else if(result.data){ this.data = result.data; this.rowCount = this.data.length; console.log("LINE 29 this.data:"+JSON.stringify(this.data)); } } handleSave(event) { const updatedfield = event.detail.draftValues; console.log('LINE 36 updatedfield:' + JSON.stringify(updatedfield) ); saveProductLineItems({ data: updatedfield }) .then(result => { if (result === 'SUCCESS') { const toastEvent = new ShowToastEvent({ title: 'Success', message: 'Records saved successfully', variant: 'success', }); this.dispatchEvent(toastEvent); console.log('LINE 48 data:' + JSON.stringify(this.data)); return refreshApex(this.data); } else { const toastEvent = new ShowToastEvent({ title: 'Error', message: 'Failed to save records', variant: 'error', }); this.dispatchEvent(toastEvent); } }) .catch(error => { console.error('err:' + error); }); } /* handleChange(event) { this.selectedStatus = event.detail.value; } */ showModalBox() { this.isShowModal = true; } hideModalBox() { this.isShowModal = false; } get cardTitle() { return `Sample Products Line Items (${this.rowCount})`; } } HTML - (using modal pop-up button) - Below is the html at both places
<template> <lightning-datatable key-field="recordId" data={data} show-row-number-column columns={columns} onsave={handleSave} draft-values={saveDraftValues}> hide-checkbox-column </lightning-datatable> </template>