You need to sign in to do that
Don't have an account?
Martin S.
How to create a DataTable with displaying bought Products from a Contact?
Hi,
I want to display a DataTable on the Contact Page and want to show in this list all products that the contact has bought. But I only get a blank table with only the table header.
I am new to SF and just used this tutorial: sfdcmonkey- Lightning-datatable-base-component (http://sfdcmonkey.com/2018/01/05/lightning-datatable-base-component/)
I tried to change the sample data to my contact data but I got no Output. I also created a field-set under Contact pointing to my product object.
Server Side Controller – Apex Class LightningDataTableVehicleController
LightningDataTableVehicle.cmp
LightningDataTableVehicleController.js
LightningDataTableVehicleHelper.js
And for displaying the data LightningDataTableVehicleTest.app
Then I went to my Contact page -> Edit this page - and there I choose the DataTable.
The final look is:
And that is the field-set I created under SF Classic - Setup - Contact Fieldset. I want to show all bought vehicles from a contact at the contact page and after that the goal is to choose one record and process with the id of the choosen vehicle.
Thank you in advance for your help!
I want to display a DataTable on the Contact Page and want to show in this list all products that the contact has bought. But I only get a blank table with only the table header.
I am new to SF and just used this tutorial: sfdcmonkey- Lightning-datatable-base-component (http://sfdcmonkey.com/2018/01/05/lightning-datatable-base-component/)
I tried to change the sample data to my contact data but I got no Output. I also created a field-set under Contact pointing to my product object.
Server Side Controller – Apex Class LightningDataTableVehicleController
public class LightningDataTableVehicleController { /* Method Name : getAccRecords Purpose : To get the wrapper of Columns and Headers */ @AuraEnabled public static DataTableResponse getAccRecords(String strObjectName, String strFieldSetName){ //Get the fields from FieldSet Schema.SObjectType SObjectTypeObj = Schema.getGlobalDescribe().get(strObjectName); Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe(); Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(strFieldSetName); //To hold the table hearders List<DataTableColumns> lstDataColumns = new List<DataTableColumns>(); //Field to be queried - fetched from fieldset List<String> lstFieldsToQuery = new List<String>(); //The final wrapper response to return to component DataTableResponse response = new DataTableResponse(); for( Schema.FieldSetMember eachFieldSetMember : fieldSetObj.getFields() ){ String dataType = String.valueOf(eachFieldSetMember.getType()).toLowerCase(); //This way we can set the type of a column //We do not get the exact type from schema object which matches to lightning:datatable component structure if(dataType == 'datetime'){ dataType = 'date'; } //Create a wrapper instance and store label, fieldname and type. DataTableColumns datacolumns = new DataTableColumns( String.valueOf(eachFieldSetMember.getLabel()) , String.valueOf(eachFieldSetMember.getFieldPath()), String.valueOf(eachFieldSetMember.getType()).toLowerCase() ); lstDataColumns.add(datacolumns); lstFieldsToQuery.add(String.valueOf(eachFieldSetMember.getFieldPath())); } //Form an SOQL to fetch the data - Set the wrapper instance and return as response if(! lstDataColumns.isEmpty()){ response.lstDataTableColumns = lstDataColumns; String query = 'SELECT Id, ' + String.join(lstFieldsToQuery, ',') + ' FROM Contact'; System.debug(query); response.lstDataTableData = Database.query(query); } return response; } //Wrapper class to hold Columns with headers public class DataTableColumns { @AuraEnabled public String label {get;set;} @AuraEnabled public String fieldName {get;set;} @AuraEnabled public String type {get;set;} //Create and set three variables label, fieldname and type as required by the lightning:datatable public DataTableColumns(String label, String fieldName, String type){ this.label = label; this.fieldName = fieldName; this.type = type; } } //Wrapper calss to hold response - This response is used in the lightning:datatable component public class DataTableResponse { @AuraEnabled public List<DataTableColumns> lstDataTableColumns {get;set;} @AuraEnabled public List<sObject> lstDataTableData {get;set;} public DataTableResponse(){ lstDataTableColumns = new List<DataTableColumns>(); lstDataTableData = new List<sObject>(); } } }
LightningDataTableVehicle.cmp
<aura:component implements="flexipage:availableForAllPageTypes" controller="LightningDataTableVehicleController"> <aura:attribute name="mydata" type="Object"/> <aura:attribute name="mycolumns" type="List"/> <aura:handler name="init" value="{!this}" action="{!c.doInit}" /> <lightning:datatable data="{! v.mydata }" columns="{! v.mycolumns }" keyField="Id"/> </aura:component>
LightningDataTableVehicleController.js
({ doInit : function(component, event, helper) { helper.getDataHelper(component, event); }, })
LightningDataTableVehicleHelper.js
({ getDataHelper : function(component, event) { var action = component.get("c.getAccRecords"); //Set the Object parameters and Field Set name action.setParams({ strObjectName : 'Contact', strFieldSetName : 'DataTableFieldSet' }); action.setCallback(this, function(response){ var state = response.getState(); if(state === 'SUCCESS'){ component.set("v.mycolumns", response.getReturnValue().lstDataTableColumns); component.set("v.mydata", response.getReturnValue().lstDataTableData); }else if (state === 'ERROR'){ var errors = response.getError(); if (errors) { if (errors[0] && errors[0].message) { console.log("Error message: " + errors[0].message); } } else { console.log("Unknown error"); } }else{ console.log('Something went wrong, Please check with your admin'); } }); $A.enqueueAction(action); } })
And for displaying the data LightningDataTableVehicleTest.app
<aura:application extends="force:slds"> <c:LightningDataTableVehicle /> </aura:application>
Then I went to my Contact page -> Edit this page - and there I choose the DataTable.
The final look is:
And that is the field-set I created under SF Classic - Setup - Contact Fieldset. I want to show all bought vehicles from a contact at the contact page and after that the goal is to choose one record and process with the id of the choosen vehicle.
Thank you in advance for your help!