0

I'm trying to find a way to refresh a custom lwc that's embedded into my record page but I can't find a way to properly achieve this.

I know you can handle this with aura using

<aura:handler event="force:refreshView" action="{!c.doInit}"/>

But I want to avoid using aura.

Can I really achieve this requirement? Maybe using event listeners?

Regards,

2
  • you can have an aura component without a body with handling event to refresh a page on lightning page and communicate with this aura component from LWC using LMS Jun 16, 2022 at 11:34
  • If you're using a wire service (e.g. getObjectInfo), your component is automatically refreshed.
    – sfdcfox
    Jun 16, 2022 at 17:40

3 Answers 3

1

You are able to use the RefreshView API now. Import RefreshView from lightning/refresh and dispatch that event.

import { RefreshEvent } from "lightning/refresh";

export default class YourComponent extends LightningElement {
  method() {
    // this will tell the view to refresh its contents
    this.dispatchEvent(new RefreshEvent());
  }
}
0

You can achieve this with Platform Events and Lightning Emp API .

Link to : Platform Events

You create Platform Event type for firing when a record is updated.
Create Record Id field which represents the record which was updated.
In your trigger you "publish" it :

List<Platform_Event__e> plEvents = new List<Platform_Event__e>();
plEvents.add( new Platform_Event__e (Record_Id__c = myRecord.Id) );
// Call method to publish events
List<Database.SaveResult> results = EventBus.publish(plEvents);

Link to : Lightning Emp API (for more information)

Then you subscribe to this Platform Event channel in your LWC with Lightning Emp API.

handleSubscribe() {
    // Callback invoked whenever a new event message is received
    const messageCallback = function (response) {
        console.log('New message received: ', JSON.stringify(response));
        // Response contains the payload of the new message received
    };

    // Invoke subscribe method of empApi. Pass reference to messageCallback
    subscribe(this.channelName, -1, messageCallback).then((response) => {
        // Response contains the subscription information on subscribe call
        console.log(
            'Subscription request sent to: ',
                JSON.stringify(response.channel)
        );
        this.subscription = response;
    });
}

In your messageCallback method you check if this Record Id field equals your recordId on the page.
If so you refresh your LWC ( create another method for it I suppose ?).

-1

You can achieve this by using event listeners. Inside your event listener method use this line it will refresh your Page.

eval("$A.get('e.force:refreshView').fire();");
1
  • 1
    This assumes aura, and OP is trying to avoid aura. I also think that the use of eval() here is unnecessary and generally bad practice (eval() is evil, as the saying goes).
    – Derek F
    Mar 1, 2023 at 15:54

You must log in to answer this question.

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