How to Navigate from LWC to Visualforce Page with Parameters?

How to Navigate from LWC to Visualforce Page with Parameters?

On July 17, 2025, Posted by , In LWC Essentials,Salesforce Technical Questions, With Comments Off on How to Navigate from LWC to Visualforce Page with Parameters?
How to Navigate from LWC to Visualforce Page with Parameters?

Question

How can I navigate from an LWC component to a Visualforce page with parameters, while ensuring compatibility with Salesforce Communities?

The LWC framework offers the NavigationMixin.Navigate functionality, which allows for obtaining URLs and performing navigation to generated URLs. This supports various page types, as detailed in the documentation. However, I am unable to find a clear method to generate a URL to a specific Visualforce page, with parameters, that is also compatible with Salesforce Communities. If you are wondering how to navigate from LWC to Visualforce page with parameters, using the “web page” navigation type seems incorrect because the URL for a Visualforce page differs depending on whether it’s accessed directly or through a community.

What is the proper approach to navigate from a flexipage via an LWC component to a Visualforce page within the same Salesforce org, while passing parameters to the page?

Answer

There isn’t a direct way to navigate to a Visualforce page from an LWC with parameters and ensure compatibility across different environments like Communities. Here’s a breakdown of the solution:

CRS Info Solutions offers top  Salesforce training in Hyderabad, including real-time projects, certification support, and interview coaching. Their practical approach ensures you’re fully job-ready..!

1.Failed Approach:

Initially, we attempted to navigate to a custom tab where the Visualforce page is embedded. This was done using the NavigationMixin.GenerateUrl, with query parameters included in the state object. The code looked something like this:

this[NavigationMixin.GenerateUrl]({
    type: 'standard__navItemPage',
    attributes: {
        apiName: "customTabName",
    },
    state: {
        c__showPanel: 'true' // Value must be a string
    }
}).then(url => {
    window.open(url);
});

This approach didn’t work as expected because the query string parameters weren’t passed to the Visualforce page due to iframe cross-domain restrictions.

2. Solution:

After some investigation, a workaround was found. Here’s how to approach navigating to a Visualforce page with parameters from an LWC, ensuring compatibility with both standard and community environments:

  • Create a cacheable, Aura-enabled Apex method that returns the Page.pagename.getUrl() value and checks whether the page is being accessed within a community using Site.getSiteId().
  • Use the wire service in LWC to invoke this Apex method and obtain the necessary URL.
  • If the page is within a community, use window.open() with the URL and appended query parameters. Communities don’t support navigating to a PageReference type as a standard web page.
  • If the page is not within a community, use NavigationMixin.GenerateUrl with the URL as a standard__webPage. Add the query parameters manually to the URL before passing it to NavigationMixin.

Here’s the LWC code that implements this solution:

handleClick(event) {
    event.stopPropagation();

    if (this._url) {
        // Add query parameters to the URL
        const urlWithParameters = this._url + '?' + this._queryParameters;

        if (this._isCommunity) {
            // Navigation to PageReference type standard__webPage not supported in communities.
            window.open(urlWithParameters);
        } else {
            this[NavigationMixin.GenerateUrl]({
                type: 'standard__webPage',
                attributes: {
                    url: urlWithParameters
                }
            }).then(generatedUrl => {
                window.open(generatedUrl);
            });
        }
    }
}

3. Update on Communities:

We encountered an issue where the Page.pagename.getUrl() did not include the community’s URL suffix, which led to broken links. This was resolved by combining a known static resource and using the following logic: This solution works for environments both inside and outside communities, whether or not a URL suffix is present. It’s a comprehensive answer on how to navigate from LWC to Visualforce page with parameters.

String theURL = PageReference.forResource('theknownresource').
        substringBefore('/resource/') + Page.pagename.getUrl();

This approach allows you to navigate from an LWC to a Visualforce page, passing the necessary query parameters and ensuring compatibility across Salesforce Communities.

Explanation of Code Snippets:

The first code snippet attempts to navigate to a custom tab using NavigationMixin.GenerateUrl, but the parameters weren’t passed correctly due to iframe restrictions. The second code snippet successfully handles the navigation by creating a URL with query parameters and using window.open() for communities or NavigationMixin for non-community environments. The third code snippet resolves an issue with missing community URL suffixes by appending the correct resource path to the Visualforce page URL, ensuring compatibility with communities. This solution clearly outlines how to navigate from LWC to Visualforce page with parameters.

Advance Your Career with Salesforce Training in Hyderabad

Our Salesforce training provides a dynamic, hands-on learning experience that equips you with the essential skills to thrive in the CRM industry. Covering crucial topics such as Salesforce Admin, Developer, and AI, the course blends theoretical knowledge with practical, real-world experience. By engaging in live projects and assignments, you’ll gain the expertise to address complex business problems using Salesforce solutions. Our experienced instructors offer technical guidance and valuable industry insights to help you succeed.

In addition to technical training, our  Salesforce training in Hyderabad includes personalized mentorship, exam preparation, and interview coaching to help you stand out in the competitive job market. You will have access to comprehensive study resources, practical exposure through live projects, and ongoing support throughout the program. Upon completion, you’ll be prepared for certification exams with the practical skills and problem-solving abilities that employers seek. Start your Salesforce career now and unlock exciting opportunities! Join our FREE demo class today!

Launch your Salesforce career now and explore amazing job prospects! Sign up for a FREE Demo today!

Comments are closed.