0

I have a custom object whose fields I want to display in an LWC under a tab on the Account Record page. I need criteria to query that related record, I only need one record stored and it's rare that there are multiple records matching the criteria. I also want to navigate from the LWC to the related record through a button.

I am unable to figure out how to query the related record and get its ID for the navigation button and pass it in HTML to display fields. That's the (hopefully) only thing I need help with.

Here's my JS code:

import { NavigationMixin } from 'lightning/navigation';
import { getRelatedListRecords } from 'lightning/uiRelatedListApi';

export default class CmpAccSCRecDisplay extends NavigationMixin(LightningElement)
{
        @api recordId;
        error;
        record;  
        @wire(getRelatedListRecords, {
            parentRecordId: this.recordId,
            relatedListId: 'Software_Assets__c',
            fields: ['Software_Assets__c.Id']
        })listInfo({ error, data }) {
            if (data) {
                this.record = data.record;
                this.error = undefined;
            } else if (error) {
                this.error = error;
                this.record = undefined;
            }
        }    
        handleSCClick()
        {
            this[NavigationMixin.Navigate]({
                type: 'standard__recordPage',
                attributes: {
                    recordId: this.data.record.recordId,
                    objectApiName: 'Software_Assets__c',
                    actionName: 'view',
                }
            });
        }
    }
    ```

1 Answer 1

4

You have a couple of problems you need to address.


@wire(getRelatedListRecords, {
    parentRecordId: '$recordId',

You want to use the reactive API; your component may be maintained across several navigations, so you need to react to those changes.


relatedListId: 'Software_Assets__r',

Relationship names end in __r. You can't just specify the child object, as it could have multiple relationships to the same parent.


})listInfo({ error, data }) {
    this.record = data;
    this.error = error;
}

You don't need to check if these are set, just set them. You're not doing anything special. Or, you could even just use the default handler:

@wire(getRelatedListRecords, {
    parentRecordId: '$recordId',
    relatedListId: 'Software_Assets__r',
    fields: ['Software_Assets__c.Id']
}) data;   

recordId: this.data.record.recordId,

You need to read the documentation. As stated, it returns records, not record, even if there's just one item. So:

recordId: this.data.records[0].recordId,
1
  • sfdcfox, thank you so much for the corrections! Especially about records vs record. I appreciate your help. Can you please throw light on how I can query the related record with specific conditions? That's what I am finding the most challenging here
    – Raghu Rao
    Jul 12, 2022 at 16:47

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .