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
Abhishek Sharma 527Abhishek Sharma 527 

show account name in contact record in lwc

Hello there, I have created lwc for showing contact records but I want to show associated account name also but in result page it shows blank in account field
can anyone pls assist in this. my code as follows.
//html
<template>
     <h2> Contact Datatable</h2>
     <template if:true={conList}>
         <lightning-datatable data={conList} columns={columns} key-field="Id">
         </lightning-datatable>
     </template>
     <template if:true={error}>
         {error}
     </template>
 </template>

//js code
import { LightningElement ,api, wire, track} from 'lwc';
import getContactList from '@salesforce/apex/method.getContactList';
export default class contactRecord extends LightningElement {
   
         @track columns = [
          { label: 'Name', fieldName: 'Name' },
          { label: 'Id', fieldName: 'Id'},
            {label: 'Account', fieldName: 'Account.Name'},
            {label: 'Email', fieldName: 'Email'}
      ];
    @track conList;
    @track error;
    @wire(getContactList)
    wiredContacts({
        error,
        data
    }) {
        if (data) {
            this.conList = data;
        } else if (error) {
            this.error = error;
        }
    }
}
------------------------
// class code
public with sharing class method {
    @AuraEnabled(cacheable=true)
    public static List<Contact> getContactList() {
        return [SELECT Id, Name, Account.Name, email
            FROM Contact];
    }
}
--------------------------------------------------------
Best Answer chosen by Abhishek Sharma 527
mukesh guptamukesh gupta
Hi Abhishek,

Please use below code:-
 
import { LightningElement ,api, wire, track} from 'lwc';
import getContactList from '@salesforce/apex/method.getContactList';
export default class contactRecord extends LightningElement {
   
         @track columns = [
          { label: 'Name', fieldName: 'Name' },
          { label: 'Id', fieldName: 'Id'},
          {label: 'Account', fieldName: 'AccountName'},
          {label: 'Email', fieldName: 'Email'}
      ];
    @track conList;
    @track error;
    @wire(getContactList)
    wiredContacts({
        error,
        data
    }) {
        if (data) {
        let contactData = JSON.parse(JSON.stringify(data)); 
      contactData.forEach(record => { 
        if (record.AccountId) { 
          record.AccountName = record.Account.Name;
         } 
        }); 
      this.conList = contactData ;
        } else if (error) {
            this.error = error;
        }
    }
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh

All Answers

mukesh guptamukesh gupta
Hi Abhishek,

Please use below code:-
 
import { LightningElement ,api, wire, track} from 'lwc';
import getContactList from '@salesforce/apex/method.getContactList';
export default class contactRecord extends LightningElement {
   
         @track columns = [
          { label: 'Name', fieldName: 'Name' },
          { label: 'Id', fieldName: 'Id'},
          {label: 'Account', fieldName: 'AccountName'},
          {label: 'Email', fieldName: 'Email'}
      ];
    @track conList;
    @track error;
    @wire(getContactList)
    wiredContacts({
        error,
        data
    }) {
        if (data) {
        let contactData = JSON.parse(JSON.stringify(data)); 
      contactData.forEach(record => { 
        if (record.AccountId) { 
          record.AccountName = record.Account.Name;
         } 
        }); 
      this.conList = contactData ;
        } else if (error) {
            this.error = error;
        }
    }
}


if you need any assistanse, Please let me know!!

Kindly mark my solution as the best answer if it helps you.

Thanks
Mukesh
This was selected as the best answer
Abhishek Sharma 527Abhishek Sharma 527
Thanks Mukesh, it's working.