lightning/uiRelatedListApi can be used to find number of related records using Lightning Web Component. Check the sample code below:
Sample Code:
HTML:
<template>
<lightning-card>
No of Contacts is {recordCount}
</lightning-card>
</template>
JavaScript:
import { LightningElement, wire, api } from 'lwc';
import { getRelatedListCount } from 'lightning/uiRelatedListApi';
export default class RelatedListComponent extends LightningElement {
@api recordId;
recordCount;
@wire( getRelatedListCount, {
parentRecordId: '$recordId',
relatedListId: 'Contacts'
})listInfo( { error, data } ) {
if ( data ) {
console.log( 'Data is ' + JSON.stringify( data ) );
this.recordCount = data.count;
} else if ( error ) {
console.log( 'Error occured', JSON.stringify( error ) );
}
}
}
js-meta.xml:
<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
<apiVersion>52.0</apiVersion>
<isExposed>true</isExposed>
<targets>
<target>lightning__RecordPage</target>
</targets>
<targetConfigs>
<targetConfig targets="lightning__RecordPage">
<objects>
<object>Account</object>
</objects>
</targetConfig>
</targetConfigs>
</LightningComponentBundle>
Output: