The LWC framework includes the NavigationMixin.Navigate functionality that can be used to obtain URLs or perform navigation to generated URLs. This supports a number of different page types, as covered in the documentation.
What I can't find is how to create a URL to a specific Visualforce page, with parameters and compatible with communities. Using the "web page" navigation type seems wrong to me, since the URL for a Visualforce page varies based on whether access is direct or within a community.
Do you know what the correct approach is for navigating from a flexipage via an LWC component to a Visualforce page in the same org, passing the page some parameters?
Best Answer
OK, so we didn't find a direct way to do it.
Failed approach
Initially we thought that the following approach should work for normal and community usage. In essence we implemented a component with navigation mixin then used something like:
Importantly, this wasn't actually trying navigate directly to a Visualforce page, but rather to a custom tab into which the page is placed (clearly the tab needs to be accessible by the user). Note the use of window.open allows the URL to be opened in a separate tab or window within the browser.
Unfortunately, the "query string parameters" included do NOT get through to the Visualforce page - this because of the use of iframes across domains.
The Solution
We eventually found a way to do this, i.e. invoke a Visualforce page with query parameters from an LWC, though it was a lot more complex:
The LWC code looks something like:
** Update:
We actually found that when using with communities that include a URL suffix that the Page.pagename.getUrl() did not add the community URL suffix and therefore didn't work. This was remedied by having a known static resource and using the following:
This then works outside communities, in a community with no URL suffix, in a community with a URL suffix and in a community with multiple URLs.