function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Jack AndrewJack Andrew 

Table is not refreshing even after using the refreshApex

I am new to LWC and trying to fetch the records in the Lightning Datatable, which is working fine. After that i am trying to insert a record into the same using the record edit form and using refreshApex to display the inserted record immediately.
Can someone help me with the same?
PFA the code snippet for the same
@wire (getAccData,{recordId: '$recordId'})
    getAccTable({data, error}){
        if(data){   
            this.tableData = data;
        }
        else{
            console.log(error);
        }
    }

Here, i am storing the data returned from apex in tableData and then calling the same table data in refreshApex. But it's not working.

handleSave(){
        insertAcc({name : this.name})
        .then((result) => {
          this.result = result;
          return refreshApex(this.tableData);
        })
        .catch((error) => {
          this.error = error;
        });
    }
Best Answer chosen by Jack Andrew
Arun Kumar 1141Arun Kumar 1141

Hey Jack,

Instead of data, try using the whole result. Please refer to the below code.

@wire (getAccData,{quoteId: '$recordId'})
    getAccTable(result){
        this.resultWired = result;
        if(result.data){   
            this.tableData = result.data;
        }
        else{
            console.log(result.error);
        }
    }


handleSave(){
        insertAcc({name : this.name})
        .then((result) => {
          this.result = result;
          return refreshApex(this.resultWired);
        })
        .catch((error) => {
          this.error = error;
        });
        
    }

Please mark it as the best answer, if it helps.
Thanks

All Answers

Arun Kumar 1141Arun Kumar 1141

Hey Jack,

Instead of data, try using the whole result. Please refer to the below code.

@wire (getAccData,{quoteId: '$recordId'})
    getAccTable(result){
        this.resultWired = result;
        if(result.data){   
            this.tableData = result.data;
        }
        else{
            console.log(result.error);
        }
    }


handleSave(){
        insertAcc({name : this.name})
        .then((result) => {
          this.result = result;
          return refreshApex(this.resultWired);
        })
        .catch((error) => {
          this.error = error;
        });
        
    }

Please mark it as the best answer, if it helps.
Thanks
This was selected as the best answer
Kunal Bhardwaj 011Kunal Bhardwaj 011
Also make sure you are importing the JS Utility:  import { refreshApex } from '@salesforce/apex';