How Can I Refresh Datatable in Wire Using refreshApex

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

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.

Robert Thorne
Author

Robert Thorne

Robert Thorne covers electric vehicle innovations, autonomous driving systems, global mobility trends, and automotive engineering developments.