You need to sign in to do that
Don't have an account?
ApexDev
LWC Datatable - not showing related records
I try to build my own LWC Datatable component on Work Order object - which will show related Work Order Line Items records. The table is empty. How to retrieve the records into my LWC?
Here is my code:
woliDatatableDemo.html
woliDatatableDemo.js
woliControllerDatatable.cls
Here is my code:
woliDatatableDemo.html
<template> <lightning-card title="NEW Work Order Line Item" icon-name="custom:custom63"> <div style="width: auto;"> <lightning-datatable key-field="Id" data={data} columns={columns} ></lightning-datatable> </div> </lightning-card> </template>
woliDatatableDemo.js
import { LightningElement, api, track, wire } from "lwc"; import getWOLIs from '@salesforce/apex/woliControllerDatatable.getWOLIs'; const columns = [ { label: 'Device', fieldName: 'AssetId' }, { label: 'Serial No.', fieldName: 'Serial_No__c'}, { label: 'Product', fieldName: 'PricebookEntryId'}, { label: 'Quantity', fieldName: 'Quantity'}, { label: 'List Price', fieldName: 'ListPrice'}, { label: 'Discount', fieldName: 'Discount'}, { label: 'Total Price', fieldName: 'TotalPrice'}, ]; export default class WoliDatatableDemo extends LightningElement { @api recordId; @track data = []; @track columns = columns; @wire(getWOLIs, { woid: "$recordId" }) wiredRecordsMethod({ data, error }) { if (data) { let result = JSON.parse(JSON.stringify(data)); this.data = result.map(function(item) { return item; }) this.error = undefined; } else if (error) { this.error = error; this.data = undefined; } } }
woliControllerDatatable.cls
public with sharing class woliControllerDatatable { @AuraEnabled(cacheable=true) public static List<WorkOrderLineItem> getWOLIs(String woId) { return [ SELECT Quantity, PricebookEntryId, Serial_No__c, TotalPrice, WorkOrder.WorkOrderNumber, Work_Order_No__c, Duration, ListPrice, Discount, WorkOrderId, Parent_WOLI__c, AssetId FROM WorkOrderLineItem WHERE Parent_WOLI__c = false AND WorkOrderId = :woId WITH SECURITY_ENFORCED ];} }
Refer the below link have solution for the similar kind of ask and you need to modify the code as per your need.
https://www.sfdckid.com/2019/08/salesforce-lightning-component-to-display-contacts.html
If this helps, Please mark it as best answer.
Thanks!!
<template if:true=data>
<lightning-datatable key-field="Id" data={data} columns={columns} ></lightning-datatable> </div>
</template>
let me know if it does not work for you.
Thanks,
Vinayak Karande
<template if:true=data>
inside this use dataTable
</template>
and pass condition in js --
initialize data is false.
if data is coming then data= true;