2

I'm struggling to find the best way to accomplish this - any suggested are appreciated! I have an LWC for a community page that will have a button to launch an external URL. The URL needs to be generated when they push the button from the apex class due to a time sensitive URL generation. I've tried a few different ways and have been unable to get it to launch.

import { LightningElement } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import getURL from '@salesforce/apex/lwcController.getURL';

export default class NavToHome extends NavigationMixin(LightningElement) {

   navigateToWebPage() {
        // Navigate to a URL

        this[NavigationMixin.Navigate]({
            type: 'standard__webPage',
            attributes: {
                url: getURL
            }
        },
        true // Replaces the current page in your browser history with the URL
      );
    }
}

HTML:

<template>
    <lightning-card title="NavToHome" icon-name="custom:custom96">
        <lightning-button
            label="Go to Home"
            class="slds-var-m-around_medium"
            onclick={navigateToWebPage}
        ></lightning-button>
    </lightning-card>
</template>

I've kept it pretty basic for demo purposes but this isn't launching the URL that is returned from getURL method of the apex class. Thoughts or better ways to do this? Thanks!

1 Answer 1

2

You're close, just get the value then use it:

async navigateToWebPage() {
    // Navigate to a URL
    let url = await getURL();
    this[NavigationMixin.Navigate]({
        type: 'standard__webPage',
        attributes: { url }
    },
    true // Replaces the current page in your browser history with the URL
  );
}

The await keyword waits for the URL to be returned, and the async keyword is necessary in any function that uses await.

7
  • Thank you @sfdcfox !! That does get me one step closer. What happens now, however, which is new behavior is in my community it tries to append the returned URL at the end of the community URL and does not launch in a separate window. Before it was launching correctly in new tab (this is what I need). Ideas on how to fix that? I'm doing some digging now...
    – EMC
    Commented May 2, 2021 at 17:36
  • @EMC Not sure. I don't do much work in communities.
    – sfdcfox
    Commented May 2, 2021 at 18:54
  • I get the same behavior internally on a record page, i.e. https://.../lightning/r/Account/0014x000006WJSTAA4/http%3A%2F%2Fwww.google.com
    – EMC
    Commented May 2, 2021 at 19:07
  • @EMC I'll try to replicate the issue this evening, and get back with you.
    – sfdcfox
    Commented May 2, 2021 at 21:12
  • @EMC Can't seem to replicate it; it works fine in my org as a App Page.
    – sfdcfox
    Commented May 2, 2021 at 22:45

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .