Sunday 19 June 2022

 getRelatedListsInfo

Use this wire adapter to get the metadata for RelatedLists in an object’s default layout.

Parameters

  • parentObjectApiName—(Required) The API name of a parent object that you want to get related lists for, like an Account.
  • recordTypeId—(Optional) The ID of the parent record type.

Usage

This code example fetches the related lists info by API name of the parent object, then iterates through the related lists.

HTML

<template>
    <lightning-card title="Related List of Account">
        <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>


JS

import { LightningElement,wire} from 'lwc';
import { getRelatedListsInfo } from 'lightning/uiRelatedListApi';
export default class GetRelatedListDetails extends LightningElement {
    error;
    relatedLists;
    @wire(getRelatedListsInfo, {
        parentObjectApiName: 'Account',
       
    })listInfo({ error, data }) {
        if (data) {
            this.relatedLists = data.relatedLists;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.relatedLists = undefined;
        }
    }
}

XML

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>54.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
         <target>lightning__RecordPage</target>
    </targets>

</LightningComponentBundle>

RESULT:





 getRelatedListRecords

Use this wire adapter to get RelatedList records.

Parameters

  • parentRecordId—(Required) The ID of the parent record that you want to get related lists for, like an Account ID.
  • relatedListId—(Required) The API name of a related list object, like Contacts, Opportunities, or Cases.
  • fields—(Optional) The API names of the related list’s column fields.
  • optionalFields—(Optional) The API names of additional fields in the related list.
  • pageSize—(Optional) The number of list records to return per page.
  • sortBy—(Optional) An array of field API names to sort the related list by. Accepts only one value per request.
  • where—(Optional) The filter to apply to related list records, in GraphQL syntax.
This code example fetches the record for the Opportunity-related list in the Account object, then iterates the opportunity to display the Id and Name.


HTML

<template>
    <lightning-card title="Opportunity Related List Records">
        <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.Id.value} -  {rec.fields.Name.value}
                    </p>
                </template>
            </div>
        </template>
    </lightning-card>

</template>

JS
import { LightningElement,wire} from 'lwc';
import { getRelatedListRecords } from 'lightning/uiRelatedListApi';
export default class GetRelatedListRecords extends LightningElement {
    error;
    records;
    @wire(getRelatedListRecords, {
        parentRecordId: '0010I00002Zq6IMQAZ',
        relatedListId: 'Opportunities',
        fields: ['Opportunity.Id','Opportunity.Name']
    })listInfo({ error, data }) {
        if (data) {
            this.records = data.records;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.records = undefined;
        }
    }
}

XML
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>54.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
         <target>lightning__RecordPage</target>
    </targets>

</LightningComponentBundle>


RESULT




 getRelatedListInfo

Use this wire adapter to get the metadata for RelatedList.

  • parentObjectApiName—(Required) The API name of a parent object that you want to get related lists for, like an Account.
  • relatedListId—(Required) The API name of a related list object such as Contacts, Opportunities, or Cases.
  • recordTypeId—(Optional) The ID of the parent record type. If not provided, the default record type is used.

This code example fetches the metadata for the Opportunity-related list in the Account object, then iterates through the list of display columns.


HTML

<template>
    <lightning-card title="Get Opportunity Related List Information">
        <template if:true={displayColumns}>
            <div class="slds-m-around_medium">
                <template for:each={displayColumns} for:item="info">
                    <p key={info.fieldApiName}>
                        {info.label}
                    </p>
                </template>
            </div>
        </template>
    </lightning-card>
</template>

JS

import { LightningElement,wire } from 'lwc';
import { getRelatedListInfo } from 'lightning/uiRelatedListApi';
export default class GetRelatedListInfo extends LightningElement {
    error;
    displayColumns;
    @wire(getRelatedListInfo, {
        parentObjectApiName: 'Account',
        relatedListId: 'Opportunities',
    })listInfo({ error, data }) {
        if (data) {
            console.log('data',JSON.stringify(data))
            this.displayColumns = data.displayColumns;
            this.error = undefined;
        } else if (error) {
            this.error = error;
            this.displayColumns = undefined;
        }
    }
}

XML

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>54.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
         <target>lightning__RecordPage</target>
    </targets>
</LightningComponentBundle>

RESULT








Get Related List Record Count

Use this wire adapter to get the RelatedList record count.

 This code example fetches the record count for a related list.

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

HTML

<template>
    <lightning-card title="getRelatedListCount">
        <div class="slds-var-p-around_medium">
          <template if:true={responseData}>
            <div>No of opportunities in account related list - {responseData.count}</div>
          </template>
        </div>
      </lightning-card>
</template>

JS

import { LightningElement,wire} from 'lwc';
import {getRelatedListCount} from 'lightning/uiRelatedListApi';
export default class GetReleatedListCount extends LightningElement {
    responseData;
    error;
    @wire(getRelatedListCount, {
      parentRecordId:'0010I00002Zq6IMQAZ', // AccountId
      relatedListId:'Opportunities' //RelatedList object Name
    })listInfo({error, data}){
      if(data){
         this.responseData = data;
         this.error = undefined;
      }
      if(error){
        this.error = error;
        this.responseData = undefined;
      }
    }
  }
 

XML

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>54.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
         <target>lightning__RecordPage</target>
    </targets>

</LightningComponentBundle>

RESULT