π 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 an amazing UI API method called
getRelatedListRecords that lets you fetch related list
data without writing any Apex. This is extremely
useful when you want to display data like:
- 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 using
getRelatedListRecords and show them in a
lightning-datatable.
π― Goal
-
Use
getRelatedListRecordsto load related list data - Map LDS response into a usable JS array
- Display results using
lightning-datatable - Handle errors cleanly
ποΈ Step 1: LWC HTML Structure
This is the layout that displays related contacts inside a card:
<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 β Fetch Related List Records
Using @wire with
getRelatedListRecords:
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) {
console.log("Raw LDS Data:", JSON.parse(JSON.stringify(data)));
// Convert LDS format β datatable format
this.data = data.records.map((rec) => ({
Id: rec.fields.Id.value,
Name: rec.fields.Name.value,
Email: rec.fields.Email.value
}));
console.log("Datatable Data:", this.data);
this.error = undefined;
} else if (error) {
this.error = error;
this.data = [];
}
}
}
π§Ύ Step 3: meta.xml File
Expose your component on the record page:
<?xml version="1.0" encoding="UTF-8"?>
<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 β the current record where component is placed
- relatedListId β API name of related list (Contacts)
- fields β fields you want to fetch
- LDS returns nested JSON, so we manually extract values to map them into a clean datatable format
This means NO Apex, NO SOQL β everything is handled via UI API.
π¬ Bonus Tips
- You can sort or filter the retrieved data manually
- Replace Contacts with any related list (Opportunities, Cases, etc.)
- Use this in Account, Contact, or custom object record pages
- UI API respects FLS & CRUD automatically
πΊ Watch the Video Tutorial
Iβve also created a detailed explanation on YouTube:
π
Watch on YouTube (CodeWithAnand)
π§Ύ Conclusion
getRelatedListRecords is one of the easiest and most powerful ways to load related list data in LWC β without writing a single line of Apex. If youβre building record page components, this is a must-use tool.