function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Akshay SethiaAkshay Sethia 

LWC: Lightning DataTable Lookup Issue

Hello Folks, im new to the salesforce org, and i was working on a project. I have completed the whole project but i am just stuck at one point. The situation is as follows:
  • There are 2 objects(Custom), lets consider them as Object A and Object B. 
  • I have a lookup relationship on Object A from Object B.
  • Consider i have a text field in Object A Named "NamedField__c".
  • I have made LWC for the Object B to display all the columns using Lightning DataTable.
But the main issue i am facing is that, Lightning data table only shows me the id of the lookup value, but not the "NamedField__c", which im trying to show.

Note: I have used Apex class to fetch the data and in that i am able to retive the values to the front end. (Check using console.log).
Also, i have tried using all combos of ObjectA__r.NamedField__c, ObjectA__c.NamedField__c and all. I had no reults when i did so.
I also ran the SOQL query in anonymous window and the results were as i expected.

Please help me out, over here in how to display the ObjectA -> NamedField__c field in the lightning datatable of Salesforce.

Thanks In advance, help would be much appriciated .
 
Danish HodaDanish Hoda
Hi Akshay,
Please check the columns variable you are using for the lightning-dataTable, it should contain ObjectA__r.NamedField__c.
If still it doesn't show up, then you need to send the data to LWC as wrapper class with all the fields.
robert wick 1robert wick 1
I appreciate your hard work. Keep posting new updates with us. This is really a wonderful post. Nice Blog Very interesting and useful information on your website. Thanks for sharing the blog and this great information which is definitely going to help us.
office.com/setup (http://www.officesetup.help)
office.com/setup (http://w-ww-office.com/setup)
mcafee.com/activate (http://www.help-mcafee.me)
mcafee.com/activate (http://w-w-w-mcafee.com/activate)
Akshay SethiaAkshay Sethia
Hey Danish Hoda,
Js Code Snippet - 
const columns = [
    { label: 'Voucher Id', fieldName: 'Name' },
    { label: 'Voucher Name', fieldName: 'Voucher_Name__c' },
    { label: 'Voucher Cost', fieldName: 'Voucher_Cost__c', type: 'currency', typeAttributes: { currencyCode: 'INR' }, cellAttributes: { alignment: 'left' } },
    { label: 'Voucher Validity', fieldName: 'Validity__c' },
    { label: 'Active', fieldName: 'Active__c' },
    { label: 'Certification Name', fieldName: 'Certification__r.Cert_Name__c', type: 'text' },
    { label: 'Voucher Comments', fieldName: 'Comments__c' },
    {
        type: 'action',
        typeAttributes: {
            rowActions: actions,
        }
    }
];

@wire(getVoucherList)
    Voucher__c(result) {
        this.refreshTable = result;
        if (result.data) {
            this.vouchers = result.data;
            this.error = undefined;
        } else if (result.error) {
            this.vouchers = undefined;
            this.error = result.error;
        }
        if (this.error != undefined) {
            console.log(this.error);
        } else {
            console.log(result.data);
        }
    }

HTML code snippet - 
<template if:true={vouchers}>
                    <lightning-datatable key-field="Id" data={vouchers} columns={columns} hide-checkbox-column="true"
                        onrowaction={handleRowActions}>
                    </lightning-datatable>
                </template>

CLass snippet - 
@AuraEnabled(Cacheable=true)
    public static List<Voucher__c> vouData(){
        return [ SELECT Name, Active__c, Certification__r.Cert_Name__c, Comments__c, Id, Validity__c, Voucher_Cost__c, Voucher_Name__c FROM Voucher__c ORDER BY Id ASC NULLS FIRST LIMIT 10 ];
    }

This is my code Danish, could you help by explaining how to use wrapper classes here?
Danish HodaDanish Hoda
Hi Akshay,
You can refer below sample code:
@AuraEnabled(Cacheable=true)
    public static List<wrpperData> vouData(){
      List<wrpperData> wrppersData = new List<wrpperData>();
     for(Voucher__c voucher = [ SELECT Name, Active__c, Certification__r.Cert_Name__c, Comments__c, Id,                           Validity__c, Voucher_Cost__c, Voucher_Name__c FROM Voucher__c ORDER BY Id ASC NULLS FIRST LIMIT 10 ]){
           wrpperData wrp = new wrpperData();
           wrp.Name = voucher.Name;
           //Assign all the elements
           wrppersData.add(wrp);
}
     return wrppersData;
    }

//create an inner class as wrapper class - wrpperData
public class wrpperData{
   @AuraEnabled
   public String Name
   @AuraEnabled
   public String VoucherName
   @AuraEnabled
   public String VoucherCost
   @AuraEnabled
   public String VoucherValidity
   @AuraEnabled
   public String CertificationName
   @AuraEnabled
   public String VoucherComments
}

//Replace the columns in the JS with wrpperData variables