Hey guys, today in this post we are going to learn about How to apply navigationmixin.navigate for navigate to different page like create new account record, web external url, visualforce page, contact list view, new tab and reports page uses of Navigation Service Library (NavigationMixin) in Lightning Web Component (LWC).
The lightning/navigation service is supported only in Lightning Experience, Experience Builder sites, and the Salesforce mobile app. It isn’t supported in other containers, such as Lightning Components for Visualforce, or Lightning Out. This is true even if you access these containers inside Lightning Experience or the Salesforce mobile app. To know more details about lightning/navigation service, Click Here.
Files we used in this post example →
lwcNavigateAction.html | LWc HTML File | Template HTML file to create Navitate to different page types in Salesforce Lightning Web Component (LWC) |
lwcNavigateAction.js | LWC JavaScript File | It’s hold a javascript on click function to Navitate page on click button in lwc. |
lwcNavigateAction.js-meta.xml | XML Meta File | It is used to where this lightning web component file you want to display as lightning__AppPage, lightning__RecordPage, lightning__HomePage. |
Final Output →
Other related post that would you like to learn in Salesforce
- Find the below steps
Create Lightning Web Component HTML
Step 1:- Create Lightning Web Component : lwcNavigateAction.html
SFDX:Lightning Web Component >> New >> lwcNavigateAction.html
lwcNavigateAction.html [Lightning Web Component HTML]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 |
<template> <lightning-card> <div class="slds-m-around_medium"> <h3 class="slds-text-heading_medium"><lightning-icon icon-name="custom:custom83" size="small"></lightning-icon> <strong style="color:#270086; font-size:13px; margin-right:5px;"> How to apply Navigation Service Library (NavigationMixin) in Lightning Web Component (LWC) </strong></h3> <br/><br/> <table class="slds-table slds-table_cell-buffer slds-table_bordered slds-table_col-bordered" width="100%" border="1" bordercolor="#999" cellpadding="5" cellspacing="0" style="border-collapse:collapse; font-weight:bold;"> <tbody> <tr> <td valign="top" width="50%" style="background:#eee;"><lightning-button variant="brand" label="New Account" onclick={actionToCreateAccountNav}></lightning-button></td> <td valign="top" width="50%" class="navigateTxt" >Navigate to Create New Account </td> </tr> <tr> <td valign="top" style="background:#eee;"><lightning-button variant="brand" label="Account Recent list View" onclick={actionToAccountListViewNav}></lightning-button></td> <td valign="top" style="color:#ff8000;">Navigate to Account List View </td> </tr> <tr> <td valign="top" style="background:#eee;"><lightning-button variant="brand" label="Navigate to Contact Recent View" onclick={actionToContactHomeNav}></lightning-button></td> <td valign="top" style="color:#ff8000;">Navigate to Contact Recent View Page</td> </tr> <tr> <td valign="top" style="background:#eee;"> <lightning-button variant="brand" label="Navigate to Files" onclick={actionToFilesHomeNav}></lightning-button></td> <td valign="top" style="color:#ff8000;">Navigate to Files</td> </tr> <tr> <td valign="top" style="background:#eee;"><lightning-button variant="brand" label="Navigate to Home" onclick={actionToHomeNav}></lightning-button></td> <td valign="top" style="color:#ff8000;">Navigate Home Page</td> </tr> <tr> <td valign="top" style="background:#eee;"><lightning-button variant="brand" label="Navigate to Chatter Page" onclick={actionToChatterNav}></lightning-button></td> <td valign="top" style="color:#ff8000;">Navigate to Chatter Page</td> </tr> <tr> <td valign="top" style="background:#eee;"> <lightning-button variant="brand" label="Navigate to Reports" onclick={actionToReportsNav}></lightning-button></td> <td valign="top" style="color:#ff8000;">Navigate to Reports</td> </tr> <tr> <td valign="top" style="background:#eee;"><lightning-button variant="brand" label="Navigate to Web Browser" onclick={actionToWebNav}></lightning-button></td> <td valign="top" style="color:#ff8000;">Navigate to Web Browser</td> </tr> <tr> <td valign="top" style="background:#eee;"> <lightning-button variant="brand" label="Navigate to Visualforce page " onclick={actionToVFNav}></lightning-button></td> <td valign="top" style="color:#ff8000;">Navigate to Visualforce page</td> </tr> </tbody> </table> <br/><br/> <br/> <!--Start RelatedTopics Section--> <div style="border:1px #ddd solid; padding:10px; background:#eee; margin:40px 0;"> <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/05/thumbsUpLike.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:16px; font-style:italic; display:inline-block; margin-right:5px;">Don't forget to check out:-</span><a href="https://www.w3web.net/" target="_blank" rel="noopener noreferrer" style="text-decoration:none;" data-aura-rendered-by="440:0">An easy way to learn step-by-step online free Salesforce tutorial, To know more Click <span style="color:#ff8000; font-size:18px;" data-aura-rendered-by="442:0">Here..</span></a></strong></p> <br/><br/> <p data-aura-rendered-by="435:0"><img src="https://www.w3web.net/wp-content/uploads/2021/07/tickMarkIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;" data-aura-rendered-by="436:0"><strong data-aura-rendered-by="437:0"><span style="font-size:17px; font-style:italic; display:inline-block; margin-right:5px; color:rgb(255 128 0);">You May Also Like →</span> </strong></p> <div style="display:block; overflow:hidden;"> <div style="width: 50%; float:left; display:inline-block"> <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> <li><a href="https://www.w3web.net/lwc-get-set-lightning-checkbox-value/" target="_blank" rel="noopener noreferrer">How to get selected checkbox value in lwc</a></li> <li><a href="https://www.w3web.net/display-account-related-contacts-in-lwc/" target="_blank" rel="noopener noreferrer">how to display account related contacts based on AccountId in lwc</a></li> <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to create lightning datatable row actions in lwc</a></li> <li><a href="https://www.w3web.net/if-and-else-condition-in-lwc/" target="_blank" rel="noopener noreferrer">how to use if and else condition in lwc</a></li> <li><a href="https://www.w3web.net/get-selected-radio-button-value-and-checked-default-in-lwc/" target="_blank" rel="noopener noreferrer">how to display selected radio button value in lwc</a></li> </ul> </div> <div style="width: 50%; float:left; display:inline-block"> <ul style="list-style-type: square; font-size: 16px; margin: 0 0 0 54px; padding: 0;"> <li><a href="https://www.w3web.net/display-account-related-contacts-lwc/" target="_blank" rel="noopener noreferrer">display account related contacts based on account name in lwc</a></li> <li><a href="https://www.w3web.net/create-lightning-datatable-row-actions-in-lwc/" target="_blank" rel="noopener noreferrer">how to insert a record of account Using apex class in LWC</a></li> <li><a href="https://www.w3web.net/fetch-picklist-values-dynamic-in-lwc/" target="_blank" rel="noopener noreferrer">how to get picklist values dynamically in lwc</a></li> <li><a href="https://www.w3web.net/edit-save-and-remove-rows-dynamically-in-lightning-component/" target="_blank" rel="noopener noreferrer">how to edit/save row dynamically in lightning component</a></li> <li><a href="https://www.w3web.net/update-parent-object-from-child/" target="_blank" rel="noopener noreferrer">update parent field from child using apex trigger</a></li> </ul> </div> <div style="clear:both;"></div> <br/> <div class="youtubeIcon"> <a href="https://www.youtube.com/channel/UCW62gTen2zniILj9xE6LmOg" target="_blank" rel="noopener noreferrer"><img src="https://www.w3web.net/wp-content/uploads/2021/11/youtubeIcon.png" width="25" height="25" style="vertical-align:top; margin-right:10px;"/> <strong>TechW3web:-</strong> To know more, Use this <span style="color: #ff8000; font-weight: bold;">Link</span> </a> </div> </div> </div> <!--End RelatedTopics Section--> </div> </lightning-card> </template> |
Create Lightning Web Component JavaScript
Step 2:- Create Lightning Web Component : lwcNavigateAction.js
SFDX:Lightning Web Component >> New >> lwcNavigateAction.js
lwcNavigateAction.js [LWC JavaScript File]
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 |
import { LightningElement,api } from 'lwc'; import { NavigationMixin } from 'lightning/navigation'; export default class LwcNavigateAction extends NavigationMixin(LightningElement) { @api recordId; actionToCreateAccountNav() { this[NavigationMixin.Navigate]({ type: 'standard__objectPage', attributes: { objectApiName: 'Account', actionName: 'new' }, }); } actionToAccountListViewNav() { this[NavigationMixin.Navigate]({ type: 'standard__objectPage', attributes: { objectApiName: 'Account', actionName: 'list' }, state: { filterName: 'Recent' }, }); } actionToContactHomeNav() { this[NavigationMixin.Navigate]({ "type": "standard__objectPage", "attributes": { "objectApiName": "Contact", "actionName": "home" } }); } actionToFilesHomeNav() { this[NavigationMixin.Navigate]({ type: 'standard__objectPage', attributes: { objectApiName: 'ContentDocument', actionName: 'home' }, }); } actionToHomeNav() { this[NavigationMixin.Navigate]({ type: 'standard__namedPage', attributes: { pageName: 'home' }, }); } actionToChatterNav() { this[NavigationMixin.Navigate]({ type: 'standard__namedPage', attributes: { pageName: 'chatter' }, }); } actionToReportsNav() { this[NavigationMixin.Navigate]({ type: 'standard__objectPage', attributes: { objectApiName: 'Report', actionName: 'home' }, }); } actionToWebNav() { this[NavigationMixin.Navigate]({ "type": "standard__webPage", "attributes": { "url": "https://www.w3web.net/" } }); } actionToVFNav() { this[NavigationMixin.GenerateUrl]({ type: 'standard__webPage', attributes: { url: '/apex/wrapperRelatedListVfp?id=' + this.recordId } }).then(generatedUrl => { window.open(generatedUrl); }); } } |
Create Lightning Web Component Meta XML
Step 3:- Create Lightning Web Component : lwcNavigateAction.js-meta.xml
SFDX:Lightning Web Component >> New >> lwcNavigateAction.js-meta.xml
lwcNavigateAction.js-meta.xml [LWC Meta Data XML]
1 2 3 4 5 6 7 8 9 10 |
<?xml version="1.0" encoding="UTF-8"?> <LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata"> <apiVersion>45.0</apiVersion> <isExposed>true</isExposed> <targets> <target>lightning__AppPage</target> <target>lightning__RecordPage</target> <target>lightning__HomePage</target> </targets> </LightningComponentBundle> |
Further post that would you like to learn in Salesforce
If you think you and your family members also like to earn money in additional time, then go this link. In this site, you can earn money for the lifetime at low effort. To Earn Unlimited Everyday With This Trick, Sign Up Free.. Get Started for Free
Flyout is a free, simple way to earn money by placing sponsored content on your blog
Check Out:- Earn Unlimited Everyday With This Trick:- Click Here
How do I transfer record ID to LWC?
To get the current record ID in LWC, we just need to import the api decorator from lwc module. Then, we have to declare the property recordId with @api decorator.
What is connectedCallback in LWC?
The connectedCallback() is a lifecycle hook in lightning web component . It fires when a component is inserted into the DOM. It runs once when the component inserted.
What is disconnected callback in LWC?
The disconnectedCallback() lifecycle hook fires when a component is removed from the DOM. Both hooks flow from parent to child. You can't access child elements from the callbacks because they don't exist yet.
Related Topics | You May Also Like
- Your reaction of the article ▾
Great work with the hard work you have done I appreciate your work thanks for sharing it…
Wow, amazing block structure! How long
Have you written a blog before? Working on a blog seems easy.
The overview of your website is pretty good, not to mention what it does.
In the content!
Very creative and knowledgeable. Thank you for sharing this . Kindly visit our website
Great post. I was checking constantly this blog and I’m impressed! Very useful information specifically the last part I care for such information a lot. I was looking for this certain info for a long time. Thank you and good luck.
Hey how can we navigate to Page 1 to Page 2 on community , where Page is having Lwc 1 and page 2 is having lwc 2.
My requirement is to pass values from lwc1 to lwc 2 and navigate to Page2 on COMMUNITY.