6

I'm using getrecord via @wire to pull account data:

@wire(getRecord, { recordId: '$recordId',
    fields: [
        ACCOUNT_MARKET_FIELD,
        ACCOUNT_FAX_FIELD,
        ACCOUNT_TIMEZONE_FIELD,
        ACCOUNT_COUNT_OF_ACTIVE_CHANNELS_FIELD,
        ACCOUNT_TOUCH_VERSION_FIELD
    ],
    modes: ['View'] }) getRecordResult({ error, data }) {
    if (error) {
        this.error = error;
    } else if (data) {
        // new account data should clear old errors
        this._errors = [];
        this.account = data;
        this._showErrorList = true;
        // finally show the component
        this._isReady = true;
        // temp to see what's going on...
        this._touchVersion = data.fields.Touch_Version__c.value;
    }
}

From what I understand, @wire is a stream of data and when I access it, I should be seeing the latest version of that data and not old, stale data. I assume that even if the data is not changed by the same page/tab I'm currently on, I should see the data update on my page. However, I am not seeing this.

In my example, Touch Version is just a standard number field being updated by a Workflow Rule. Whenever something changes the Account, this field is incremented by 1. While I see the touch version incrementing if I edit/save the record on the main tab, if an Apex trigger causes this to increment or I edit the same record on a different tab, the main tab does not increment.

Is there a way to be notified (receive the latest data) when the record data on my main tab is changed by another different tab or process?

7

The wire adaptor does not have Streaming built into it. The wire adaptor uses LDS cache and this blog in a good read on how all this works under the hood.

Essentially you will need a lightning Data Service along with wire adaptor if you want to listen for change from the different components on the same page.

If the action is asynchronous or on a different page, you will have to refresh the page to get the latest dataset or provide a button or an action so you can refresh and get the data.

If you need it to be automatic without manual interaction then this can be achieved using Change Data Capture or platform and using the empi lightning component.

5
  • 1
    CDC + refreshApex should work for most use cases. – sfdcfox May 18 '20 at 19:38
  • Thanks for the answer and comment. It would seem that my best option is to wrap my LWC with an Aura component implementing the empi component listening for a PushTopic. Unless I understand the Developer Guide, CDC appears to be geared more toward external system sync. – Swisher Sweet May 18 '20 at 21:19
  • 1
    @SwisherSweet you can implement the empApi in Lwc itself, refer – Rahul Gawale May 19 '20 at 6:08
  • @rahulgawale wonderful! I didn't realize they added this. Much appreciated. – Swisher Sweet May 19 '20 at 14:28
  • @SwisherSweet@rahulgawle Thank you !! I fixed the link. – Mohith Shrivastava May 19 '20 at 16:38
-1

It should be able to get the latest data of the record. But @wire is only called once before the page begin to render. You can use this code to forcefully refresh the view from lwc component.

eval("$A.get('e.force:refreshView').fire();");
5
  • 3
    Hmm, no, you should not attempt to use $A methods in LWC. Nor should you use eval. – sfdcfox May 18 '20 at 19:21
  • 1
    @sfdcfox $A is disallowed but you can still use eval as a workaround. And it should work fine – arufian May 18 '20 at 19:22
  • 2
    Circumventing security restrictions via eval is a Bad Idea. It may work today, but may not work tomorrow. If you're using that "trick", you should stop doing so. – sfdcfox May 18 '20 at 19:25
  • 2
    In fact, I'm going to go report this security vulnerability so that it can be fixed at some point. – sfdcfox May 18 '20 at 19:25
  • If this is not allowed, then there won't be any way to refresh a component that uses PageReference parameters set via urlAddressable interface. Currently, if we redirect to a component from LWC we cannot reload the component due to page reference state not refreshing unless it is refreshed via parent container Aura component. The only workable solution I found is this - Stack Exchange Question Link – VarunC May 19 '20 at 18:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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