๐Ÿ”„ How to Use getRelatedListRecords in LWC to Fetch Related List Data in Salesforce

Published on November 17, 2025 โ€ข by Anand Kumar Jha

๐Ÿ”น Introduction

Salesforce provides a powerful UI API method called getRelatedListRecords that allows you to fetch related list data without writing any Apex.

  • Contacts related to an Account
  • Opportunities related to an Account
  • Cases under a Contact
  • Any standard or custom related list

In this tutorial, youโ€™ll learn how to fetch Contacts related to an Account and display them in a lightning-datatable.

๐ŸŽฏ Goal

  • Use getRelatedListRecords via UI API
  • Map LDS response into a usable JS array
  • Display data using lightning-datatable
  • Handle errors cleanly

๐Ÿ—๏ธ Step 1: LWC HTML Structure


<template>
  <lightning-card title="Contacts (Related List)">
    <template if:true={data}>
      <lightning-datatable
        key-field="Id"
        data={data}
        columns={columns}
      ></lightning-datatable>
    </template>

    <template if:true={error}>
      <p>Error loading related records.</p>
    </template>
  </lightning-card>
</template>

โš™๏ธ Step 2: JavaScript


import { LightningElement, api, wire } from "lwc";
import { getRelatedListRecords } from "lightning/uiRelatedListApi";

export default class TestGetRelatedListData extends LightningElement {
  @api recordId;

  columns = [
    { label: "Name", fieldName: "Name" },
    { label: "Email", fieldName: "Email" }
  ];

  data = [];
  error;

  @wire(getRelatedListRecords, {
    parentRecordId: "$recordId",
    relatedListId: "Contacts",
    fields: ["Contact.Id", "Contact.Name", "Contact.Email"]
  })
  wiredRecords({ data, error }) {
    if (data) {
      this.data = data.records.map(rec => ({
        Id: rec.fields.Id.value,
        Name: rec.fields.Name.value,
        Email: rec.fields.Email.value
      }));
      this.error = undefined;
    } else if (error) {
      this.error = error;
      this.data = [];
    }
  }
}

๐Ÿงพ Step 3: meta.xml


<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
  <apiVersion>62.0</apiVersion>
  <isExposed>true</isExposed>
  <targets>
    <target>lightning__RecordPage</target>
  </targets>
</LightningComponentBundle>

๐Ÿง  How It Works

  • parentRecordId โ†’ current record context
  • relatedListId โ†’ related list API name
  • fields โ†’ fields to fetch
  • LDS returns nested JSON, so we manually map it for datatable usage

๐Ÿ’ฌ Bonus Tips

  • Works with any related list (Cases, Opportunities, custom)
  • No Apex, no SOQL
  • Respects FLS & CRUD automatically

๐Ÿ“บ Watch the Video

โ–ถ Watch on YouTube

๐Ÿงพ Conclusion

getRelatedListRecords is one of the easiest and most powerful ways to load related list data in LWC without Apex.