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
Raghu.2020Raghu.2020 

Lightning datatable not displaying

Aura Component
 
<aura:component controller="objSearchController" implements="force:appHostable,flexipage:availableForAllPageTypes" access="global">
    
    <aura:attribute name="objName" type="String" />
    <aura:attribute name="datacolumns" type="List"/>
    <aura:attribute name="showTable" type="Boolean" default="false"/>
    <aura:attribute name="fieldsList" type="objSearchController.Result[]" />
    
    <lightning:card title="Object Search">
        <div class="slds-m-around_medium">
            <lightning:input name="objSearch" label="Enter Object Name: " value="{!v.objName}"/><br/>
            <lightning:button variant="brand-outline" label="Get Fields" onclick="{!c.getFields}"/>
        </div>
    </lightning:card>

    <aura:if isTrue="{!v.showTable}">
        <lightning:datatable data="{!v.fieldsList}" columns="{!v.dataColumns}" keyField="Sno"/>
    </aura:if>
</aura:component>
Controller:
 
({
    getFields : function(component, event, helper) {
        var objName = component.get("v.objName");
        var action = component.get("c.getObjFields");
        component.set('v.datacolumns', [
                {label: 'Field Name', fieldName: 'fieldName', type: 'text'},
                {label: 'Field Type', fieldName: 'fieldType', type: 'text'},
            ]);
        action.setParams({ "objName" : objName });

        action.setCallback(this, function(response) {
            var state = response.getState();
            var fields = response.getReturnValue();
            console.log(fields);
            if (state === "SUCCESS") {
                component.set("v.showTable",true);
                component.set("v.fieldsList",fields);
            }
            else {
                console.log(state);
            }
        });

          $A.enqueueAction(action);
    }
})

Apex Controller:
 
public with sharing class objSearchController {
    
    public objSearchController() {

    }

    @AuraEnabled
    public static List<Result> getObjFields(String objName){
        
        Integer count = 0;
        List<Result> resList = new List<Result>();
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        Schema.SObjectType leadSchema = schemaMap.get(objName);
        Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();

        for (String fieldName: fieldMap.keySet()) {
            String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();
            Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
            String fieldType = String.valueOf(fielddataType);

            count = count+1;

            Result res = new Result();
            res.Sno = count;
            res.fieldName = fieldLabel;
            res.fieldType = fieldType;
            resList.add(res);
        }

        return resList;
    }

    public class Result{
        
        @AuraEnabled public Integer Sno;
        @AuraEnabled public String fieldName;
        @AuraEnabled public String fieldType;
        
        public Result()
        {
            fieldName = '';
            fieldType = '';
        }
    }
}

​​​​​​​
 
Best Answer chosen by Raghu.2020
Maharajan CMaharajan C
Hi Raghu,

There is 2 problems in your code:

1. In Apex Class i have updated the below bold lines:
 
public with sharing class objSearchController {
    
    public objSearchController() {
        
    }
    
    @AuraEnabled
    public static List<Result> getObjFields(String objName){
        
        Integer count = 0;
        List<Result> resList = new List<Result>();
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        //Schema.SObjectType leadSchema = schemaMap.get(objName);
        //Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
        
        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(objName).getDescribe().fields.getMap();

        for (String fieldName: fieldMap.keySet()) {
            String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();
            Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
            String fieldType = String.valueOf(fielddataType);
            
            count = count+1;
            
            Result res = new Result();
            res.Sno = count;
            res.fieldName = fieldLabel;
            res.fieldType = fieldType;
            resList.add(res);
        }
        system.debug('resList--> ' + resList);
        return resList;
    }
    
    public class Result{
        
        @AuraEnabled public Integer Sno;
        @AuraEnabled public String fieldName;
        @AuraEnabled public String fieldType;
        
        public Result()
        {
            fieldName = '';
            fieldType = '';
        }
    }
}

2. In Aura Component: ( Case Sensitive issue )

You have decalared the Column attribute with small letters  <aura:attribute name="datacolumns" type="List"/>. But inside lightning:Datatable tag you have used the captial letter. <lightning:datatable data="{!v.fieldsList}" columns="{!v.dataColumns}" keyField="Sno"/>. So use the attributes name properly while referring. It should same how it's declared.

