10

Whats the lwc equivalent of this aura event?

$A.get('e.force:refreshView').fire();

3 Answers 3

9

I had this issue myself today as well. In my case, I had a Quick Action Lightning Aura Component which enveloped a Lightning Web Component (because there is currently no way of using a Lightning Web Component as a Quick Action)..

What I did is set up my Aura Component to catch 'close' events:

<!-- aura/auraComponent/auraComponent.cmp -->
<aura:component implements="force:lightningQuickAction,force:hasRecordId">
    <c:webComponent recordId="{!v.recordId}" onclose="{!c.handleClose}" />
</aura:component>

And process them with the events I would LIKE to throw from my Web Component:

// aura/auraComponent/auraComponentController.js
({
    handleClose: function() {
        $A.get('e.force:refreshView').fire();
        $A.get("e.force:closeQuickAction").fire();
    }
})

And in my Web Component I throw the following Custom Event to trigger the refresh & quick action close:

this.dispatchEvent(new CustomEvent('close'));

I know it's ugly, but at the time of writing I don't think there's a better way to refresh an underlying record page & close a Quick Action modal from a Lightning Web Component.

2
  • I have copied your code. closeQuickAction works, but not refreshView. I have tried single quotes and double quotes (you mixed them), but that does not make any difference. Does the refresh update the related lists in your case? Commented Mar 4, 2020 at 8:07
  • 1
    No, actually - it refreshes my record detail page but not the related lists. It might be to do with this known issue: success.salesforce.com/issues_view?id=a1p3A000000mDOuQAM Commented Mar 6, 2020 at 8:16
4

Starting in Winter '21, you'll be able to use getRecordNotifyChange() function to refresh your record page from a lightning web component.

getRecordNotifyChange is working almost the same way force:refreshView did, except it takes a list of recordId which makes it a little bit more flexible:

import { LightningElement, wire } from 'lwc';
import { getRecord, getRecordNotifyChange } from 'lightning/uiRecordApi';
import apexUpdateRecord from '@salesforce/apex/Controller.apexUpdateRecord';
     
export default class NotifyRecordChangeExample extends LightningElement {
  @api recordId;
     
  // Wire a record.
  @wire(getRecord, { recordId: '$recordId', fields: ... })
  record;
     
  async handler() {
    // Update the record via Apex.
    await apexUpdateRecord(this.recordId);
    // Notify LDS that you've changed the record outside its mechanisms.
    getRecordNotifyChange([{recordId: this.recordId}]);
  }
}
3
  • getRecordNotifyChange only works for LDS data right? and I tried this it was not working for refreshing stale LDS cache data. Commented Apr 5, 2021 at 20:34
  • It's for LDS right, if you pass a record Id it should refresh this record's cache. Commented Apr 6, 2021 at 9:40
  • Thanks @FabienTaillon, working like a charm, could you please explain why do we need to wire a method additionally, without below code it's not working. // Wire a record. @wire(getRecord, { recordId: '$recordId', fields: ... }) record; Commented Jun 29, 2021 at 5:30
-1

call the refreshApex () method if you want to get result from the SF

import apexMethod from '@salesforce/apex/Namespace.Classname.apexMethod';
@wire(apexMethod, { apexMethodParams })
propertyOrFunction;

    // opportunitiesOverAmount.js
import { LightningElement, api, wire } from 'lwc';
import { refreshApex } from '@salesforce/apex';
import getOpptyOverAmount from '@salesforce/apex/OpptiesOverAmountApex.getOpptyOverAmount';
import updateOpptyStage from '@salesforce/apex/OpptiesOverAmountApex.updateOpptyStage';

export default class OpportunitiesOverAmount extends LightningElement {
    @api amount;

    @wire(getOpptyOverAmount, { amount: '$amount' })
    opptiesOverAmount;

    handleClick(e) {
        updateOpptyStage({
            stage: 'Won'
        })
        .then(() => {
            return refreshApex(this.opptiesOverAmount);
        })
        .catch((error) => {
            this.message = 'Error received: code' + error.errorCode + ', ' +
                'message ' + error.body.message;
        });
    }
}
2
  • This helps updating the value of @wire property. But $A.get('e.force:refreshView').fire(); is used to tell OTHER components that things need to refresh.
    – zaitsman
    Commented Feb 22, 2019 at 1:15
  • 1
    Yes that is correct @zaitsman, I need to tell other components that things need to be refreshed.
    – user64922
    Commented Feb 25, 2019 at 6:05

You must log in to answer this question.

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