1

I am struggling with relationship fields in a datatable using Lightning Web Components. I am querying through Apex and returning a list from the DB. I've looked through a bunch of threads here on suggestions but I can't seem to make anything work. I need to rename the __r relationship fields because it appears that the data table cannot render the data there. Here's my current approach after landing on this solution: Flatten data to display it using lightning datatable in LWC

I've also tried this solution here: How to change values of data returned from imperative Apex?

But couldn't figure out how to adapt it to my use case, which is on me.

I am sure I am making a silly mistake, being new to this. I would appreciate any advice.

I am getting the correct data if I just render the data table with the returned data and no __r names. Everything works fine.

     <template if:true={data}>
                    <!-- <lightning-input class="searchBar" label="Search" value={keyWord} onchange={handleKeywordChange}>
                        </lightning-input> -->
                    <lightning-datatable
                        key-field="Id"
                        data={data}
                        columns={columns}
                        onrowaction={handleRowActions}
                        hide-checkbox-column="true">
                    </lightning-datatable>
                </template> ```   

import { LightningElement, wire, track, api } from 'lwc';
import getOldValues from '@salesforce/apex/ValuesDataFeed.getOldValues';
import { refreshApex } from '@salesforce/apex';
import { NavigationMixin } from 'lightning/navigation';

const COLS = [
    {
        label: "Action",
        type: "button", 
        fixedWidth: 100, 
        typeAttributes: 
        { 
            label: 'View', 
            title: 'View', 
            name: 'viewDetails', 
            value:'viewDetails', 
            variant: 'brand', 
       }},
    {
        label:"Action",
        type: "button",
        fixedWidth: 100,
        typeAttributes: 
        {
            label: 'Edit',
            title: 'Edit',
            name: 'editDetails',
            value: 'editDetails',
            variant: 'brand',
        }},
    {label: 'Asset', fieldName:'AssetName', editable: false, sortable: true},
    {label: 'Asset Type', fieldName:'AssetType', editable: false, sortable: true},
    {label: 'Address', fieldName:'Asset__r.Address_Line1__c', editable: false, sortable: true},
    {label: 'Valuation Type', fieldName:'Agency_Edit_Valuation__c', type:'currency', editable: false, sortable: true},
    {label: 'Valuation', fieldName:'Agency_Edit_Valuation__c', editable: false, sortable: true},
    {label: 'Use', fieldName:'Agency_Edit_Use__c', editable: false, sortable: true},
    {label: 'Entity #', fieldName:'Entity', editable: false, sortable: true},
    {label: 'Remove', fieldName:'Agency_Edit_Removed__c', editable: false, sortable: true},
];

export default class ValuesAssetTable extends NavigationMixin(LightningElement) {
    @track data = [];
    @track columns = COLS;

    @wire(getOldValues)
            wiredAssets({ error, data }) {
                this.data = data;
                if(this.data) {
                    let preparedAssets = [];
                    this.data.forEach(asset => {
                        let preparedAsset = {};
                        preparedAssets.Id = asset.id;
                        preparedAssets.AssetName = asset.Asset__r.Name;
                        preparedAssets.AssetType = asset.Asset__r.Asset_Type__c;
                        preparedAssets.AssetAddress = asset.Asset__r.Address_Line1__c;
                        preparedAssets.AssetValuationType = asset.Agency_Edit_Valuation_Type__c;
                        preparedAssets.AssetValuation = asset.Agency_Edit_Valuation__c;
                        preparedAssets.AssetUse = asset.Agency_Edit_Use__c;
                        preparedAssets.Entity = asset.Agency_Edit_Their_1__c;
                        preparedAssets.Remove = asset.Agency_Edit_Removed__c;
                        preparedAssets.push(preparedAsset);
                        window.console.log(preparedAssets)
                    });
                    this.data = preparedAssets;
                    window.console.log(JSON.stringify(this.assets))
                } 
                else if (error) {
                    this.error = error;
                    this.data = undefined;
                 }
             }


3

You are almost on it.

While making your 'preparedAsset' object you are using the name of the list 'preparedAssets' instead of 'preparedAsset'(object variable name)

Replace your object creation code with below one:-

this.data.forEach(asset => {
                        let preparedAsset = {};
                        preparedAsset.Id = asset.id;
                        preparedAsset.AssetName = asset.Asset__r.Name;
                        preparedAsset.AssetType = asset.Asset__r.Asset_Type__c;
                        preparedAsset.AssetAddress = asset.Asset__r.Address_Line1__c;
                        preparedAsset.AssetValuationType = asset.Agency_Edit_Valuation_Type__c;
                        preparedAsset.AssetValuation = asset.Agency_Edit_Valuation__c;
                        preparedAsset.AssetUse = asset.Agency_Edit_Use__c;
                        preparedAsset.Entity = asset.Agency_Edit_Their_1__c;
                        preparedAsset.Remove = asset.Agency_Edit_Removed__c;
                        preparedAssets.push(preparedAsset);
                        window.console.log(preparedAssets)
                    });
1
  • Oh my, how embarrassing. I am grateful for your help. – bblackinton Jan 8 '20 at 18:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.