function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Girish Reddy 52Girish Reddy 52 

Hello, I wrote program for datatable but the records are not displaying on the UI. Please help me

Controller 
public with sharing class datatablecont {
    @AuraEnabled(cacheable=true)
    public static List<BMCServiceDesk__Incident__c> getInc(){
            return [select id,name from BMCServiceDesk__Incident__c ];    }
}


JS
import { LightningElement,wire,track } from 'lwc';
import getInc from '@salesforce/apex/datatablecont.getInc';
const colm = [
    {label:'Id', fieldName:'Id', type:'id'},
    {label:'Name', fieldName:'Name', type:'text'}
]
export default class Datatable extends LightningElement {
    @track col = colm;
    @wire(getInc) getIncident;
}

HTML
<template>
    <lightning-card title="Incident Tickets">
        <lightning-datatable key-field="Id" data={getIncident} columns={col}></lightning-datatable>
    </lightning-card>
</template>
Best Answer chosen by Girish Reddy 52
CharuDuttCharuDutt
Hii Girish Reddy
Try Below Code
<template>
        <lightning-datatable
                key-field="id"
                data={data}
                columns={columns}>
        </lightning-datatable>
        <div>{error}</div> 
</template>

#######################################################

import { LightningElement,wire } from 'lwc';
import getInc from '@salesforce/apex/datatablecont.getInc';
const columns = [{label: 'Id',fieldName: 'Id'},
{label: 'Name',fieldName: 'Name',type: 'text',sortable: true}];
export default class Datatable extends LightningElement {
    data = [];
    columns = columns;
    records = '';
    error;

    @wire(getInc,{
        }
    )
    wiredCases(value){
        this.wiredActivities = value;
        const { data, error } = value;
    
    if(data){
        let dataEditing = JSON.parse(JSON.stringify(data));
        this.data = dataEditing;
        
    }else if(error){
        this.error = error;
    }
    
}
}
Please Mark It As Best Answer If It Helps
Thank You!

All Answers

AnkaiahAnkaiah (Salesforce Developers) 
Hi Girish,

Refer the below link have solution for similar kind of ask and modify the logic as per your need.

https://developer.salesforce.com/forums/?id=9062I000000gBxIQAU

https://www.sfdckid.com/2021/07/how-to-fetch-data-in-lwc-salesforce.html

If this information helps, Please mark it as best answer.

Thanks!!
CharuDuttCharuDutt
Hii Girish Reddy
Try Below Code
<template>
        <lightning-datatable
                key-field="id"
                data={data}
                columns={columns}>
        </lightning-datatable>
        <div>{error}</div> 
</template>

#######################################################

import { LightningElement,wire } from 'lwc';
import getInc from '@salesforce/apex/datatablecont.getInc';
const columns = [{label: 'Id',fieldName: 'Id'},
{label: 'Name',fieldName: 'Name',type: 'text',sortable: true}];
export default class Datatable extends LightningElement {
    data = [];
    columns = columns;
    records = '';
    error;

    @wire(getInc,{
        }
    )
    wiredCases(value){
        this.wiredActivities = value;
        const { data, error } = value;
    
    if(data){
        let dataEditing = JSON.parse(JSON.stringify(data));
        this.data = dataEditing;
        
    }else if(error){
        this.error = error;
    }
    
}
}
Please Mark It As Best Answer If It Helps
Thank You!
This was selected as the best answer