2

Using wire decorator I am calling Apex method on load of the page. Same wired method I want to call on reset button. I have a requirement on click of reset button Page should show ONLOAD data and reset all the selection in lightning combo box. following is the HTML and JS part of my component. HTML

<lightning-button-icon  icon-name="utility:refresh" alternative-text="Reset" 
                        class="slds-p-around_xx-small" onclick={refreshTable}>
</lightning-button-icon>

JS

@track selectedyear;
@track selectedmonth;

@wire(fetchAccdata, {year : '$selectedyear', month :'$selectedmonth'})
AccountData({error,data}){
    if(data){
        this._accwisedata=data;
    }
    else if(error){
        console.error('Error:\n',error);
    }
}

refreshTable(event){
    this.selectedacc = null;
    this.Name= null;
    this.selectedmonth=this._data.currentMon;
    this.selectedyear=this._data.currentYr;
    this.AccountData();// I have tried this and following refereshApex option.
    //refreshApex(AccountData);
    console.log('I am in Reset Table EVENTTTTTTTTTTTTTTTTTTTTTTT');
}

1 Answer 1

3

You can use refreshApex for this (in your code you are not using it right):

To refresh Apex data provisioned via an Apex @wire, call refreshApex(). The function provisions the data using the configuration bound to the @wire and updates the cache.

Link : Refresh the Cache When Using @wire

So your Code should look in the next way :

Note:

The parameter you refresh with refreshApex() must be an object that was previously emitted by an Apex @wire.

wiredAccounts;
_accwisedata;

@wire(fetchAccdata, {year : '$selectedyear', month :'$selectedmonth'})
setAccountData(value){
    // Hold on to the provisioned value so we can refresh it later.
    this.wiredAccounts = value;
    const { data, error } = value;
    if(data){
        this.accountData = data;
    } else if(error){
        console.error('Error:\n',error);
    }
}

refreshTable(event){
    this.selectedacc = null;
    this.Name= null;
    this.selectedmonth=this._data.currentMon;
    this.selectedyear=this._data.currentYr;

    // here you refresh the wiredAccounts parameter
    refreshApex(this.wiredAccounts).then(() => {
         // do something with the refreshed data in this.accountData
    }); 
}

Do not forget to import refreshApex into your LWC.

import { refreshApex } from '@salesforce/apex';
3
  • Hi @Vitalii Dehnerys, thanks for the reply. I made changes as u said I am getting data of wired method that I hv used onload. but I need to repeat that above code to set the values on UI again. but I want without repeating the same code, is there any way to print same wired method (data on UI) or same functionality /values on UI in refreshTable() method. I was did it for Imperative calling, I used same functionality of SAVE AS DRAFT button in NEXT button. handleNext(event){ this.handleSaveAsDraft(); } this change repeated the same code/functionality of save as draft into next button. Sep 7, 2022 at 4:26
  • Hi @Vitalii Dehnerys, its worked for me. I have assigned values this.selectedmonth and this.selectedyear inside refreshApex i.e refreshApex(this.wiredAccounts).then(() => { this.selectedmonth=this._data.currentMon; this.selectedyear=this._data.currentYr; }); thanks.. Sep 8, 2022 at 7:02
  • @MANISHJADHAV Nice to hear, you are welcome. Sep 8, 2022 at 14:48

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.

Not the answer you're looking for? Browse other questions tagged or ask your own question.