Add Hyperlinks to Columns in LWC Lightning Data Table

In the Salesforce LWC Lightning data table, you might be required to add a hyperlink to a record name or links to a URL in a table column to navigate to the record detail page or an external URL.

In this Salesforce tutorial, we will learn how to add hyperlinks to columns in the LWC Lightning datatable.

In the examples below, we will see the method to add hyperlinks for the following scenarios.

  • Add hyperlink to record name in LWC data table
  • Add a hyperlink in the LWC data table for an external URL
  • Add a hyperlink to view the parent records in the LWC data table

Hyperlinks in Salesforce LWC Lightning data tables

In Salesforce Lightning data tables, to make a column clickable as a hyperlink and direct users to a record detail page, external URL, or custom destination, we configure the record’s values with a URL link using the “type: url” configuration.

With this URL, the click action will navigate to the record page using the attribute target: ‘_self,’ and for the external source URL, we use the recordLink: ‘https://www.example.com‘ attribute to navigate to that external site instead of a Salesforce page.

Add Hyperlink to Record name in LWC data table

In the example below, we will create a Lightning Web Component (LWC) data table with hyperlinks to record names, dynamically fetching contact records.

  1. First, we will create a controller class to fetch and display Contact object records.
public with sharing class ContactController {
    @AuraEnabled(cacheable=true)
    public static List<Contact> getContactList() {
        return [SELECT Id, Name, Email, Phone FROM Contact LIMIT 10];
    }
}
  1. Create an LWC component and enter the code below in the HTML file.
<template>
    <lightning-card title="LWC table with hyperlink">
        <div class="slds-m-around_medium">
            <lightning-datatable
                key-field="Id"
                data={contacts}
                columns={columns}
                hide-checkbox-column>
          </lightning-datatable>
        </div>
    </lightning-card>
</template>
  1. After this, enter the code below in the JS file of the LWC component, defining the logic of adding hyperlinks to the contact names in the data table.
import { LightningElement, wire } from 'lwc';
import getContactList from '@salesforce/apex/ContactsController.getContactList';

export default class DynamicDataTable extends LightningElement {
    contacts;
    error;

    columns = [
        {
            label: 'Contact Name',
            fieldName: 'link',
            type: 'url',
            typeAttributes: { label: { fieldName: 'Name' }, target: '_blank' }
        },
        { label: 'Email', fieldName: 'Email'},
        { label: 'Phone', fieldName: 'Phone'}
    ];

    @wire(getContactList)
    wiredContacts({ error, data }) {
        if (data) {
            this.contacts = data.map(contact => ({
                ...contact,
                link: '/lightning/r/Contact/${contact.Id}/view'
            }));
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.contacts = undefined;
            console.error('Error fetching contacts:', error);
        }
    }
}

In the above code, we first fetched contacts from the controller class using getContacts. After this, we defined the array of table columns in which we added hyperlinks to the contact name.

We used type: ‘url’ for the contact name column with a link field, meaning it’ll display a clickable URL. The typeAttributes map the contact’s name to the link’s label and sets it to open in a new tab with target: ‘_blank’. The other columns (Email and Phone) are mapped as text values without links.

To hyperlink the email and phone columns, you have to modify the code as follows.

{ label: 'Email', fieldName: 'Email', type: ' email'},
 { label: 'Phone', fieldName: 'Phone', type: 'phone'}
  1. To make