datacolumns ->  dataColumns == Case Sensitive issue here. 

 
<aura:component controller="objSearchController" implements="force:appHostable,flexipage:availableForAllPageTypes" access="global">
    
    <aura:attribute name="objName" type="String" />
    <aura:attribute name="datacolumns" type="List"/>
    <aura:attribute name="showTable" type="Boolean" default="false"/>
    <aura:attribute name="fieldsList" type="objSearchController.Result[]" /> 



    <lightning:card title="Object Search">
        <div class="slds-m-around_medium">
            <lightning:input name="objSearch" label="Enter Object Name: " value="{!v.objName}"/><br/>
            <lightning:button variant="brand-outline" label="Get Fields" onclick="{!c.getFields}"/>
        </div>
    </lightning:card>
    
    <aura:if isTrue="{!v.showTable}">
        <lightning:datatable data="{!v.fieldsList}" columns="{!v.datacolumns}" keyField="Sno"/>
    </aura:if>
</aura:component>

Thanks,
Maharajan.C

All Answers

ANUTEJANUTEJ (Salesforce Developers) 
Hi Raghu,

Can you try checking if you are getting the necessary records from the controller?

Also, can you check if the soql is returning proper records?

Additionally, I am adding a link for implementation that you can try checking : https://www.sfdcpoint.com/salesforce/lightning-datatable-example-salesforce/

Let me know if it helps you and close your query by marking it as solved so that it can help others in the future.  

Thanks.
Raghu.2020Raghu.2020
Hi Anutej,

I'm getting the required records from controller and I also tried the link. Still the datatable is not visible.
Maharajan CMaharajan C
Hi Raghu,

There is 2 problems in your code:

1. In Apex Class i have updated the below bold lines:
 
public with sharing class objSearchController {
    
    public objSearchController() {
        
    }
    
    @AuraEnabled
    public static List<Result> getObjFields(String objName){
        
        Integer count = 0;
        List<Result> resList = new List<Result>();
        Map<String, Schema.SObjectType> schemaMap = Schema.getGlobalDescribe();
        //Schema.SObjectType leadSchema = schemaMap.get(objName);
        //Map<String, Schema.SObjectField> fieldMap = leadSchema.getDescribe().fields.getMap();
        
        Map <String, Schema.SObjectField> fieldMap = schemaMap.get(objName).getDescribe().fields.getMap();

        for (String fieldName: fieldMap.keySet()) {
            String fieldLabel = fieldMap.get(fieldName).getDescribe().getLabel();
            Schema.DisplayType fielddataType = fieldMap.get(fieldName).getDescribe().getType();
            String fieldType = String.valueOf(fielddataType);
            
            count = count+1;
            
            Result res = new Result();
            res.Sno = count;
            res.fieldName = fieldLabel;
            res.fieldType = fieldType;
            resList.add(res);
        }
        system.debug('resList--> ' + resList);
        return resList;
    }
    
    public class Result{
        
        @AuraEnabled public Integer Sno;
        @AuraEnabled public String fieldName;
        @AuraEnabled public String fieldType;
        
        public Result()
        {
            fieldName = '';
            fieldType = '';
        }
    }
}

2. In Aura Component: ( Case Sensitive issue )

You have decalared the Column attribute with small letters  <aura:attribute name="datacolumns" type="List"/>. But inside lightning:Datatable tag you have used the captial letter. <lightning:datatable data="{!v.fieldsList}" columns="{!v.dataColumns}" keyField="Sno"/>. So use the attributes name properly while referring. It should same how it's declared.

datacolumns ->  dataColumns == Case Sensitive issue here. 

 
<aura:component controller="objSearchController" implements="force:appHostable,flexipage:availableForAllPageTypes" access="global">
    
    <aura:attribute name="objName" type="String" />
    <aura:attribute name="datacolumns" type="List"/>
    <aura:attribute name="showTable" type="Boolean" default="false"/>
    <aura:attribute name="fieldsList" type="objSearchController.Result[]" /> 



    <lightning:card title="Object Search">
        <div class="slds-m-around_medium">
            <lightning:input name="objSearch" label="Enter Object Name: " value="{!v.objName}"/><br/>
            <lightning:button variant="brand-outline" label="Get Fields" onclick="{!c.getFields}"/>
        </div>
    </lightning:card>
    
    <aura:if isTrue="{!v.showTable}">
        <lightning:datatable data="{!v.fieldsList}" columns="{!v.datacolumns}" keyField="Sno"/>
    </aura:if>
</aura:component>

Thanks,
Maharajan.C
This was selected as the best answer
Raghu.2020Raghu.2020
Hi Maharajan, 

Thank you so much. This resolved my issue.