I have a custom LWC Page which is used as Public site.

In my Page I have form which will populate the data in Contact object and display the value in Data table.

I have integrated inline edit in my data table by Using updateRecord, I'm getting Invalid Privileges.

So i was trying to update the field value by calling Apex Controller.

 <lightning-datatable key={stu.Id} key-field="Id" data={stu.spitems} columns={studentProgram}
                                data-record-id={studentsData.studentId} draft-values={draftValues}
                                onsave={handleInlineEdit} show-row-number-column=false>

handleInlineEdit(event) {
    var draftValuesStr = JSON.stringify(event.detail.draftValues);

I'm editing the two row's value in the data table, On click save calling the handleInlineEdit method, draftValuesStr have JSON value like Below

    "StartDate": "2020-05-10",
    "Id": "a022w0000033ckoTFC"
    "StartDate": "2020-05-03",
    "Id": "a022w0000033ckpFDC"

I can pass this draftValuesStr to APEX controller, my question here is - How we can update the value in each row by Id in APEX. Please help me on this.


If you mean how to deserialize draftValueStr for updating existing Contacts, then it would be better to create wrapper class and use JSON.deserialize() method:

public class ContactWrapper {
    public String StartDate;
    public String Id;

String draftValueStr = '[{"StartDate": "2020-05-10","Id": "a022w0000033ckoTFC"},{"StartDate": "2020-05-03","Id": "a022w0000033ckpFDC"}]';

List<ContactWrapper> drafts = (List<ContactWrapper>) JSON.deserialize(draftValueStr, List<ContactWrapper>.class);

Then iterate the list to get ids, select contacts and update them:

Set<Id> ids = new Set<Id>();
for (ContactWrapper draft : drafts) {
    ids.add((Id) draft.Id);
Map<Id, Contact> contacts = new Map<Id, Contact>([SELECT StartDate FROM Contact WHERE Id IN :ids]);

for (ContactWrapper draft : drafts) {
    if (contacts.get(draft.Id) != null) {

        // there we need some date formatting
        // to get Date from draft.StartDate
        Date draftDate = someFunctionToFormatDate(draft.StartDate);

        contact.get(draft.Id).StartDate = draftDate;
update contacts.values();

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.