0

Okay so I am using a lightning datatable and I want to update or more rows of a specific field. The problem is that the table is not refreshing after the records are updating so I feel like I'm getting something wrong... been stuck here for a while.

I used the code found here -> Reference: Display Data in a Table with Inline Editing

apex

@AuraEnabled(cacheable=true)
public static List<Contacts> getContacts(String accId) {
 //.....
 //...
 return conList;
}

@AuraEnabled
public static String updateContacts(Object data) {
    List<Contact> contactsForUpdate = (List<Contact>) JSON.deserialize(
         JSON.serialize(data),
         List<Contact>.class
    );
    try {
        update contactsForUpdate;
        return 'Success: contacts updated successfully';
    }
    catch (Exception e) {
        return 'The following exception has occurred: ' + e.getMessage();
    }
}

js

@wire(getContacts, { accId: '$recordId' })
wiredList({ error, data }) {
  this.wireRes = { error, data };
  if(data) {
   //...
  } else if(error) {
   //...
  }


async handleUpdate(event) {
  this.draftForUpdate = event.detail.draftValues;
  const idsToUpdt = this.draftForUpdate.map(draft => { return { "recordId": draft.Id } });

  try {
        // Pass edited fields to the updateContacts Apex controller
        const result = await updateContacts({data: this.draftForUpdate });
        console.log(JSON.stringify("Apex update result: "+ result));
        this.dispatchEvent(
            new ShowToastEvent({
                title: 'Success',
                message: 'Contact updated',
                variant: 'success'
            })
        );

        // Refresh LDS cache and wires
        getRecordNotifyChange(idsToUpdt);

        // Display fresh data in the datatable
        refreshApex(this.wireRes).then(() => {
            // Clear all draft values in the datatable
            this.draftValues = [];
        });
   } catch(error) {
           this.dispatchEvent(
               new ShowToastEvent({
                   title: 'Error updating or refreshing records',
                   message: error.body.message,
                   variant: 'error'
               })
         );
    };
}

So... The update happens but the list I am passing is cached therefore I get the list of contacts before the update. I thought this method -> getRecordNotifyChange() would basically clear the cache and force the wire method to fire again. Guess I am wrong...? Is there a way to achieve this?

Thank you in advance!

4
  • 3
    This is your problem: wiredList({ error, data }) { this.wireRes = { error, data };. You must not use destructuring. You must hold the exact object you got back from the wire for passing to refreshAlex.
    – Phil W
    Nov 29, 2021 at 12:19
  • @PhilW You should add this as answer rather than a comment 😅
    – sfdcfox
    Nov 29, 2021 at 12:20
  • @sfdcfox I was just finding my previous answer that addressed a similar question :D
    – Phil W
    Nov 29, 2021 at 12:23
  • I could have sworn I saw this one year ago reply but I still missed the point, unboh-liev-a-bohl. Thanks a lot that thing kinda ruined my weekend Nov 29, 2021 at 12:33

0

Browse other questions tagged .