Sunday, 19 June 2022

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




No comments:

Post a Comment