function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
mhittmhitt 

Custom lightning component button not navigating to URL

Hi, 

I have a custom button in a login flow that needs to navigate to an external website. Below is the code I have for the component and controller, but I think the button needs to redirect to a new tab and open the URL there (the button doesn't navigate the user anywhere). 

Component
<aura:component implements="lightning:availableForFlowScreens" access="global">

    <lightning:button variant="brand" label="Find My Closer" title="Find My Closer" onclick="{! c.navigate }" />
    
</aura:component>

Controller:
({
    navigate  : function(component, event, helper) {
        
        var urlEvent = $A.get("e.force:navigateToURL");
        urlEvent.setParams({
            "url": 'https://www.google.com'
        });
        urlEvent.fire();
    }
})
Any help is appreciated.
Thanks in advance!​
Raj VakatiRaj Vakati
You cannt able to use force:navigateToURL in the salesforce lightnign app by using .app  . It will work only on the Salesforce mobile  and lightning experience  app 


You code is correct but i guess you are testingn from the .app .. Move it into the app using app builder and test it it will work ... 



And try to use navigation API rather than the force:navigateToURL 
mhittmhitt
Hi Raj,

Thank you for the reply. This component is being displayed and used as a login flow (for Community) when the User's cell number is blank in their profile. This button is being tested using that scenario with the intent to redirect the User to another website, however when I click this button it doesn't go anywhere. This should also be a globally accessable page. I'm not exactly sure what changes need to be made to match your suggestion, and I apologize for the novice question but I have little experience with Lightning components 8-). Could you please provide an example?

Thank you!
Akshay_DhimanAkshay_Dhiman
Hi Matt Hitt
 
// Component//

<aura:component implements="force:appHostable">
    <div id="aura-page">
        <div class="container">
            <ui:button label="gotoURL" press="{!c.gotoURL}" />
        </div>
        <div class="container">
            
            <lightning:button variant="brand" label="Find My Closer" title="Find My Closer" onclick="{! c.navigate }" />

        </div>
    </div>
</aura:component>

//Controller//

({
    gotoURL : function(component, event, helper) {
        helper.gotoURL(component);
    },
    navigate : function(component, event, helper) {
        helper.navigate(component);
    }
})

// Helper Controller//
({
    gotoURL : function (component) {
        var urlEvent = $A.get("e.force:navigateToURL");
        urlEvent.setParams({
          "url": "/006/o"
        });
        urlEvent.fire();
    },
    navigate : function(component) {
        var address = '/Salesforce.com+Inc/@37.793779,-122.39448,17z/';
        var urlEvent = $A.get("e.force:navigateToURL");
        urlEvent.setParams({
          "url": 'https://www.google.com/maps/place/' + address
        });
        urlEvent.fire();
    }
})

if you found this answer helpful then please mark it as best answer so it can help others.   
  
  Thanks 
  Akshay