0

I have a save and a cancel, both triggered by a button click:

saveCriteria(){
    let criteriaToSave = [];
    //reassemble drill up/down fields on the criteria
    for(let i = 0; i < this.tableData.length; i++){
        criteriaToSave.push(parseObjectFieldsForDrillDown(this.tableData[i]));
    }
    //pass tableData to apex to save.
    saveCriteriaUpdates({criteriaJSON: JSON.stringify(criteriaToSave), criteriaApiName: this.objectName})
        .then(result => {
            console.log(result);
            return refreshApex(this.wiredResults);
            //Show toast on success/error
        }).catch(error => {
            console.log(JSON.stringify(error));
            //Show toast on error
        });
}

cancelUpdates(){
    return refreshApex(this.wiredResults);
}

The refreshApex() works in the then callback of the apex method call on save. I see my debugs from my wire function after saving again.

But when I just click cancel, I want to refresh my data to its original state from the server to wipe out all changes, so want to do a refreshApex(). But in the cancel, the refreshApex does nothing. I don't see my debugs from my wire function. No debugs from the error part of the wire function... my table does not refresh back to its original state... nothing.

Is there some trick to refreshApex I am missing? It seems to work in an asynchronous callback, but not a direct call like my cancel button...

PS: My wire function and extra property for refresh looks like this:

wiredResults;

@wire(getSObjectsForTable, {stepId: '$stepId', objectApiName: '$objectName'})
getSObjectsForTable(result){
    this.wiredResults = result;
    if(result.data){
        console.log(result.data);
        this.tableData = getTableData(result.data);
        this.tableColumns = getColumns(this.tableData, allColumns);
    } else if(result.error){
        console.log(result.error);
        let toastEvent = new ShowToastEvent({
            title: 'Error',
            message: 'Error retrieving records: ' + result.error,
            variant: 'error'
        });
        this.dispatchEvent(toastEvent);
    }
    this.tableLoaded = true;
}
2
  • Yes, that question answers how to do it exactly like I am doing it, and it works in an asynch callback from apex, but does nothing when I call refreshApex on button click. So... – Tyler Dahle Mar 23 '19 at 2:52
  • 1
    Most functions in LWC (wires specifically) return a promise, however according to the docs there is no promise on this function. Therefore there is no callback, so it doesn't return anything. So, like the recorded question, don't use a return. – tsalb Mar 23 '19 at 3:54