2

I have a custom lwc that works with refresh apex. I want a method to be refreshed from another function

js:

import {refreshApex} from '@salesforce/apex';
import initData from '@salesforce/apex/myClass.initData';

export default class Tm_GDPRInfoSignatureLWC LightningElement {

    wiredGDPRresult;
    @track myData;

    @wire(initData , {recordId : '$recordID'}) wiredInitData(result) {
        this.wiredGDPRresult = result;
        //data retrieved
        if(result.data) {
            console.log('entered');
            this.myData = result.data;
            console.log('finished');
        }
        //error
        else if(result.error){
            console.log(JSON.stringify(result.error));
        }
    }

    refreshData(){
        return refreshApex(this.wiredGDPRresult);
    }
}

And it's working find when initializing the lwc but whenever I call refreshData it's doing nothing (not even displaying console.logs())

Is there something I'm setting wrong or do I need to call refreshApex in a different way?

EDIT

I'm calling my function whenever I receive a custom event from another lwc. And it's receiving that event but refreshApex doesn't do anything

connectedCallback(){
    registerListener('refreshData', this.refreshData, this);
}
2
  • Can you show how you call refreshData and a snippet of the template too? (I see you made a change for the import usages)
    – Phil W
    Oct 21, 2019 at 11:10
  • @PhilW I just updated the question and add where my refreshData methods i being called from. Thanks
    – molinet
    Oct 21, 2019 at 11:13

1 Answer 1

7

It will actually work but as its cacheable (or cached to be precise) apex method, the wired function imperativeWire will be invoked ONLY if the apex method response changes/modified. However every time you invoke refreshData, you will see the apex log - can be tested in developer console.

For testing, you can return some accounts:

@AuraEnabled(cacheable=true)
public static List<Account> getAccountsList() {
    return [SELECT Id, Name, Phone FROM Account LIMIT 10];
},

Then use wired function to get these accounts in LWC component.

Then when you change some field value in one of these accounts and invoke refreshData you will see the console log in LWC also.

2
  • You're right. It's only entering if my response has changed. Thanks, I didn't notice that at first try!
    – molinet
    Oct 21, 2019 at 11:26
  • Nice....I too needed this
    – Mohit K
    Oct 21, 2019 at 15:27

You must log in to answer this question.

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