Navigating to Record Detail Pages in Salesforce Lightning Web Components (LWC)

Arun
2 min readMay 12, 2023

--

In Salesforce Lightning Web Components (LWC), navigating to different pages is a common requirement. One such scenario is navigating to a record’s detail page when a user clicks on a link. This post will guide you through the process of creating clickable links in LWC that lead to record detail pages, using the NavigationMixin provided by Salesforce.

Understanding NavigationMixin

The NavigationMixin is a class provided by Salesforce that allows you to navigate to different pages in your Lightning Experience. It provides the Navigate method, which you can use to navigate to various pages like a record's detail page, a new record creation page, or even an external URL.

Setting Up Your Component

Firstly, to use the NavigationMixin, you have to extend it in your component's JavaScript controller:

import { LightningElement, api, wire } from 'lwc';
import { NavigationMixin } from 'lightning/navigation';

export default class YourComponentName extends NavigationMixin(LightningElement) {
// Your component's code goes here
}

Creating Clickable Links

Now, let’s suppose you have a table where each row represents a record, and you want to make certain fields clickable. For example, we have a Name field that we want to make clickable:

<td scope="row">
<div class="slds-truncate" title={item.StepName}>
<a data-id={item.Id} onclick={navigateToRecord}>{item.Name}</a>
</div>
</td>

Here, we added an onclick event handler to call a navigateToRecord function when the link is clicked. We also added a data-id attribute to store the ID of the record we want to navigate to.

Navigating to a Record’s Detail Page

Next, we’ll define the navigateToRecord function in our JavaScript controller. This function uses the NavigationMixin.Navigate method to navigate to the record's detail page:

navigateToRecord(event) {
const recordId = event.currentTarget.dataset.id;

this[NavigationMixin.Navigate]({
type: 'standard__recordPage',
attributes: {
recordId: recordId,
actionName: 'view'
}
});
}

In this function, we’re retrieving the record ID from the data-id attribute of the clicked link and passing it to the NavigationMixin.Navigate method to navigate to the record's detail page.

Conclusion

And that’s it! Now, when you click on the Name link in your table, it will navigate to the corresponding record's detail page. By following this approach, you can make any field in your table clickable and navigate to the respective record's detail page. This can greatly enhance the usability of your components and provide a more seamless user experience. Remember to always test your components thoroughly to ensure they work as expected.

--

--