1

I am trying to use an example piece of code by Salesforce, but it's not working for me.

It starts with a VF page, which unfortunately is necessary, because it is not possible to add an action to a list view in Lightning. So I must use a VF page, called by a custom button. This is the page:

<apex:page>
  <apex:includeLightning />
  <div id="lightning" />
  <script>
    $Lightning.use("c:SelectCountryForAccountWrapperApp", function() {
      $Lightning.createComponent("c:SelectCountryForAccountWrapper",
        { },
        "lightning",
        function(cmp) {
          console.log("callback was called");
          // do some stuff
        }
      );
    });
  </script>
</apex:page>

Via a small Aura app, this invokes a LWC which consists of HTML:

<template>
    <lightning-card title="Select country">
        <template for:each={countries} for:item="country">
            <lightning-button key={country}
                                variant="base"
                                label={country}
                                title={country}
                                onclick={handleSelect}>
            </lightning-button>
        </template>
    </lightning-card>
</template>

and JS of course:

import { LightningElement, track } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';
import availableCountries from '@salesforce/apex/SelectCountryForLWCController.availableCountries';

export default class SelectCountry extends NavigationMixin(LightningElement) {

    @track countries = [];

    connectedCallback() {
        availableCountries()
        .then(result => {
            this.countries = result;
        })
        .catch(error => {
            // TODO
        });
    }

    handleSelect(event) {
        this[NavigationMixin.Navigate]({
            type: 'standard__objectPage',
            attributes: {
                objectApiName: 'Account',
                actionName: 'new'
            }
        });
    }
}

So far it's working, clicking the button displays a list of country names, fetched by Apex code. Each country name is clickable.

But when I click on a country name, the console displays:

Error: [LWC error]: Invalid event type "WireContextEvent" dispatched in element . Event name must 1) Start with a lowercase letter 2) Contain only lowercase letters, numbers, and underscores at logError (aura_proddebug.js:4760) at SelectCountry.dispatchEvent (aura_proddebug.js:8662) at SelectCountry.wrappedMethod [as dispatchEvent] (aura_proddebug.js:14573) at DomContextBinding.get (contextService.js:106) at findContextProvisions (contextService.js:296) at getContextProvisions (contextService.js:337) at Object.getContextValues (contextService.js:368) at getDelegates (navigationService.js:7) at NavigationServiceClient.navigateTo (navigationService.js:130) at SelectCountry.[Navigate] (navigation.js:18)

and nothing happens.

Googling this message tells me that I should just ignore it, it's some Salesforce bug. But it seems I can't ignore it, because nothing happens, no navigation. I could use some assistance.

2

This will not work as you are using lightning out, I would suggest you to use window.open('/'+ObjectId); JS method to do the navigation.

If you are trying to navigate to account record page then please pass your account id to window.open method along with the forward slash in single quotes.

NavigationMixin.Navigate will work only if you are using your LWC from lighting app builder.

2
  • Thanks. I guess I will have to use NavigationMixin.GenerateUrl to generate the URL and pass it on to window.open. developer.salesforce.com/docs/component-library/bundle/… suggests this should not be difficult, but so far I'm only getting javascript:void(0); as URL. I guess that will be another question. – Sander de Jong Apr 21 '20 at 8:18
  • 1
    if you are going with window.open then you may completely avoid NavigationMixin, I have updated my answer as well. – gs650x Apr 21 '20 at 8:25

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.