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
Yuki TanYuki Tan 

my datatable rows show null

Hi, i write a code about datatable and apex, but  the result is null.
I have a custome object called Warehouse.It have a relationship field, related to Product object. And a number filed called Amount.
 want to show they in datatable. Now page show null but amount of records is right. And i try to show Name,Type__c , they will show. Just Product__r.Name and Amount__c is null.
Im sure that my SOQL is right.because i run it in develope consle query editor.

This is my code.
cmp:
<aura:handler name="init" value="{!this}" action="{!c.doInit}" />
	<aura:attribute name="columns" type="List"/>
	<aura:attribute name="data" type="Object"/>
	<aura:attribute name="errors" type="Object" default="[]"/>

	<lightning:card title="Pending QA">
		<lightning:datatable
			columns="{! v.columns }"
			data="{! v.data }"
			keyField="Id"
			errors="{! v.errors}"
			hideCheckboxColumn="true"/>
	</lightning:card>
controller:
doInit : function(component, event, helper) {
    	component.set('v.columns', [
            { label: 'Product', fieldName: 'Product__r.Name', type: 'text',editable:false},
            { label: 'Amount', fieldName: 'Amount__c', type: 'text',editable:false}]);
    	helper.getPendingRecord(component,event,helper);
    }
Helper:
getPendingRecord : function(component, event, helper) {
		var action = component.get("c.query");
        // call back
        action.setCallback(this, function(response){
            var state = response.getState();
            // call back success
            if (state == "SUCCESS"){
                var result = response.getReturnValue();
                component.set('v.data', result);
            }
            // call back failed
            else {
                var toastEvent=$A.get("e.force:showToast");
                toastEvent.setParams({
                    "title":'Error!',
                    "message":'Get Record Failed!',
                    "duration":5000,
                    "type":ERROR
                });
                toastEvent.fire();
            }
        });
        $A.enqueueAction(action);
	}
class:
public static List<Warehouse__c> query() {
    	List<Warehouse__c> pending = new List<Warehouse__c>();
        pending = [SELECT Product__r.Name,Amount__c FROM Warehouse__c WHERE Type__c = 'pendingArea'];
        return pending;
    }
Pls help me. Thanks!


 
Best Answer chosen by Yuki Tan
Maharajan CMaharajan C
HI Yuki,

Lightning datatable will not support the display of parent object field data's in Table . Simply Relationship Queries will not work directly in Lightning datatable. So you can only display the  Warehouse__c object fields. Display the Product__C  field will work but Product__r.Name.

This is in Ideas : https://success.salesforce.com/ideaView?id=0873A000000EA7VQAW  

SO we have some workaround for this:

1. By Using Flattener in JS Controller:
https://developer.salesforce.com/forums/?id=9062I000000IJzvQAG
https://developer.salesforce.com/forums/?id=9062I000000XtwnQAC
https://iwritecrappycode.wordpress.com/2017/11/22/salesforce-lightning-datatable-query-flattener/
https://www.wissel.net/blog/2018/08/lightning-datatables-and-relationship-queries.html

2. Using the Wrapper Class in Apex controller instead of normal Sobject List return:
https://developer.salesforce.com/forums/?id=9060G0000005V95QAE

Thanks,
Maharajan.C

 

All Answers

Yuki TanYuki Tan
I try to change amout columns type to numer. It show now.
But the product name still is null.
The product filed of Warehouse__c is custome filed. And the product object is a standard object. Name filed of product obj is also a standard field.
Maharajan CMaharajan C
HI Yuki,

Lightning datatable will not support the display of parent object field data's in Table . Simply Relationship Queries will not work directly in Lightning datatable. So you can only display the  Warehouse__c object fields. Display the Product__C  field will work but Product__r.Name.

This is in Ideas : https://success.salesforce.com/ideaView?id=0873A000000EA7VQAW  

SO we have some workaround for this:

1. By Using Flattener in JS Controller:
https://developer.salesforce.com/forums/?id=9062I000000IJzvQAG
https://developer.salesforce.com/forums/?id=9062I000000XtwnQAC
https://iwritecrappycode.wordpress.com/2017/11/22/salesforce-lightning-datatable-query-flattener/
https://www.wissel.net/blog/2018/08/lightning-datatables-and-relationship-queries.html

2. Using the Wrapper Class in Apex controller instead of normal Sobject List return:
https://developer.salesforce.com/forums/?id=9060G0000005V95QAE

Thanks,
Maharajan.C

 
This was selected as the best answer
Yuki TanYuki Tan
hi, Tks very much. It help me solve the problem. Can i show the ralation url in the datatable? I mean that i click the product name then jump to the which product page. Can i do that? pls~