[SalesForce] How do we pass the field api name in lwc for a lightning data table

I have fetched all the fields to be displayed on the datatable in my apex query.

In my js file where i store the column details

@track columns = [{
        label: 'Deal Name',
        fieldName: 'Marketing_Activity__r.Name',
        type: 'text'            
    },
    {
        label: 'Deal Type',
        fieldName: 'Deal_Type__c',
        type: 'text'

    },
    {
        label: 'Rent',
        fieldName : 'Rent__c',
        type: 'currency'

    },
    {
        label: 'Term',
        fieldName : 'Term_Mos__c',
        type: 'number'
    },
    {
        label: 'Security Deposit',
        fieldName: 'Aircraft__r.Lease__r.Security_Deposit_Amount_Cash_or_LOC__c',
        type: 'currency'

    }

It fetches value for rent,term and deal_type. but not for Security deposit and Deal Name.
Any suggestions???
How to display the above details on the datatable in lwc?

Best Answer

You can't traverse relationships in the fieldName attribute. Instead, you would need to map the data beforehand, then use that field's name.

@track error;
@track data;

@wire(methodName, { /* params here */ })
  wireDealInfo({ error, data }) {
    if(data) {
      this.data = data
        .map(row => { ...row, dealName: row.Marketing_Activity__r.Name })
        .map(row => { ...row, securityDeposit: row.Aircraft__r.Lease__r.Security_Deposit_Amount_Cash_or_LOC__c });
      this.error = null;
    }
    if(error) {
      this.error = error;
      this.data = [];
    }
  }

Then, for your data table columns:

@track columns = [{
    label: 'Deal Name',
    fieldName: 'dealName',
    type: 'text'            
},
// ... other rows the same
{
    label: 'Security Deposit',
    fieldName: 'securityDeposit',
    type: 'currency'
}];
Related Topic