0

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>
3
  • You need this data as List of Wrapper or single Wrapper
    – A.K.
    Aug 30, 2023 at 10:25
  • Please add your LWC code as well to answer better
    – A.K.
    Aug 30, 2023 at 11:03
  • sure..thank you for all your help
    – SF Dev
    Aug 30, 2023 at 13:58

1 Answer 1

0

In your code you are having 3 column and when you will edit quantity. You will get that field and you can edit that field. If you need to add other data then you have to add that data in your datatable.

In you Apex Code you need to update records

    List<Sample_Product_Line_Item__c> spliToUpdate = new List<Sample_Product_Line_Item__c>();
    for (SampleProductLineItemWrapper wrapper : wrappers) {
    
         if (wrapper.quantity != null) {
             spliToUpdate.add(new Sample_Product_Line_Item__c(Id = wrapper.recordId, quantity = wrapper.quantity)); 
         }
     }
    
    if (!spliToUpdate.isEmpty()) {
        update spliToUpdate;
        return 'SUCCESS';
    }
6
  • Its a list of wrapper.. with this approach... the values are coming null. List<SampleProductLineItemWrapper> wrappers = (List<SampleProductLineItemWrapper>) JSON.deserialize( JSON.serialize(data), List<SampleProductLineItemWrapper>.class );
    – SF Dev
    Aug 30, 2023 at 10:44
  • here is my wrapper class... public class SampleProductLineItemWrapper { AuraEnabled public Sample_Product_Line_Item__c oli { get; set; } AuraEnabled public String productName { get; set; } AuraEnabled public Decimal quantity { get; set; } AuraEnabled public String recordId { get; set; } public SampleProductLineItemWrapper(Sample_Product_Line_Item__c oli) { this.oli = oli; this.quantity = oli.Quantity__c; this.recordId = oli.Id; } }
    – SF Dev
    Aug 30, 2023 at 10:49
  • currently only Quantity field is editable=true..
    – SF Dev
    Aug 30, 2023 at 15:09
  • yes that is the reason you are getting that property in your wrapper. oil is coming blank because you did not update this.
    – A.K.
    Aug 30, 2023 at 15:26
  • I hope above will help
    – A.K.
    Aug 31, 2023 at 5:13

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .