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
Aditi Mohanty 5Aditi Mohanty 5 

data is not showing in lightning component

global with sharing class products {
    @AuraEnabled
    public static List<PricebookEntry> getproduct() {

   List<PricebookEntry> prod= [SELECT Product2.Name, Unitprice FROM PricebookEntry];
  return prod;

    }
}

component--
<aura:component controller="products" > 
<aura:attribute name="productname" type="PricebookEntry[]"/>
<aura:attribute name="cols" type="List"/>  
     
    <aura:handler name="init" value="{!this}" action="{!c.doinit}"/>
   
    <lightning:datatable
            data="{!v.productname}"
            columns="{!v.cols}"
            keyField="Id"
            onrowaction="{!c.onRowAction}"/>
   
       
</aura:component>

controller---
({
	doinit : function(component) {
       component.set( 'v.cols' , [
            {
                'label': 'Name',
                'fieldName': 'Name',
                'type': 'text'
            },
{label': 'Price',
                'fieldName': 'Price',
                'type': 'Currency'
},
            {
                'label': 'Action',
                'type': 'button',
                'typeAttributes': {
                    'label': 'Add to Cart',
                    'name': 'view_details'
                }
            },
            {
                'label': 'Action',
                'type': 'button',
                'typeAttributes': {
                    'label': 'Buy',
                    'name': 'view_details'
                }
            }
            ]);
        
  var action = component.get("c.getproduct"); 
        // var self = this;
        action.setCallback(this, function(actionResult) {
            var state = actionResult.getState();
        if (state === "SUCCESS") {
          component.set("v.productname", actionResult.getReturnValue());
            component.set("v.productprice",actionResult.getReturnValue());
        }
            });
          $A.enqueueAction(action);
    },
		
	
})

Name and price of the products is not showing in datatable, but soql is executing successfully. What to do?
Best Answer chosen by Aditi Mohanty 5
Dushyant SonwarDushyant Sonwar
Aditi,

I found two mistake in your code:
  1. You are using UnitPrice field in soql but you are binding wrong field name('Price')
  2. You are using parent field in lightning data table. Lightning Data table does not support parent fields . You need to create a column dynamically in the returned list.
I did some minor tweaks in controller js
({
	doinit : function(component) {
       component.set( 'v.cols' , [
            {
                'label': 'Name',
                'fieldName': 'Name',
                'type': 'text'
            },
			{'label': 'Price',
                'fieldName': 'UnitPrice',
                'type': 'Currency'
			},
            {
                'label': 'Action',
                'type': 'button',
                'typeAttributes': {
                    'label': 'Add to Cart',
                    'name': 'view_details'
                }
            },
            {
                'label': 'Action',
                'type': 'button',
                'typeAttributes': {
                    'label': 'Buy',
                    'name': 'view_details'
                }
            }
            ]);
        
  		var action = component.get("c.getproduct"); 
        // var self = this;
        action.setCallback(this, function(actionResult) {
            var state = actionResult.getState();
        if (state === "SUCCESS") {
          var records = actionResult.getReturnValue();
            for (var i = 0; i < records.length; i++) {
				 records[i].Name = records[i].Product2.Name;
            }
            component.set("v.productname", records);
            
            component.set("v.productprice",actionResult.getReturnValue());
        }
            });
          $A.enqueueAction(action);
    }
		
	
})

Let us know if you still cannot see the columns. Hope this helps!

All Answers

AnudeepAnudeep (Salesforce Developers) 
Can you please try removing the single quotes in the JSON for label, fieldName, type?
 
component.set( 'v.cols' , [
            {
                label: 'Name',
                fieldName: 'Name',
                type: 'text'
            },

Please see the example in the documentation for the exact syntax

Let me know if this helps, if it does, please mark this answer as best so that others facing the same issue will find this information useful. Thank you
Aditi Mohanty 5Aditi Mohanty 5
It is not working. The output is same, no data,
It is working for product2.obj not for pricebookEntry.
Dushyant SonwarDushyant Sonwar
Aditi,

I found two mistake in your code:
  1. You are using UnitPrice field in soql but you are binding wrong field name('Price')
  2. You are using parent field in lightning data table. Lightning Data table does not support parent fields . You need to create a column dynamically in the returned list.
I did some minor tweaks in controller js
({
	doinit : function(component) {
       component.set( 'v.cols' , [
            {
                'label': 'Name',
                'fieldName': 'Name',
                'type': 'text'
            },
			{'label': 'Price',
                'fieldName': 'UnitPrice',
                'type': 'Currency'
			},
            {
                'label': 'Action',
                'type': 'button',
                'typeAttributes': {
                    'label': 'Add to Cart',
                    'name': 'view_details'
                }
            },
            {
                'label': 'Action',
                'type': 'button',
                'typeAttributes': {
                    'label': 'Buy',
                    'name': 'view_details'
                }
            }
            ]);
        
  		var action = component.get("c.getproduct"); 
        // var self = this;
        action.setCallback(this, function(actionResult) {
            var state = actionResult.getState();
        if (state === "SUCCESS") {
          var records = actionResult.getReturnValue();
            for (var i = 0; i < records.length; i++) {
				 records[i].Name = records[i].Product2.Name;
            }
            component.set("v.productname", records);
            
            component.set("v.productprice",actionResult.getReturnValue());
        }
            });
          $A.enqueueAction(action);
    }
		
	
})

Let us know if you still cannot see the columns. Hope this helps!
This was selected as the best answer
Aditi Mohanty 5Aditi Mohanty 5
Thanks for the answer. Name of product is now showing but not price one. Can you help me with component part. i don't know how to add productprice variable in datatable in component part.