Use wire adapters to get related list records.

Salesforce introduced a new module lightning/uiRelatedListApi in the summer’22 release for the LWC framework, which includes new wire adapters for retrieving related list records.

  • getRelatedListRecordsBatch—Returns record data for a batch of related lists.
  • getRelatedListInfoBatch—Returns metadata for a batch of related lists.
import { LightningElement,api,wire } from 'lwc';
import { getRelatedListRecordsBatch } from 'lightning/uiRelatedListApi';
export default class GetRelatedListRecordBatch extends LightningElement {
//get Account Related List Records
@api recordId;
error;
records;
@wire(getRelatedListRecordsBatch, {
parentRecordId: '$recordId',
relatedListParameters: [
{
relatedListId: 'Contacts',
fields: ['Contact.Name','Contact.Id']
},
{
relatedListId: 'Opportunities',
fields: ['Opportunity.Name','Opportunity.Amount']
},
{
relatedListId: 'Cases',
fields: ['Case.Subject','Case.Id']
}
]
}) allListInfo({ error, data }) {
if (data) {
this.records = data.records;
console.log('data: '+JSON.stringify(data));
this.error = undefined;
} else if (error) {
this.error = error;
this.records = undefined;
}
}
}

I used the getRelatedListRecordsBatch wire adapter in the above LWC JS controller to retrieve the accounts-related lists (Contacts, Opportunities, Cases) data without writing apex code.

The result will return in JSON format:

Note: If a related list is not added to the parent object record’s page layout, you will not receive the result for that related list and will see an error message in the response. However, you will receive the response for other related lists.

The getRelatedListInfoBatch wire adapter will return related list metadata information.

Below wire adapters used for retrieving records, metadata, and record count from a related list are now widely available (GA).

  • getRelatedListRecords—Returns record data for a related list.
  • getRelatedListInfo—Returns metadata for a related list.
  • getRelatedListsInfo—Returns metadata for related lists in an object’s default layout.
  • getRelatedListCount—Returns the record count for a related list.

Use the getRelatedListRecords wire adapter to get single related list records.

Parameters

  • <strong>parentRecordId</strong>— (Required) The ID of the parent record for which you want to retrieve related lists, such as an Account ID.
  • <strong>relatedListId</strong>— (Required) A related list object’s API name, such as Contacts, Opportunities, or Cases.
  • <strong>fields</strong>—(Optional) The API names of the column fields in the related list.

Returns

<template>
<lightning-card title="wireGetRelatedListRecords">
<template if:true={records}>
<div class="slds-m-around_medium">
<template for:each={records} for:item="rec">
<p key={rec.fields.Id.value}>
{rec.fields.Name.value}
</p>
</template>
</div>
</template>
</lightning-card>
</template>
import { LightningElement,wire,api} from 'lwc';
import { getRelatedListRecords } from 'lightning/uiRelatedListApi';
export default class GetRelatedListRecords extends LightningElement {
@api recordId;
error;
records;
//get all contacts records of Account
@wire(getRelatedListRecords, {
parentRecordId: '$recordId',
relatedListId: 'Contacts',
fields: ['Contact.Name','Contact.Id']
}) contacts({ error, data }){
if (data) {
this.records = data.records;
console.log('data: '+JSON.stringify(data));
this.error = undefined;
} else if (error) {
this.error = error;
this.records = undefined;
}
}
}
data returned in JSON format

getRelatedListsInfo

This wire adapter can be used to retrieve the metadata for multiple RelatedLists.

Parameters

  • parentObjectApiName—(Required) The API name of a parent object, such as an Account, for which you want to retrieve related lists.
  • recordTypeID—(Optional) The parent record type’s ID

Returns

<template>
<lightning-card title="wireGetRelatedListsInfo">
<template if:true={relatedLists}>
<div class="slds-m-around_medium">
<template for:each={relatedLists} for:item="relatedList">
<p key={relatedList.label}>
{relatedList.label}
</p>
</template>
</div>
</template>
</lightning-card>
</template>
import { LightningElement,wire } from 'lwc';
import { getRelatedListsInfo } from 'lightning/uiRelatedListApi';
import ACCOUNT_OBJECT from '@salesforce/schema/Account';
export default class GetRelatedListInfo extends LightningElement {
error;
relatedLists;
@wire(getRelatedListsInfo, {
parentObjectApiName: ACCOUNT_OBJECT,
recordTypeId: '012E0000000RR6wIAG' //optional
})listInfo({ error, data }) {
if (data) {
this.relatedLists = data.relatedLists;
console.log('Data: '+ JSON.stringify(data));
this.error = undefined;
} else if (error) {
this.error = error;
this.relatedLists = undefined;
}
}
}

getRelatedListCount

To obtain the RelatedList record count, use this wire adapter.

Parameters

  • parentRecordId— (Required) The ID of the parent record for which you want to obtain related lists, such as an Account ID.
  • relatedListId— (Required) The API name of a related list object, such as Contacts, Opportunities, or Cases.

Returns

<template>
<lightning-card title="wireGetRelatedListCount">
<template if:true={responseData}>
<div class="slds-m-around_medium">
<p key={responseData.count}>
{responseData.count} : {responseData.hasMore}
</p>
</div>
</template>
</lightning-card>
</template>
import { LightningElement ,wire,api} from 'lwc';
import { getRelatedListCount } from 'lightning/uiRelatedListApi';
export default class GetRelatedListCount extends LightningElement {
error;
responseData;
@api recordId;
//Account's related contact list count
@wire(getRelatedListCount, {
parentRecordId: '$recordId',
relatedListId: 'Contacts'
})listInfo({ error, data }) {
if (data) {
this.responseData = data;
//console.log('data: '+JSON.stringify(data));
this.error = undefined;
} else if (error) {
this.error = error;
this.responseData = undefined;
}
}
}

Arun Kumar

Arun Kumar is a Certified Salesforce developer and Salesforce Champions (Platform Champions), a Computer Science graduate, working on the Salesforce platform and providing customer solutions using force.com as a Salesforce consultant/developer. He is the founder of SFDCLessons. :)

Leave a Reply