Sunday 19 June 2022

 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




No comments:

Post a Comment