How can I refresh datatable in Wire using refreshApex

 @wire(_getContacts,{recordId:'$recordId'}) wiredContacts({error,data}){ this.dataToRefresh = data; if (data) { this.contacts = this.dataToRefresh.recordList; this.ContactsRecords = this.dataToRefresh.cList; this.contactsSize = " Case Contacts (" + this.contacts.length + ")"; }else{ // } }; 
relateContacts() { this.showSpinner = true; this.showtable=false; relateContacts({contacts: this.selected, recordId: this.recordId}) .then(data => { this.showSpinner=false; this.showtable=true; this.showSuccessMessage(); refreshApex(this.dataToRefresh); //location.reload(); this.isShowModal = false; }) .catch(error => { console.log(error); this.showSpinner=false; const evt = new ShowToastEvent({ title: 'Application Error', message: error.body.message, variant: 'error', mode: 'sticky' }); this.dispatchEvent(evt); this.showSpinner = false; }); } 

For this code, I tried refreshApex with all possible ways. but I'm not sure the miss here. I've Checked all the blogs but everywhere, the solution is mentioned.

Tried refreshApex like below :

@wire(_getContacts,{recordId:'$recordId'}) wiredContacts({data}){ this.dataToRefresh = data; But this also does not work 

1 Answer

Ah that is a fun one ! Your issue is using destructuring in wiredContacts as the parameter. (The {data} or {data,error} normally works as a parameter to the function being called back, except if you have to do refresh)
Try this instead.

@wire(_getContacts,{recordId:'$recordId'}) wiredContacts(value){ this.dataToRefresh = value; const {data, error} = value; //Rest of you code now with data and error } 

Then in your other method you can do:

method(){ refreshApex(this.dataToRefresh); } 

Salesforce does show doing this in their example code, but it’s easy to miss and experience the fun you have been having with this.

See the last example on their page.

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

You Might Also Like