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 {

@api recordId;
error;
records;


@wire(getRelatedListRecordsBatch, {
parentRecordId: '$recordId',
relatedListParameters: [
{
relatedListId: 'Contacts',
fields: ['Contact.Id'
,'Contact.Name']
},
{
relatedListId: 'Opportunities',
fields: ['Opportunity.Id'
,'Opportunity.Name']
}
]
}) 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;
}
}
}

The getRelatedListRecordsBatch wire adapter in the above exaple controller is to retrieve the accounts-related lists (Contacts, Opportunities) data without writing apex code.

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

  • parentRecordId — (Required) The ID of the parent record for which you want to retrieve related lists, such as an Account ID.
  • relatedListId — (Required) A related list object’s API name, such as Contacts, Opportunities etc
  • fields —(Optional) The API names of the column fields in the related list.
import {LightningElement,api,track, wire } from 'lwc';
import { getRelatedListRecords } from 'lightning/uiRelatedListApi';
import { getRecord,getFieldValue } from 'lightning/uiRecordApi';
import ACCOUNT_RECORDTYPE from '@salesforce/schema/Account.RecordType.Name';
import ACCOUNT_PARENTID from '@salesforce/schema/Account.ParentId';
import ACCOUNT_INDUSTRY from '@salesforce/schema/Account.Industry';

const _FIELDS = [ACCOUNT_RECORDTYPE, ACCOUNT_PARENTID, ACCOUNT_INDUSTRY];

export default class iv_AccountBrandDetailViewer extends LightningElement {

@api recordId;
parentAccountId = null;
account_Industry = null;
account_RecordTypeName = null;
@track brandDetailItems;

@wire(getRecord, {
recordId: '$recordId',
fields: _FIELDS
})
wiredAccountRecord({data,error}) {
// console.log('wiredAccountRecord:', data );
if (data) {
this.parentAccountId = data.fields['ParentId'].value;
this.account_Industry = data.fields['Industry'].value;
this.account_RecordTypeName = data.fields['RecordType']['value']['fields']['Name'].value;
}
}

@wire(getRelatedListRecords, {
parentRecordId: '$recordId',
relatedListId: 'BrandDetails__r',// customo Object(Child to parent)
fields: [
/* Business Brand Fields */
'iv_BrandDetail__c.iv_Brand__r.Name',
'iv_BrandDetail__c.iv_Brand__r.iv_Brand_Colour_Hex__c'
],
sortBy: ['iv_BrandDetail__c.iv_Brand__r.iv_Brand_Display_Order__c']
})
brandDetailsListInfo({error, data}) {
if (data) {
this.brandDetailItems = data.records;
}else if (error) {
console.error('Error', error);
this.brandDetailItems = undefined;
}
}// End of brandDetailsListInfo
}

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

Parameters:
parentObjectApiName —is the required API name of a parent object, like an Account, from which you want to fetch related lists.
recordTypeID —(Optional) The parent record type’s ID.

Returs: Related List Summary Collection
data: FetchRespons

import ACCOUNT_OBJECT from '@salesforce/schema/Account';
export default class GetRelatedListInfo extends LightningElement {
error;
relatedLists;
@wire(getRelatedListsInfo, {
parentObjectApiName: ACCOUNT_OBJECT,
recordTypeId: '0121X00000********' //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;
}
}
}

Leave a comment