Workaround: Refresh Custom LWC on Change of Data in Standard Components or Backend Updates in Salesforce
This content presents a practical workaround to refresh a custom Lightning Web Component (LWC) when data changes in standard Salesforce components or backend updates, a scenario not supported out-of-the-box. It explains how to leverage Platform Events to notify the custom LWC of data changes so it can trigger refreshes using refreshApex. The article includes sample Apex triggers and helper classes to publish these events, along with a Lightning Web Component subscribing to these events to refresh its data accordingly. Salesforce professionals can implement this pattern to ensure custom LWCs stay up-to-date with related standard object changes without manual intervention.
- Use Platform Events to notify custom LWCs of backend or standard data changes.
- Publish Platform Events from Apex triggers on relevant record updates.
- Subscribe to Platform Events in LWCs using lightning/empApi to trigger refreshApex.
- Leverage refreshApex() to refresh wired Apex data in LWCs upon receiving events.
- Limit platform event publishing to specific field changes for efficiency.
If you are looking for a solution to refresh your custom Lightning Web Component on change of data in one of the Standard Component or from Backend. This blog is for you! To refresh the APEX in LWC we have refreshApex(), to refresh the standard components on change of custom LWC we have refreshViewAPIs To refresh a Custom LWC whenever data is changing on Standard component or in backend unfortunately there are no OOTB way to achieve it. But we are here to create a workaround of it. As a workaround we will use Platform Events to notify custom LWC that something has changed in data and it needs to refresh. Please check steps below: Step 1: Create a Platform Event as shown below. Step 2: You need to publish the platform event using an automation flow or trigger. In my scenario I have used triggers. I have created OpportunityTrigger and published it on AfterUpdate event.