1

I am using NavigationMixin to navigate from one lwc to another lwc (app page in a custom tab) like this:

   this[NavigationMixin.Navigate]({
        type: 'standard__navItemPage',
        attributes: {
            apiName: 'MyCustomTab'
        }
    });

My problem is when I deploy this in my partner org, which has a namespace, I get an error because MyCustomTab doesn't exist. The proper name is MyNamespace__MyCustomTab. How can I solve this issue? I want my code working on both scenarios, with and without namespace.

Thank you.

4
  • 1
    Once packaged you need the namespace anyway. What scenario is causing a failure if you add the namespace?
    – Phil W
    Commented Jul 13, 2023 at 9:41
  • I develop in a developer sandbox which doesn't have the namespace. then, after testing, we deploy in our partner org and in this one, we have the namespace. Commented Jul 14, 2023 at 11:18
  • 1
    You might consider using scratch orgs instead. They will have the namespace.
    – fred
    Commented Jul 14, 2023 at 11:22
  • Yes, that should be the best solution. But it's a development that I have inherited from other developer who is not in the company anymore. Anyway, thank you all! Commented Jul 14, 2023 at 13:58

1 Answer 1

1

You need to create one method into the apex which returns the org prefix. In that method, you just need to query the org prefix and return it to the LWC. Query: return [SELECT NamespacePrefix FROM Organization].NamespacePrefix;

In LWC Js file you need to import this method like this:

import getPrifix from '@salesforce/apex/SomeApex.getOrgPrefix';

then define the one variable to hold the prefix.

@track orgPrefix;

After that write the wire method to get the prefix from the org.

@wire(getPrifix)
    prefix({data, error}){
        if (data) {
            this.orgPrefix = data;
        }
    }

Then you can use this prefix in the custom tab in which you need to navigate.

@track tabName = 'MyCustomTab';
navigateNext() {
    let tabNameWithPrifix = "";
    if(this.orgPrifix != undefined || this.orgPrifix != null){
        tabNameWithPrifix = this.orgPrifix+"__"+this.tabName;
    }

    this[NavigationMixin.Navigate]({
        type: 'standard__navItemPage',
        attributes: {
            apiName: tabNameWithPrifix,
        }
    });
}
1
  • 2
    Careful; since you overwrite this.tabName every time navigateNext is called you could end up with multiple prefixes applied (you may get away with it if the LWC's state is always lost because of the navigation, but this code is brittle). Personally, I'd deal with this in a function called from setters on the stored prefix and tab name that sets an internal value used only in the navigateNext method.
    – Phil W
    Commented Jul 14, 2023 at 15:21

You must log in to answer this question.

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