Created
August 8, 2019 18:17
-
-
Save sudikrt/e952c28e2213ecb47cbeccf25494d6db to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public with sharing class DataTableColumns { | |
| @AuraEnabled | |
| public String label {get;set;} | |
| @AuraEnabled | |
| public String fieldName {get;set;} | |
| @AuraEnabled | |
| public String type {get;set;} | |
| public DataTableColumns(String label, String fieldName, String type){ | |
| this.label = label; | |
| this.fieldName = fieldName; | |
| this.type = type; | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public with sharing class DataTableResponse { | |
| @AuraEnabled | |
| public List<DataTableColumns> lstDataTableColumns {get;set;} | |
| @AuraEnabled | |
| public List<sObject> lstDataTableData {get;set;} | |
| @AuraEnabled | |
| public sObject firstRow {get;set;} | |
| public DataTableResponse(){ | |
| lstDataTableColumns = new List<DataTableColumns>(); | |
| lstDataTableData = new List<sObject>(); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| public with sharing class FieldSetHelper { | |
| /* | |
| @param String strObjectName : required. Object name to get the required filed set | |
| @param String strFieldSetName : required. FieldSet name | |
| @return List<DataTableColumns> list of columns in the specified fieldSet | |
| */ | |
| public static List<DataTableColumns> getColumns (String strObjectName, String strFieldSetName) { | |
| Schema.SObjectType SObjectTypeObj = Schema.getGlobalDescribe().get(strObjectName); | |
| Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe(); | |
| Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(strFieldSetName); | |
| List<DataTableColumns> lstDataColumns = new List<DataTableColumns>(); | |
| for( Schema.FieldSetMember eachFieldSetMember : fieldSetObj.getFields() ){ | |
| String dataType = String.valueOf(eachFieldSetMember.getType()).toLowerCase(); | |
| DataTableColumns datacolumns = new DataTableColumns( String.valueOf(eachFieldSetMember.getLabel()) , | |
| String.valueOf(eachFieldSetMember.getFieldPath()), | |
| String.valueOf(eachFieldSetMember.getType()).toLowerCase() ); | |
| lstDataColumns.add(datacolumns); | |
| } | |
| return lstDataColumns; | |
| } | |
| /* | |
| @param String strObjectName : required. Object name to get the required filed set | |
| @param String strFieldSetName : required. FieldSet name | |
| @param String whereCondition : optional query condition | |
| @param Boolean isSingleRow : param specifies the the response expects single row | |
| @return DataTableResponse : Wrapper class containing the data | |
| */ | |
| public static DataTableResponse getTableData(String strObjectName, String strFieldSetName, String whereCondition, Boolean isSingleRow){ | |
| DataTableResponse response = getTableData (strObjectName, strFieldSetName, whereCondition); | |
| if (isSingleRow) { | |
| if (response.lstDataTableData != null && !response.lstDataTableData.isEmpty ()) { | |
| response.firstRow = response.lstDataTableData [0]; | |
| } | |
| } | |
| return response; | |
| } | |
| /* | |
| @param String strObjectName : required. Object name to get the required filed set | |
| @param String strFieldSetName : required. FieldSet name | |
| @param String whereCondition : optional query condition | |
| @return DataTableResponse : Wrapper class containing the data | |
| */ | |
| public static DataTableResponse getTableData(String strObjectName, String strFieldSetName, String whereCondition){ | |
| Schema.SObjectType SObjectTypeObj = Schema.getGlobalDescribe().get(strObjectName); | |
| Schema.DescribeSObjectResult DescribeSObjectResultObj = SObjectTypeObj.getDescribe(); | |
| Schema.FieldSet fieldSetObj = DescribeSObjectResultObj.FieldSets.getMap().get(strFieldSetName); | |
| List<DataTableColumns> lstDataColumns = new List<DataTableColumns>(); | |
| List<String> lstFieldsToQuery = new List<String>(); | |
| DataTableResponse response = new DataTableResponse(); | |
| for( Schema.FieldSetMember eachFieldSetMember : fieldSetObj.getFields() ){ | |
| String dataType = String.valueOf(eachFieldSetMember.getType()).toLowerCase(); | |
| if(dataType == 'datetime'){ | |
| dataType = 'date'; | |
| } | |
| 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())); | |
| } | |
| if(! lstDataColumns.isEmpty()){ | |
| response.lstDataTableColumns = lstDataColumns; | |
| String query = 'SELECT Id, ' + String.join(lstFieldsToQuery, ',') + ' FROM ' + strObjectName; | |
| if (!String.isBlank(whereCondition)) { | |
| query += ' ' + whereCondition; | |
| } | |
| System.debug(query); | |
| response.lstDataTableData = Database.query(query); | |
| } | |
| return response; | |
| } | |
| public static Integer getRandonNumber (Integer len) { | |
| String str = string.valueof(Math.abs(Crypto.getRandomLong())); | |
| String randomNumber = str.substring(0, len); | |
| return Math.abs(Integer.valueOf(randomNumber)); | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment