0

I am trying to display list of related oportunities using a lightning datatable on account record page. I can see data is getting fetched in console, but nothing is shown in datatble.

Apex:

public with sharing class oppRelatedAcc {
   

    @AuraEnabled(cacheable = true)
    public static List<Opportunity> getOppRecords( String id ){
        try {
                return [Select Id,Name, StageName, Amount, AccountId from Opportunity where AccountId =:id]; 

            
        } catch (Exception e) {
            throw new AuraHandledException(e.getMessage());
        }
    }


}

HTML:

<template>
    <lightning-card title="Practise Datatable">

        <lightning-datatable
        
        key-field="Id"
        data={opps}
        columns={columns}>

           

        </lightning-datatable>


    </lightning-card>
</template>

JS:

import { LightningElement, wire, track, api } from 'lwc';
import getOppRecords from '@salesforce/apex/oppRelatedAcc.getOppRecords'



const columns=
[

    {label: "Opp Name", fieldName: "Name"},
    {label: "Stage Name", fieldName: "StageName"},
    {label: "Amount", fieldName:"Amount"}

];

export default class Datatable_relatedOpp extends LightningElement {

    opps=[];
    @api recordId;

    @wire(getOppRecords, {id: '$recordId'}) 
    wiredOpps({data, error})
    {
        if(data)
        {
            this.opps= data;
            console.log(data);
            console.log(this.opps)
        }
    }

}

1 Answer 1

1

You have the columns defined as a const in the JS file, but the view of the component is referencing a variable that should be defined within the controller class.

import { LightningElement, wire, track, api } from 'lwc';
import getOppRecords from '@salesforce/apex/oppRelatedAcc.getOppRecords';

const columns = [
  { label: 'Opp Name', fieldName: 'Name' },
  { label: 'Stage Name', fieldName: 'StageName' },
  { label: 'Amount', fieldName: 'Amount' },
];

export default class Datatable_relatedOpp extends LightningElement {
  opps = [];
  columns = columns;
  @api recordId;

  @wire(getOppRecords, { id: '$recordId' })
  wiredOpps({ data, error }) {
    if (data) {
      this.opps = data;
    }
  }
}

You must log in to answer this question.

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