0

Apex

public with sharing class LightningDataTable {

    @AuraEnabled(cacheable=true)
    public static list<providerEntity__c> getProviderList(string searchKey,string ProviderId) {
        string sTempSearchKey = '%' + searchKey + '%';

        // create contact list to store search result 
        list<providerEntity__c> lstContact = new list<providerEntity__c>();

        // query contact records
        for(providerEntity__c oCon : [Select Id, Name, Provider_Name_API__c, NPI__c, FedId__c, LicenceId__c, MedicareOscarNumber__c, (select Name, Site__c,  SLName__c,ProviderIdTxt__c,LOBName__c,PayToContractName__c from Service_Affiliation1__r )  From providerEntity__c WHERE (Provider_Name_API__c LIKE : sTempSearchKey OR NPI__c LIKE: sTempSearchKey  OR FedId__c LIKE: sTempSearchKey ) ]){
            lstContact.add(oCon);
        }

        //If there are no records found with searchKey then, throw aura exception with a message
        if(lstContact.size() == 0){
            throw new AuraHandledException('No Record Found..'); 
        }

        return lstContact;
    }

}

HTML

<template>
    <lightning-card title = "Search " icon-name = "custom:custom63">  
        <div class = "slds-m-around_medium">  
            <lightning-input type = "search" onchange = {handleKeyChange} class = "slds-m-bottom_small" label = "Search" >
            </lightning-input>  
            <template if:true = {providers}>

                <div style="height: 300px;">  
                    <lightning-datatable key-field="Id"
                                         data={providers}
                                         columns={columns}
                                         hide-checkbox-column="true"
                                         show-row-number-column="true"
                                         onrowaction={handleRowAction}>
                    </lightning-datatable>  
                </div>   
            </template>      
            <template if:true = {error}>  
                {error}>                  
            </template>  
        </div>  
    </lightning-card>
</template>

JS

