0

I am trying to display opportunity share records in lwc data table, I am getting blank rows even though when i execute the query in the workbench, records are being returned with values.

Code below:

import {LightningElement, track, wire} from 'lwc';
import getOppdata from '@salesforce/apex/LWCExampleController.retriveOpportunities';
const columns = [
    {
        label: 'User',
        fieldName: 'UserOrGroupName',
        type: 'text',
    },
     {
        label: 'AccessLevel',
        fieldName: 'OpportunityAccessLevel',
        type: 'text'
    }
]
export default class OpptyAccountLWC extends LightningElement {
    @track data = [];
    @track columns = columns;

    @wire(getOppdata)
    opp({error, data}) {
        if(data) {

            let currentData = [];

            data.forEach((row) => {

                let rowData = {};

                rowData.User = row.UserOrGroup.Name;
                rowData.AccessLevel = row.OpportunityAccessLevel;
                //window.alert('########## '+row.UserOrGroup.Name );
                //console.log('########### '+ row.UserOrGroup.Name);

                currentData.push(rowData);
            });

            this.data = currentData;
        }
        else if(error) {
            window.console.log(error);
        }
    }
}

HTML:

<template>
    <lightning-card title="OpptyShareRecords">
        <lightning-datatable columns={columns} 
                            data={data} 
                            key-field="id"
                            hide-checkbox-column="true"
                            show-row-number-column="true"></lightning-datatable>
    </lightning-card>
</template>

Controller class:

public class LWCExampleController {    
    @AuraEnabled(cacheable=true)
    public static list<OpportunityShare> retriveOpportunities() {
        return [SELECT OpportunityAccessLevel,UserOrGroup.Name
                FROM OpportunityShare];
    }
}
1
  • 2
    You can use the format code button {} to format your code properly.
    – sfdcfox
    Dec 1, 2021 at 4:25

1 Answer 1

1

Your fieldName properties are incorrect:

const columns = [
    {
        label: 'User',
        fieldName: 'User', // Was UserOrGroupName
        type: 'text',
    },
     {
        label: 'Access Level',
        fieldName: 'AccessLevel', // Was OpportunityAccessLevel
        type: 'text'
    }
]

The label is what's used for the header value, and fieldName is what's used for the data.

Also, you can use Array.prototype.map instead of pushing to an array:

this.data = data.map((row) => ({ User: row.UserOrGroup.Name, AccessLevel: row.OpportunityAccessLevel }));
2
  • const columns = [ { label: 'User', fieldName: 'User', type: 'text', }, { label: 'AccessLevel', fieldName: 'AccessLevel', type: 'text' } ] this.data = data.map((row) => ({ User: row.UserOrGroup.Name, AccessLevel : row.OpportunityAccessLevel})); } // I updated the code to above , I am now getting blank values in AccessLevel field
    – Raghu
    Dec 1, 2021 at 6:54
  • I update the width parameter in columns,that fixed the issue with second column being blank. Thanks a ton SFDCFOX for helping with the correct approach.
    – Raghu
    Dec 1, 2021 at 7:08

You must log in to answer this question.

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