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> -->
                </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, 
            label: 'View', 
            title: 'View', 
            name: 'viewDetails', 
            variant: 'brand', 
        type: "button",
        fixedWidth: 100,
            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;

            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;
                    this.data = preparedAssets;
                else if (error) {
                    this.error = error;
                    this.data = undefined;

1 Answer 1


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;
  • Oh my, how embarrassing. I am grateful for your help. Jan 8, 2020 at 18:30

You must log in to answer this question.

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