Another way for displaying contacts related to the accounts here https://www.codekiat.com/2020/04/how-to-pass-record-id-from-lightning-web-component-to-apex-controller.html
I just want to display contacts in account record page. For this I used @api recordId and used connectedCallback(). I am using this component in account record page. But related contacts are not displaying.
hey i have try this check it out html <template> <lightning-card> <lightning-datatable if:true={contacts} data={contacts} columns={cols} key-field="id"></lightning-datatable> </lightning-card> </template> js import { LightningElement,api, wire } from 'lwc'; import getcontact from '@salesforce/apex/account_contact.getcontact'; const Columns = [ {label: 'Contact Name', fieldName: 'Name'}, {label: 'Email', fieldName: 'Email'}, {label: 'Phone', fieldName: 'Phone'}, {label: 'Account Name', fieldName: 'Account_Name'},
]// colum for datatable //why wirev decorator? wire is recative in nature when ever their is change in org data it will automaticaaly reflect in lwc . export default class Releted_Contact extends LightningElement { @api recordId; // give reacordid of account contacts; error; cols=Columns; //if the Apex method takes a string parameter, don’t pass a string directly. Instead, pass an object that contains a property whose value is a string. @wire(getcontact,{val: '$recordId'})wiredContacts({ error, data }) { if (data) { this.contacts = data; this.error = undefined; // to show account name let currentData = []; data.forEach((row)=> { let rowData = {}; rowData.Name = row.Name; rowData.Email = row.Email; rowData.Phone = row.Phone; if (row.Account) { rowData.Account_Name = row.Account.Name; } currentData.push(rowData);
Please go through below link it has implementation similar to your use case.
https://www.w3web.net/display-account-related-contacts-in-lwc/
https://pritamshekhawat.wordpress.com/category/lightning-component/lightning-web-component/
If it helps please mark it as best answer.
Thanks!
>> https://salesforce.stackexchange.com/questions/298048/need-help-with-lwc-trying-to-create-datatable-to-show-related-contacts-with
The above link has an implementation of showing related contacts can you try checking it once?
Let me know if it helps you and close your query by marking it as the best answer so that it can help others in the future.
Thanks.
You can take reference from below code.
In this code I have displayed Accountand Account related Contact and Opportunity.
---apex class ------html -------.js If you find your Solution then mark this as the best answer.
Thank you!
Regards
Suraj Tripathi
Try Below Code Please Mark It As Best Answer If It Helps
Thank You!
https://www.codekiat.com/2020/04/how-to-pass-record-id-from-lightning-web-component-to-apex-controller.html
Naveen
I just want to display contacts in account record page. For this I used @api recordId and used connectedCallback().
I am using this component in account record page. But related contacts are not displaying.
html
<template>
<lightning-card>
<lightning-datatable if:true={contacts}
data={contacts} columns={cols} key-field="id"></lightning-datatable>
</lightning-card>
</template>
js
import { LightningElement,api, wire } from 'lwc';
import getcontact from '@salesforce/apex/account_contact.getcontact';
const Columns = [
{label: 'Contact Name', fieldName: 'Name'},
{label: 'Email', fieldName: 'Email'},
{label: 'Phone', fieldName: 'Phone'},
{label: 'Account Name', fieldName: 'Account_Name'},
]// colum for datatable
//why wirev decorator? wire is recative in nature when ever their is change in org data it will automaticaaly reflect in lwc .
export default class Releted_Contact extends LightningElement {
@api recordId; // give reacordid of account
contacts;
error;
cols=Columns; //if the Apex method takes a string parameter, don’t pass a string directly. Instead, pass an object that contains a property whose value is a string.
@wire(getcontact,{val: '$recordId'})wiredContacts({ error, data }) {
if (data) {
this.contacts = data;
this.error = undefined;
// to show account name
let currentData = [];
data.forEach((row)=> { let rowData = {};
rowData.Name = row.Name;
rowData.Email = row.Email;
rowData.Phone = row.Phone;
if (row.Account) {
rowData.Account_Name = row.Account.Name;
}
currentData.push(rowData);
});
this.contacts = currentData;
//
} else if (error) {
this.error = error;
this.contacts = undefined;
}
console.log(data);
console.log(error);
}
}
apex class
public class account_contact {
@AuraEnabled(cacheable=true)
public static List<Contact> getcontact(Id val) {
return [select id,Name,Email,Phone,Account.Name from Contact where accountid=:val limit 100 ];
}
}
o/p: each account you will list of related contact