Codementor Events

Different navigations possible in LWC components

Published May 05, 2022Last updated Oct 31, 2022

We know that we want to move from one page to other page in the web, and why not we could do that within the Salesforce Lightning web components a.k.a LWCs.
Below are the actions that we can perform to navigate from LWC components.

  1. Navigate to a web page like “https://sunilkhuwal.wordpress.com“, I like to give this url Meh. 😊😊
  2. Navigate to Object Home page like Account home page
  3. Navigate to Record Detail page like Account record Detail page(In your case it could any other object’s record page.
  4. Navigate to the Object List page, like Account List View page
  5. Navigate to file Preview
  6. While I am writing Salesforce might have introduced new mode of navigation in LWC.

We will discuss the code pattern individually for each of these styles. But before that just one thing that you need to do in the js file part. Keep note of the “LightningElement” should be encapsulated inside of the NavigationMixin like NavigationMixin(LightningElement) while exporting the default class

import { LightningElement } from "lwc";
import { NavigationMixin } from "lightning/navigation";
export default class NavigationServiceExample extends NavigationMixin(
  LightningElement
) {
   //Navigations method
}

I am keeping a one time html file to show how that would be called from. For rest of the examples only javascript should be enough, if you still have any issues you can comment here and I’ll respond to that.

<template>
  <lightning-card title="Navigation Examples">
    <div class="slds-p-around_medium">
      <lightning-button
        label="Open Sunil Web"
        onclick={openWebHandler}
      ></lightning-button>
    </div>
</lightning-card>
</template>
openWebHandler() {
    this[NavigationMixin.Navigate]({
      type: "standard__webPage",
      attributes: {
        url: "https://sunilkhuwal.wordpress.com"
      }
    });
  }
openAccountHandler() {
    this[NavigationMixin.Navigate]({
      type: "standard__objectPage",
      attributes: {
        objectApiName: "Account",
        actionName: "home"
      }
    });
  }
openAccountRecordHandler() {
    this[NavigationMixin.Navigate]({
      type: "standard__recordPage",
      attributes: {
        objectApiName: "Account",
        actionName: "view",
        recordId: "$recordId"
      }
    });
  }
openAccountListHandler() {
    this[NavigationMixin.Navigate]({
      type: "standard__objectPage",
      attributes: {
        objectApiName: "Account",
        actionName: "list"
      }
    });
  }
openFilePreviewHandler() {
    this[NavigationMixin.Navigate]({
      type: "standard__namedPage",
      attributes: {
        pageName: "filePreview"
      },
      state: {
        //The file record id has to be passed in here
        recordIds: "$fileId1,$fileId2",
        selectedRecordId: "$fileId1"
      }
    });
  }

Config file navigationExamples.js-meta.xml

<?xml version="1.0" encoding="UTF-8" ?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>51.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__RecordPage</target>
        <target>lightning__AppPage</target>
        <target>lightning__HomePage</target>
    </targets>
</LightningComponentBundle>

If you know any other type of navigations pls fill in the comments, so that we can update this post to help more people.

Peace 🤘

Discover and read more posts from Sunil Kumar
get started
post commentsBe the first to share your opinion
ANANT ALANE
9 months ago

Hi Sunil,
Can you help me to redirect from one app record page to another app record page

Sunil Kumar
9 months ago

Do you want to say the Record detail page belonging to another type of object? Could you please let me know you your requirement more in detail?

Codefire
a year ago

Hi Sunil,
I am struggling to close a console tab which is being implemented on lwc. Can you please help me out as most of the answers suggest to use workspace pay by wrapping in a aura component but since it is totally implemented in lwc, its not possible. Please suggest something. Thank you in advance

Frans Fourie
2 years ago

Good day. I am struggling with a navigation. I am using standard__recordPage and found that it will navigate to the lightning page that’s set as default. Is there a way to navigate to a specific page? But it must be of record type and accept a recordId

Sunil Kumar
2 years ago

Hi Frans, you use the blow code snipped to reach to the specific record detail page.

openAccountRecordHandler() {
this[NavigationMixin.Navigate]({
type: “standard__recordPage”,
attributes: {
objectApiName: “Account”,
actionName: “view”,
recordId: “$recordId”
}
});
}

If you could explain a bit more about your query then I can provide you with the appropriate solution.

Like going to the record detail page or record home page. like that

Frans Fourie
2 years ago

Hi thank you for the reply. If you navigate like your example, it takes you to the DEFAULT lightning page for the object. I have more than one lightning page for the object, and would like to specify in the navigationMixIn which page I want to go to.

Example.

openAccountRecordHandler() {
this[NavigationMixin.Navigate]({
type: “standard__recordPage”,
attributes: {
objectApiName: “Account”,
actionName: “view”,
recordId: “$recordId”
pageName: “page3” <<<<<<------------
}
});
}

But you can’t specify page name like that, so would like to know if there is another option.

Paul Ketchup with Everything
a year ago

I’m facing the same difficulty. The default Lightning Record Page I’m working with has a header and right side bar. I have a different LRP for the same record without the header and side bar that I want to use for Ctrl+P printing.

I am struggling to find a way to navigate to the non-default layout record page. I have tried creating a SF app and associating the alternate page layout with the app, then accessing the app by URL, without success.

Show more replies