1

I am working on a lightning web component where i am trying to parse comma separated values(field names) entered in the Columns and add it to an array.but columns givies undefined value.

HTML files:

<template>
    <lightning-card title="Contact Related List">
        <lightning-datatable data={objectField} columns={columns} key-field="Id">
        </lightning-datatable>
    </lightning-card>
</template>

Javascript:

import { LightningElement, track, wire, api } from 'lwc';
import getContactsRelatedToAccount from 
'@salesforce/apex/RelatedListCon.getContactsRelatedToAccount';
//import { getObjectInfo } from 'lightning/uiObjectInfoApi';

export default class AccountRelatedContacts extends LightningElement {
    @api fieldsName;
    // @api objectApiName;
    @track objectField;
    @api testDemo;
    @api recordId;
    @track tableData;
    @track columns;
    @track fieldArray=[]; 
    
    connectedCallback() {
        var columnTest=this.fieldsName;
        //window.console.log(columnTest);

        if(columnTest !== null && typeof columnTest !== undefined && columnTest !== ''){
            this.columns = columnTest.split(",");
        }
    }

    @api get objectApiName() {
        return this._objectApiName;
    }
    
    set objectApiName(value) {
        this.setAttribute('objectApiName', value);
        this._objectApiName = '';
        console.log('ABC');
        console.log(this.columns);
    }
    // @wire(getObjectInfo, { objectApiName: '$objectApiName' })
    // objectinfo;

//     eldListArray;
//     @api set fieldsName(value) {
//   this.fieldListArray = (value||'').split(',');
// }
// get fieldsName() {
//   return (this.fieldListArray||[]).join(',');
// }

    @wire(getContactsRelatedToAccount , { accId: '$recordId', test: '$testDemo', listFields: '$fieldsName' })
    wiredRecordsAMethod({ error, data }) {
        if (data != null) {
            this.objectField  = data;
            this.error = undefined;
        } 
        else if (error) {
            this.error = error;
            this.objectField  = undefined;
        }
    }
}

XML:

<?xml version="1.0" encoding="UTF-8"?>
<LightningComponentBundle xmlns="http://soap.sforce.com/2006/04/metadata">
    <apiVersion>52.0</apiVersion>
    <isExposed>true</isExposed>
    <targets>
        <target>lightning__AppPage</target>
        <target>lightning__RecordPage</target>
        <target>lightning__HomePage</target>
      
    </targets>
    <targetConfigs>
        <targetConfig targets="lightning__RecordPage,lightning__AppPage,lightning__HomePage">
            <property
            name="objectApiName"
            type="String"
            label="Object Name"
            description="Automatically bind the page's object name to the component variable"
            default=" " />
            <property name="testDemo" type="String" />
            <property
            name="fieldsName"
            type="String"
             />
        </targetConfig>
    </targetConfigs>
</LightningComponentBundle>
1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.