force:refreshView (getRecordNotifyChange) in Lightning web component

362 Views

We’re kind of in love with the lightning UI and the aura feature (e.force:refreshview) which updated the data on the current lightning page/record/component. We missed the feature in LWC and I saw many using the workaround to use a combination of AURA and LWC to just have this functionality.

Starting Winter ’21, there is a new function in town !

Using it is as simple as what we’ve been used to in Aura. You’ll just need to import it
(import { getRecordNotifyChange } from 'lightning/uiRecordApi';), and then call it with a list of record Ids
(getRecordNotifyChange([{recordId: this.recordId}]);).

Full sample code looks like this:

This implies one change though: force:refreshView was refreshing the whole page, whereas getRecordNotifyChange() is only refreshing the records you ask him to refresh. So if your Apex code is for instance updated the current Account, but also some child Contacts, you may have to pass to getRecordNotifyChange() all the Ids updated by your Apex method.

Credits: https://blog.texei.com/refresh-a-record-page-from-a-lightning-web-component-14a5874ff68e

Leave a Comment