Skip to main content
4 respuestas
  1. 6 abr 2022, 5:55
    Hii Shekhar

    Try Below Code

    public with sharing class ContactHelper {

    @AuraEnabled(cacheable=true)

    public static List<Contact> getContactList() {

    return [SELECT Id, FirstName, LastName, Account.Name, AccountId, email

    FROM Contact];

    }

    }

    <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>

    import { LightningElement ,api, wire, track} from 'lwc';

    import getAccountList from '@salesforce/apex/AccountHelper.getContactList';

    export default class ContactDatatableLWCExample extends LightningElement {

    @track columns = [{

    label: 'First Name',

    fieldName: 'FirstName',

    type: 'text',

    sortable: true

    },

    {

    label: 'Last Name',

    fieldName: 'LastName',

    type: 'text',

    sortable: true

    },

    { label: "Account Name", fieldName: "recordLink", type: "url",

    typeAttributes: {label: {fieldName: "AccountName"}, tooltip: "Name", target: "_blank", linkify: true} },

    {

    label: 'Phone',

    fieldName: 'Phone',

    type: 'phone',

    sortable: true

    },

    {

    label: 'Email',

    fieldName: 'Email',

    type: 'url',

    sortable: true

    }

    ];

    @track error;

    @track conList ;

    @wire(getContactList)

    wiredContacts({

    error,

    data

    }) {

    if (data) {

    let contactData = JSON.parse(JSON.stringify(data));

    contactData.forEach(record => {

    if (record.AccountId) {

    record.recordLink = "/" + record.AccountId;

    record.AccountName = record.Account.Name;

    }

    });

    this.conList = contactData ;

    } else if (error) {

    this.error = error;

    }

    }

    }

    Please Mark It As Best Answer If It Helps

    Thank You!