[SalesForce] Navigation and PageReference in Lightning Component – Why is url void

UPDATE: I just verified it works when I navigate to my Lightning Component as part of a Lightning Page.

My company is still in classic, so we're wrapping our components in Visualforce. https://developer.salesforce.com/docs/atlas.en-us.lightning.meta/lightning/components_visualforce.htm

Now, the question is how do I make PageReference/Navigation Link work in a Component, within a Visualforce page?

I've also now tried force:navigateToURL. Works fine when the component is in a Lightning Page, errors in a VF page.
https://developer.salesforce.com/docs/component-library/bundle/force:navigateToURL/documentation

PRIOR:
DefaultUrl seems to work as expected, but url is returning void. Why is that? I've tried other PageReference types, and url is always void.

enter image description here

<aura:component access="public" implements="force:appHostable,lightning:isUrlAddressable">
<aura:attribute name="url" type="String" />
<aura:attribute name="pageReference" type="Object"/>

<aura:handler name="init" value="{! this }" action="{! c.init }"/>


<lightning:navigation aura:id="navService"/>
<a href="{!v.url}">Link</a>

({
init : function(component, event, helper) {
    var navService = component.find("navService");
    var pageReference = {
        type: 'standard__navItemPage',
        attributes: {
            apiName: "MyVisualForcePage"   
        }
    };
    component.set("v.pageReference", pageReference);
    // Set the URL on the link or use the default if there's an error
    var defaultUrl = "apex/MyVisualForcePage";
    navService.generateUrl(pageReference)
        .then($A.getCallback(function(url) {

            console.log('### url' + url);
            console.log('### defaultUrl' + defaultUrl);

            component.set("v.url", url ? url : defaultUrl);
        }), $A.getCallback(function(error) {
            component.set("v.url", defaultUrl);
        }));
}

})

Best Answer

Here what I see in the documentation To enable direct navigation to a Lightning component via URL, add the lightning:isUrlAddressable interface to the component. This interface is used with the lightning:navigation component to navigate from one component to the URL-addressable component. This navigation feature is supported only in Lightning Experience and the Salesforce App.

For force:navigateToURL I see below.

This event is handled by the one.app container. It’s supported in Lightning Experience, Salesforce app, and Lightning communities.

You need to check if user is in classic or lightning and then fire different mehod of navigation.

For classic you can use window.open it also works in lightning.

In short, you can not access those in classic.

Related Topic