You need to sign in to do that
Don't have an account?
Base Line
problem with the table in the component I made
Hi. I created a component which when dropped onto the opportunity screen, it is supposed to show a summary of the total value of all the added products. I checked the logs and the relevant data is sent back from the server but for some reason it is not showing on my table. If you can please take a look at my code and advise me on what is wrong, I'd highly appreciate it.Here it is:
COMPONENT
<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="OppProductsTotalValueController">
<aura:attribute name="oppProducts" type="OpportunityLineItem[]"/>
<aura:attribute name="displayText" type="String" default="" description="for displaying a message when the opportunity has no products added"/>
<aura:attribute name="tableColumns" type="List"/>
<aura:attribute name="headerTitle" type="Aura.Component[]">
<b>Total Value of Added Products</b>
</aura:attribute>
<aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
<lightning:card>
<aura:set attribute="title">{!v.headerTitle}</aura:set>
<p class="slds-card__body_inner">
<aura:text value="{! v.displayText }"/>
</p>
<lightning:datatable data="{!v.oppProducts}" columns="{!v.tableColumns}" keyField="id" hideCheckboxColumn="true"/>
</lightning:card>
</aura:component>
CONTROLLER
({
doInit: function(component, event, helper) {
//Get opportunity products
var action = component.get("c.getOppProducts");
action.setParams({"oppId": component.get("v.recordId")});
// Pass the data to my attribute and create table
action.setCallback(this, function(response){
var state = response.getState();
if(state === "SUCCESS"){
if(response.getReturnValue().length == 0)
{
component.set("v.displayText","This opportunity has no products added to it.");
}
else
{
helper.createTable(component);
component.set("v.oppProducts", response.getReturnValue());
//For checking the data passed back from the server
console.log(JSON.parse(JSON.stringify(component.get("v.oppProducts"))));
}
}
else{
console.log('Problem getting Opportunity Products, response state: ' + state);
}
});
$A.enqueueAction(action);
}
})
HELPER
({
createTable : function(component) {
component.set('v.tableColumns', [
{label:'Product Code', fieldName:'prodCode', type:'text'},
{label:'Total Value', fieldName:'totalVal', type:'currency'}
]);
}
})
APEX CLASS
public class OppProductsTotalValueController {
@AuraEnabled
public static OpportunityLineItem[] getOppProducts(Id oppId)
{
// Check to make sure relevant fields are accessible to this user
String[] fieldsToCheck = new String[] {
'ProductCode', 'TotalPrice'
};
Map<String,Schema.SObjectField> fieldDescribeTokens =
Schema.SObjectType.OpportunityLineItem.fields.getMap();
for(String field : fieldsToCheck) {
if( ! fieldDescribeTokens.get(field).getDescribe().isAccessible()) {
throw new System.NoAccessException();
}
}
//Check for any products which have been added
OpportunityLineItem[] returnedOppProds;
returnedOppProds = [SELECT ProductCode, TotalPrice FROM OpportunityLineItem WHERE (OpportunityId = :oppId AND TotalPrice > 0)];
return returnedOppProds;
}
}
Thanks in advance!
COMPONENT
<aura:component implements="flexipage:availableForAllPageTypes,flexipage:availableForRecordHome,force:hasRecordId" access="global" controller="OppProductsTotalValueController">
<aura:attribute name="oppProducts" type="OpportunityLineItem[]"/>
<aura:attribute name="displayText" type="String" default="" description="for displaying a message when the opportunity has no products added"/>
<aura:attribute name="tableColumns" type="List"/>
<aura:attribute name="headerTitle" type="Aura.Component[]">
<b>Total Value of Added Products</b>
</aura:attribute>
<aura:handler name="init" action="{!c.doInit}" value="{!this}"/>
<lightning:card>
<aura:set attribute="title">{!v.headerTitle}</aura:set>
<p class="slds-card__body_inner">
<aura:text value="{! v.displayText }"/>
</p>
<lightning:datatable data="{!v.oppProducts}" columns="{!v.tableColumns}" keyField="id" hideCheckboxColumn="true"/>
</lightning:card>
</aura:component>
CONTROLLER
({
doInit: function(component, event, helper) {
//Get opportunity products
var action = component.get("c.getOppProducts");
action.setParams({"oppId": component.get("v.recordId")});
// Pass the data to my attribute and create table
action.setCallback(this, function(response){
var state = response.getState();
if(state === "SUCCESS"){
if(response.getReturnValue().length == 0)
{
component.set("v.displayText","This opportunity has no products added to it.");
}
else
{
helper.createTable(component);
component.set("v.oppProducts", response.getReturnValue());
//For checking the data passed back from the server
console.log(JSON.parse(JSON.stringify(component.get("v.oppProducts"))));
}
}
else{
console.log('Problem getting Opportunity Products, response state: ' + state);
}
});
$A.enqueueAction(action);
}
})
HELPER
({
createTable : function(component) {
component.set('v.tableColumns', [
{label:'Product Code', fieldName:'prodCode', type:'text'},
{label:'Total Value', fieldName:'totalVal', type:'currency'}
]);
}
})
APEX CLASS
public class OppProductsTotalValueController {
@AuraEnabled
public static OpportunityLineItem[] getOppProducts(Id oppId)
{
// Check to make sure relevant fields are accessible to this user
String[] fieldsToCheck = new String[] {
'ProductCode', 'TotalPrice'
};
Map<String,Schema.SObjectField> fieldDescribeTokens =
Schema.SObjectType.OpportunityLineItem.fields.getMap();
for(String field : fieldsToCheck) {
if( ! fieldDescribeTokens.get(field).getDescribe().isAccessible()) {
throw new System.NoAccessException();
}
}
//Check for any products which have been added
OpportunityLineItem[] returnedOppProds;
returnedOppProds = [SELECT ProductCode, TotalPrice FROM OpportunityLineItem WHERE (OpportunityId = :oppId AND TotalPrice > 0)];
return returnedOppProds;
}
}
Thanks in advance!
Set your tableColumns like:
component.set('v.tableColumns', [
{label:'Total Price', fieldName:'TotalPrice', type:'Currency'},
{label:'Id', fieldName:'Id', type:'Id'}
]);
Assume return value from the Apex is:
[{"TotalPrice":240,"Id":"00k0oXXXXXxAo1XXAS"},{"TotalPrice":300,"Id":"00k0oXXxxxxAo1XXAS"}]
So the fieldName of your tableColumns must be same as it is in return value and the type of it's also same as according to it's value.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
All Answers
Set your tableColumns like:
component.set('v.tableColumns', [
{label:'Total Price', fieldName:'TotalPrice', type:'Currency'},
{label:'Id', fieldName:'Id', type:'Id'}
]);
Assume return value from the Apex is:
[{"TotalPrice":240,"Id":"00k0oXXXXXxAo1XXAS"},{"TotalPrice":300,"Id":"00k0oXXxxxxAo1XXAS"}]
So the fieldName of your tableColumns must be same as it is in return value and the type of it's also same as according to it's value.
I hope you find the above solution helpful. If it does, please mark as Best Answer to help others too.
Thanks,
Ajay Dubedi
Your advice did the trick! Much appreciated!