How to Refresh View in LWC?

How to Refresh View in LWC?

On April 28, 2025, Posted by , In LWC Essentials, With Comments Off on How to Refresh View in LWC?
How to refresh a view in LWC

Question:

In Aura, we can use $A.get('e.force:refreshView').fire(); to trigger a view refresh for standard components. However, Salesforce DX refuses to deploy JavaScript code with $A in it because its usage is disallowed in Lightning Web Components (LWC). How can we achieve the same result of refreshing the view in LWC without using $A?

Answer:

While LWC doesn’t support $A.get('e.force:refreshView').fire(), you can use the updateRecord method from lightning/uiRecordApi to refresh a record page. This method allows you to refresh the record page, such as an Account Detail Page, by triggering an update on the record without any direct DOM manipulation.

Boost your career with expert Salesforce training in Nagpur—join our free demo and start your journey to certification today!

Here’s an example of how to achieve this:

1.Import the updateRecord method:

Import the method from lightning/uiRecordApi to enable you to perform updates on the record.

import { updateRecord } from 'lightning/uiRecordApi';

Code explanation:
The line import { updateRecord } from 'lightning/uiRecordApi'; imports the updateRecord function from the lightning/uiRecordApi module, which allows you to perform record updates in a declarative manner. This function is specifically used for updating Salesforce records in LWC without the need for custom Apex code, helping to keep the component’s code efficient and aligned with Salesforce standards.

2.Use the updateRecord method:

When you want to trigger a refresh of the page, you can call updateRecord with the record ID, which forces the page to reload the details.

updateRecord({ fields: { Id: this.recordId } });

Code explanation:
The code updateRecord({ fields: { Id: this.recordId } }) is used to refresh a record page in LWC by updating the specified record. It triggers the refresh process for the record with the provided recordId, ensuring the UI reflects any recent changes or updates made to the record.

3.Full Example Code:

The following example shows how to trigger an update of the record and then refresh the Account detail page.

import { LightningElement, api } from 'lwc';
import { updateRecord } from 'lightning/uiRecordApi';
import updateAccount from '@salesforce/apex/ApexControllerClass.updateAccount';

export default class LightningWebComponentExample extends LightningElement {
    @api recordId;

    handleClick() {
        // Call Apex Method imperatively to update Account record
        updateAccount({ accountId: this.recordId })
            .then(result => {
                if (result) {
                    // Refresh Account Detail Page
                    updateRecord({ fields: { Id: this.recordId } });
                }
            });
    }
}

Code explanation:
The code defines a Lightning Web Component (LWC) that triggers a page refresh after updating an Account record. It imports the updateRecord method from lightning/uiRecordApi to refresh the record page. The updateAccount Apex method is called imperatively using the handleClick method, passing the recordId to update the Account. Once the update is successful, the updateRecord method is called to refresh the Account detail page.

This approach is preferred because it uses native LWC functionality without relying on Aura framework components.If you are not dealing with record detail pages but want to refresh a custom component or page, consider using an event-driven architecture or implementing reactive properties in your component to reflect changes dynamically.

Alternative Method: Using eval() (Not Ideal)

Though not recommended due to its reliance on eval(), a workaround to call $A.get('e.force:refreshView').fire(); can be achieved by using:

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

Code explanation:
The eval("$A.get('e.force:refreshView').fire();") code executes the Aura method force:refreshView dynamically by passing it as a string to the eval() function. This method triggers a refresh of the current view in Salesforce, but using eval() in LWC is discouraged due to security and performance concerns.

However, this approach is less than ideal as it bypasses LWC’s restrictions and should be avoided for long-term solutions.

Salesforce Training in Nagpur – Transform Your Career Path

Take a leap forward in your professional journey with our industry-recognized Salesforce online training program in Nagpur. Designed for both aspiring professionals and experienced individuals, this program offers comprehensive knowledge of Salesforce CRM, hands-on exposure to real-world projects, and step-by-step guidance to ace certifications like Salesforce Administrator and Developer. With a job-focused curriculum, you’ll gain the skills and confidence needed to excel in the Salesforce ecosystem and achieve new career milestones.

We prioritize practical, industry-relevant learning to ensure you’re fully prepared for the competitive job market. Our Salesforce training in Nagpur combines personalized mentorship, in-depth course materials, and real-time support for certification and interview preparation. Whether you’re starting fresh or upskilling, our expert trainers will help you unlock your potential and stand out in the Salesforce domain.

Join us for a free demo session today and begin your journey toward a thriving Salesforce career!!!


Comments are closed.