Add Related Fields Columns in Salesforce LWC Data Table

In Salesforce, we have objects with related lists displayed on the record page of each record. The related lists show a list of records linked to the current record through a lookup or master-detail relationship, to view related information without leaving the current record page.

When it comes to the related field column in the LWC data table, we fetch the data of the object’s related fields and display them in the object data table along with other fields.

In this Salesforce tutorial, we will learn how to add related field columns to the Salesforce LWC data table.

Add Related Fields Columns in LWC Data Table

To add related fields as a column in the LWC datatable, we need the fields from a parent or lookup object. For example, displaying the accounts related to contact records on the data table.

To fetch the Account name, we can refer to it in the SOQL query as Account.Name, but the LWC lightning data table does not support dot notation fields and does not render them in the data table.

To display the related list columns in the LWC data table, we need to format them using the flatten transformation feature.

In the example below, we will display the contact records along with the related list columns Account name and the Owner Name.

  1. First, define a controller class to fetch the contact records and the related list fields Account name and Owner name (contact owner).
public with sharing class ContactController {
    @AuraEnabled(cacheable=true)
    public static List<Contact> getRelatedContacts() {
        return [SELECT Id, Name, Account.Name, Owner.Name 
        FROM Contact 
        LIMIT 10];
    }
}
  1. Now, create an LWC component and enter the code below in the HTML template to render data in an LWC data table.
<template>
    <lightning-card title="Contacts with related fields"></lightning-card>
    <lightning-datatable
        key-field="Id"
        data={data}
        columns={columns}
        hide-checkbox-column="true"
    ></lightning-datatable>
</template>
  1. We transformed the data using the code below in the JS file to format the related lists column for display in the LWC data table.
import { LightningElement, wire } from 'lwc';
import getRecords from '@salesforce/apex/ContactController.getRelatedContacts';

export default class RelatedListDataTable extends LightningElement {
    
    columns = [
        { label: 'Record Name', fieldName: 'Name', type: 'text' },
        { label: 'Account Name', fieldName: 'AccountName', type: 'text' },
        { label: 'Owner Name', fieldName: 'OwnerName', type: 'text' }
    ];

    data = []; 
    @wire(getRecords)
    wiredRecords({ error, data }) {
        if (data) {
            this.data = data.map(record => ({
                Id: record.Id,
                Name: record.Name,
                AccountName: record.Account ? record.Account.Name : '', 
                OwnerName: record.Owner ? record.Owner.Name : ''
            }));
        } else if (error) {
            console.error('Error fetching records:', error);
        }
    }
}

In the above method, we have used the @wire decorator to link the fetched records to the wiredRecords function, which processes the data. If data is returned, it maps each record into an object with Id, Name, AccountName, and OwnerName, storing it in this.data.

For example, mapped AccountName in the table from the fetched value Account.Name using AccountName: record.Account ? record.Account.Name.

  1. Make the component visible to the Lightning pages using the code below in the meta.xml file inside the <LightningComponentBundle> tag
<isExposed>true</isExposed>

    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
    </targets>
  1. After this, deploy the LWC component to the Lightning page. To deploy the component, navigate to the lightning page > settings > Edit page.

In the Lightning App Builder, add the component to the page region and save the changes.

Now you will see the data table with the contact record name, their related account, and related user(owner) names in the table columns.