3

With datatable, the 2nd object references (Vendor_Name__r.Name and Purchase_Agreement__r.Name) here weren't displaying in the view. The entire column was blank.

 @track columns = [
        {label: 'Vendor', fieldName: 'Vendor_Name__r.Name', sortable: true},
        {label: 'Quote Name', fieldName: 'Name', sortable: true},
        {label: 'Status', fieldName: 'Status__c', sortable: true},
        {label: 'Purchase Agreement', fieldName: 'Purchase_Agreement__r.Name', sortable: true}
    ];

enter image description here

When I change Vendor_Name__r.Name TO Vendor_Name__c AND Purchase_Agreement__r.Name to Purchase_Agreement__c, the Ids render in the view just fine.

@track columns = [
        {label: 'Vendor', fieldName: 'Vendor_Name__r.Name', sortable: true},
        {label: 'Quote Name', fieldName: 'Name', sortable: true},
        {label: 'Status', fieldName: 'Status__c', sortable: true},
        {label: 'Purchase Agreement', fieldName: 'Purchase_Agreement__r.Name', sortable: true}
    ];

enter image description here

As a workaround, I figured the following would work, but all rows in my datatable are now blank. enter image description here In the console, it does print this out:

***** quote: {"Vendor_Name__c":"0013k00002io4VgAAI","Name":"0029","Status__c":"Ready for Review","Purchase_Agreement__c":"a4vDL0000013VfjYAE","Id":"a4cDL000000hddsYAA","Vendor_Name__r":{"Name":"Acme1","Id":"0013k00002io4VgAAI"},"Purchase_Agreement__r":{"Name":"PA# 0001","Id":"a4vDL0000013VfjYAE"}}

HTML

<template if:true={quoteList}>
    <lightning-datatable 
        data={quoteList} 
        columns={columns} 
        key-field="Id"            
        default-sort-direction={defaultSortDirection}
        sorted-direction={sortDirection}
        sorted-by={sortedBy}
        onsort={handleSort}>
    </lightning-datatable>
</template>

JS

    @track columns = [
        {label: 'Vendor', fieldName: 'VendorName', sortable: true},
        {label: 'Status', fieldName: 'Status', sortable: true},
        {label: 'Quote Name', fieldName: 'QuoteName', sortable: true},
        {label: 'Purchase Agreement', fieldName: 'PurchaseAgreementName', sortable: true}
    ];


    connectedCallback() {
        getQuotes({rfqID : this.recordId, statusValue : this.filterValue})
        .then(results => {
            // this.quoteList = results;
            
            results.forEach(result => {
                console.log('**** VendorName: '+result.Vendor_Name__r.Name);
                this.quoteList.push({
                    VendorName: result.Vendor_Name__r.Name,
                    Status: result.Status__c,
                    QuoteName: result.Name,
                    PurchaseAgreementName: result.Purchase_Agreement__r.Name
                });
            })

            this.quoteList.forEach(
                quote => console.log('***** quote: ' + JSON.stringify(quote))
            );


            this.error = undefined;
        })
        .catch(error => {
            this.error = error;
            this.queryResults = undefined;
        });
}
0

1 Answer 1

1

What you need to understand here is that the data table component only picks up data from the object in your array. Technically speaking, the relationship data is there, but is not part of the object, it is an attribute of a nested object, hence why it can't access it.

The quickest approach you can take here - from my experience - is to iterate the list before assigning the data to the attribute used by the data table component, properly formatting the value of the field. You'll probably need some recursive function that is able to gather data from multiple levels of nesting.

Instead of doing this:

apexMethod().then((res) => {
    this.valueOfTheDataTable = res;
})

It would look something like this:

apexMethod().then((res) => {
    // process the data before assigning to the component's variable below
    res.forEach(row => {
        row.vendorName = getFieldValue(row, "Vendor_Name__r.Name");
    });

    this.valueOfTheDataTable = res;
})

And of course, in your column definition for the data table, you would use vendorName, the value that the forEach function processed, instead of the lookup path in the Salesforce hierarchy, Vendor_Name__r.Name.

4
  • 1
    I have such a recursive function that I posted here. I use it so frequently it's one of the first methods I put into any project when I decide I might need nested object access.
    – sfdcfox
    Commented Jan 18, 2023 at 3:09
  • Thanks Renato. Not sure why recursion would be needed. Also, I'm ok at js, not great, so the syntax was a bit confusing. Commented Jan 18, 2023 at 3:43
  • I was able to flatter the referenced colums with a wrapper class in my Apex. Commented Jan 18, 2023 at 3:55
  • That's another approach, but ideally you shouldn't need to edit the back end for this. This adds some overhead and makes the method invocation slower, plus this wrapper class might not have the same schema you might need in a different component that uses the same method/query. Which is why handling this kind of operation is better done in the front end (or at least implement this in a controller that calls a selector class that is common to other classes). Commented Jan 18, 2023 at 15:27

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