0

In my LWC I have:

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);
    } 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;
}

I have a save and cancel button that fires events where I want to do refreshApex just to grab fresh data from the server.

My save:

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

This works. The refresh apex actually causes the wire function to execute again.

My cancel:

cancelUpdates(){
    this.showButtons = false;
    refreshApex(this.wiredResults);
}

This does not work. I have tried returning the refreshApex in the cancel as well with no luck. It seems to be fine when called in the promise of an Apex method call... but not working when just... called. It just does nothing, there aren't even errors in my wire function, it does not refresh it at all.

How can I do a refreshApex on button click outside of an Apex method call promise?

4
  • When you say it does not refresh, are you saying that cancelUpdates() is never called? Have you tried putting some logs in that function? Also if you want to re-render your component, you will need to utilize a tracked property, do you have one?
    – Jayant Das
    Apr 5, 2019 at 15:50
  • CancelUpdates is called. When I trigger saveCriteria with my button, the console logs from my wire function appear in my Dev console. When I click cancel and trigger cancelUpdates, the function triggers, but my wire function is silent. I've placed debugs all over my wire function in case there was some error, but nothing, it just doesn't refresh it. Apr 5, 2019 at 15:58
  • Actually I realize that I had encountered same thing that console logs don't work as expected in imperative refresh but the function does get invoked, most likely it just utilizes the cache. Do you have a tracked property to make sure you are re-rendering the data?
    – Jayant Das
    Apr 5, 2019 at 16:16
  • I think your issue may be related to this question. Take a look and see if the answer there works for you.
    – Jayant Das
    Apr 5, 2019 at 16:22

1 Answer 1

2

This is happening because the underlying cached data has not changed. The javascript wire method will only be called if it detects a change in the data returned.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .