0

I have a lwc

html file code :

 <template if:true={sObjectRecordList}> 
    <lightning-datatable key-field="Id"
                        data={sObjectRecordList}
                        columns={columns}
                        onsave={handleSave}
                        draft-values={draftValues}>
    </lightning-datatable>
</template>

Now I am fetching and updating the records in the datatable using apex class methods

 /*The apex code which fetches the datatable records*/
 
 @wire(setSObjectRecordList , { sObjectName : 'Contact',
                               sObjectFieldList :  '$sObjectFieldDetails',
                               parentRecordId : '$recordId',
                               parentRecordIdFieldName : 'AccountId'
                              })    
sObjectRecords({error, data }) {
    if (data) {
        this.sObjectRecordList = data;
    } else if (error) {
        this.error = error;
    }
}


 /*The apex code for updating the records edited in the datatable*/

 async handleSave(event) {
    const updatedFields = event.detail.draftValues;
    
    // Prepare the record IDs for getRecordNotifyChange()
    const notifyChangeIds = updatedFields.map(row => { return { "recordId": row.Id } });

   // Pass edited fields to the updateContacts Apex controller
    await dmlOperationOnSObjectRecords({data: updatedFields})
    .then(result => {
        this.dispatchEvent(
            new ShowToastEvent({
                title: 'Success',
                message: 'Records updated',
                variant: 'success'
            })
        );

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

    // Display fresh data in the datatable        
      refreshApex(this.sObjectRecordList);     
    
   }).catch(error => {
       this.dispatchEvent(
            new ShowToastEvent({
                title: 'Error updating or refreshing records',
                message: error.body.message,
                variant: 'error'
            })
        );
    });
}

My records are getting updated via the methods.

However, the data is not getting refreshed in the datatable using the refreshApex method, despite using a separate variable while fetching the records for the datatable.

2 Answers 2

1

Assign the whole response to a property, and then refresh it using refreshApex.

tempVariable;
sObjectRecords(value) {
    this.tempVariable = value;
    if (data) {
        this.sObjectRecordList = data;
    } else if (error) {
        this.error = error;
    }
}

...

refreshApex(this.tempVariable);
1
  • you are right, using the temp variable works. It has to be written like sObjectRecords(value) instead of sObjectRecords({value }).
    – Madhurima
    Commented Jan 3, 2021 at 12:46
0

Make sure sObjectRecordList has been decorated with @track.

Since Sprint'20, it is not mandatory to make primitive properties reactive, but Objects and Arrays still must be declared reactive.

@track sObjectRecordList = [];

You must log in to answer this question.

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