import { LightningElement,track,api, wire } from 'lwc';
import getProviderList from '@salesforce/apex/LightningDataTable.getProviderList';
// import standard toast event 
import {ShowToastEvent} from 'lightning/platformShowToastEvent'; 
import { NavigationMixin } from 'lightning/navigation';
const columns = [
    {label: 'Name',fieldname: 'Provider_Name_API__c',type: 'Text;},
    {label: 'Id',fieldname: 'ServiceAffiliation1Name',type: 'Text'}, 
    {label: 'NPI',fieldname: 'NPI__c',type: 'Text'},
    {label: 'TIN',fieldname: 'FedId__c',type: 'Text'},
    {label: 'Site Id',fieldname: 'ServiceAffiliation1Site',type: 'Text'},
    {label: 'PPG',fieldname: 'ServiceAffiliation1SLName',type: 'Text'},
    {label: 'Address',fieldname: 'ServiceAffiliation1ProviderIdTxt',type: 'Text'},
    {label: 'LOB',fieldname: 'ServiceAffiliation1LOBName',type: 'Text'},
    {label: 'Contract Name',fieldname: 'ServiceAffiliation1PayToContractName',type: 'Text'},
    {label: 'License Id',fieldname: 'LicenceId__c',type: 'Text'},
    {label: 'Medicare Oscar Number',fieldname: 'MedicareOscarNumber__c',type: 'Text'}
];


export default class ProviderLightningTable extends  NavigationMixin(LightningElement) {
  
    @api recordId;
    @track providers; 
    @track error; 
    @track columns = columns; 
    searchKey = '';
    ProviderId;
 
    @wire(getProviderList, {
        searchKey: '$searchKey',
        ProviderId: 'recordId'
    })
    wiredclass(value){
        this.wiredActivities = value;
        const { data, error } = value;

        if (data) { 
            var ObjData = JSON.parse(JSON.stringify(data));

            ObjData.forEach(Record => {

                Record.ServiceAffiliation1Name =  Record.Service_Affiliation1__r.Name != undefined ? Record.Service_Affiliation1__r.Name : '';
                Record.ServiceAffiliation1Site =  Record.Service_Affiliation1__r.Site__c != undefined ? Record.Service_Affiliation1__r.Site__c : '';
                Record.ServiceAffiliation1SLName =  Record.Service_Affiliation1__r.SLName__c != undefined ? Record.Service_Affiliation1__r.SLName__c: '';
                Record.ServiceAffiliation1ProviderIdTxt =  Record.Service_Affiliation1__r.ProviderIdTxt__c != undefined ? Record.Service_Affiliation1__r.ProviderIdTxt__c: '';
                Record.ServiceAffiliation1LOBName =  Record.Service_Affiliation1__r.LOBName__c != undefined ? Record.Service_Affiliation1__r.LOBName__c: '';
                Record.ServiceAffiliation1PayToContractName =  Record.Service_Affiliation1__r.PayToContractName__c != undefined ? Record.Service_Affiliation1__r.PayToContractName__c: '';

            });

            this.providers = ObjData;
            this.error = undefined;
        } else if (error) {  
            this.error = error;  
            this.providers = undefined;
        }
    }

    handleKeyChange( event ) {
        this.searchKey = event.target.value;
    }
}
0

1 Answer 1

1

Record.Service_Affiliation1__r is a list, so in order to access to its elements you have to use brackets notation, i.e. Record.Service_Affiliation1__r[0].Name.
So if you only need the info of the first child, you can change your forEach to:

ObjData.forEach((record) => {
    if (record.Service_Affiliation__r && record.Service_Affiliation__r.length) {
        record.ServiceAffiliation1Name =  record.Service_Affiliation__r[0].Name;
        record.ServiceAffiliation1Site =  record.Service_Affiliation__r[0].Site__c;
        record.ServiceAffiliation1SLName =  record.Service_Affiliation__r[0].SLName__c;
        record.ServiceAffiliation1ProviderIdTxt =  record.Service_Affiliation__r[0].ProviderIdTxt__c;
        record.ServiceAffiliation1LOBName =  record.Service_Affiliation__r[0].LOBName__c;
        record.ServiceAffiliation1PayToContractName =  record.Service_Affiliation__r[0].PayToContractName__c;
    }
});

Keep in mind that A single providerEntity__c record may have multiple children, therefore Service_Affiliation1__r may have more than one element and if you have to show hierarchical data in a table, you might want to give a look at Tree Grid component.

5
  • @SattiRenukaSravanthi In JS the if should be if (record.Service_Affiliation1__r && record.Service_Affiliation1__r.length) {. About where assinging providerId in apex, you should update your question explaining what is the goal of that method: would you get from that query and what is the requirement.
    – RubenDG
    Commented Sep 26, 2021 at 12:46
  • Providerentity__c is parent, serviceaffiliation__c is child,service_affiliation1__c is childrelationship name..iwant tosearch parent obj fields on search bar where I need to display data of both parent and child in datatable. Providerid__c is the lookup field on child. Is there any need to assign providerid(ProviderId: 'recordId') in the apex.if yes where and to what..Thanks
    – Renuka
    Commented Sep 26, 2021 at 15:05
  • @SattiRenukaSravanthi please post the output of console.log(JSON.stringify(ObjData));. About providerId you need to use it if you want to fetch only records that are children of the specific Providerentity__c. If so, replace the whole WHERE clause with WHERE Id = :ProviderId
    – RubenDG
    Commented Sep 26, 2021 at 19:52
  • Objdata Proxy {} providerLightningTable.js:4 [{"Id":"a1Q0m000001uiuREAQ","Name":"984874","Provider_Name_API__c":"Grace","NPI__c":"9876543211","FedId__c":"123456","LicenceId__c":"87654","MedicareOscarNumber__c":"12345","Service_Affiliation__r":[{"ProviderId__c":"a1Q0m000001uiuREAQ","Id":"a1R0m000000KteZEAS","Name":"SA-1732680","LOBName__c":"tres"}],"ServiceAffiliation1Name":"SA-1732680","ServiceAffiliation1LOBName":"tres"}]
    – Renuka
    Commented Sep 27, 2021 at 5:00
  • @SattiRenukaSravanthi I updated the forEach, try it now.
    – RubenDG
    Commented Sep 27, 2021 at 7:15

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .