LWC Data Table not getting refreshed on click of delete button

datatablelightning-web-componentsrefresh

LWC Data table is not getting refreshed on delete of a row. code below:

import { LightningElement,wire,track,api } from 'lwc';
import distProfileShareList from '@salesforce/apex/DistributorProfileShareLWC.distProfileShareList';
import { deleteRecord } from 'lightning/uiRecordApi';
import { ShowToastEvent } from 'lightning/platformShowToastEvent';
import { getRecord } from "lightning/uiRecordApi";
import { refreshApex } from '@salesforce/apex';
import deleteDistributorBPFocusAreaShareRecords from '@salesforce/apex/DistributorProfileShareLWC.deleteDistributorBPFocusAreaShareRecords';
const columns = [
    {
         label: 'User',
         fieldName: 'User',
         type: 'text',
         fixedWidth: 300
     },
      {
         label: 'AccessLevel',
         fieldName: 'AccessLevel',
         type: 'text',
         fixedWidth: 300
     },
     {
        label: 'Id',
        fieldName: 'Id',
        type: 'text',
        fixedWidth: 300,
        hideLabel:true
    },
    {
        label: 'RowCause',
        fieldName: 'RowCause',
        type: 'text',
        fixedWidth: 300,
        hideLabel:true
    },
 ] 
export default class DistributorProfileShare extends LightningElement {
    @track columns = columns;
    @track data=[];
    @api recordId; 
    @track distributorProfileRec;
    @track userId;
    @track accessLevel;
    @track selectedRecIdValue;
    @track dpShareRecords = [];
    @api distributorProfileId;
    @api strRecordId;
    @api selectedDistributorProfileIdList=[];
    @track errorMsg;
    @track recordsCount = 0;
    refreshTable;
    @wire(distProfileShareList,{ distributorProfileId: '$strRecordId'}) 
    distributorProfileRec({ error, data }) {
        this.refreshTable=data;
        if(data) {
            let currentData = [];
            this.data = data.map((row) => ({ 
                User: row.UserOrGroup.Name,
                AccessLevel : row.AccessLevel,
                Id: row.Id,
                RowCause:row.RowCause}));
                console.log('###### allData is ######### '+ JSON.stringify(data)); 
        }
        else if(error) {
            window.console.log(error);
        }
    }
    getSelectedIdAction(event){
        const selectedRows = event.detail.selectedRows;
        window.console.log('selectedRows# ' + JSON.stringify(selectedRows));
        for (let i = 0; i<selectedRows.length; i++){
            alert("You selected: " + selectedRows[i].Id);
             this.selectedDistributorProfileIdList.push(selectedRows[i].Id);
        }
    }
    deleteDistributorShareRowAction(){
       deleteDistributorBPFocusAreaShareRecords({recordIds:this.selectedDistributorProfileIdList}) 
       .then(()=>{
        this.template.querySelector('lightning-datatable').selectedRows=[];
            const toastEvent = new ShowToastEvent({
                message:'Record deleted successfully',         
                variant:'success'
            });
            window.alert('Record Has been deleted on line 83 ### ');
            this.dispatchEvent(toastEvent);
            return refreshApex(this.refreshTable); // this method not working.
        })
        .catch(error =>{
            this.errorMsg =error;
            window.console.log('unable to delete the record due to ' + JSON.stringify(this.errorMsg));
        });
    }
}

Best Answer

distributorProfileRec({ error, data }) {
    this.refreshTable=data;

Should be:

distributorProfileRec(response) {
    this.refreshTable = response;
    const { data, error } = response;

In most cases, you only care about data and error, but if you need to use refreshApex, you need the entire response, which includes the information that refreshApex needs to call Apex and refresh the cache. This is true for both imperative and wire Apex calls.