• mik Per
  • NEWBIE
  • 0 Points
  • Member since 2020

  • Chatter
    Feed
  • 0
    Best Answers
  • 0
    Likes Received
  • 0
    Likes Given
  • 0
    Questions
  • 1
    Replies
public with sharing class DataTableController {
    
    @AuraEnabled
    public static DataTableWrapper initRecords(String ObjectName, String fieldSetName, String Orderby, String OrderDir) {
        
        DataTableWrapper dtw = new DataTableWrapper();
        List<LabelDescriptionWrapper> labelList = new List<LabelDescriptionWrapper>();
        List<String> fieldSet = new List<String>();
        Set<String> fieldNameSet = new Set<String>(getFieldSet(ObjectName, fieldSetName).split(','));
        system.debug('fieldNameSet11'+fieldNameSet);
        
        if(Schema.getGlobalDescribe().containsKey(ObjectName) ) {
            //sObject sObj = Schema.getGlobalDescribe().get(ObjectName).newSObject() ;
            //system.debug('sObj+++++++++++++++++++'+sObj);
            
            //get all the labels for sObject fields and put them in a map, keyed to the field api name
            Map<String, Schema.SObjectField> fieldMap = Schema.getGlobalDescribe().get(ObjectName).getDescribe().fields.getMap();
            //fieldmap contains all fields name of an sObject in proper naming case.
            system.debug('fieldMap++++++++'+fieldMap);
            Map<Schema.SObjectField,String> fieldToAPIName = new Map<Schema.SObjectField,String>();
            system.debug('fieldToAPIName+++++++++'+fieldToAPIName);
            Map<String, String> apiNameToLabel = new Map<String, String>();
            Boolean loopFlag = false;
            for(String fieldName :  fieldNameSet){
                if(loopFlag == false && fieldMap.containsKey(fieldName)){
                    fieldSet.add(fieldName);
                    labelList.add(new LabelDescriptionWrapper(fieldMap.get(fieldName).getDescribe().getLabel(), 
                                                              fieldName == 'Name' ? 'LinkName' : fieldName,  
                                                              'url',
                                                              new typeAttributesWR(new labelWR(fieldName), '_self'),
                                                              true));
                    loopFlag = true; 
                    continue;
                }
                if(fieldMap.containsKey(fieldName)) {
                    fieldSet.add(fieldName);
                    //labelList.add(new LabelDescriptionWrapper(fieldMap.get(fieldName).getDescribe().getLabel(), fieldName, fieldMap.get(fieldName).getDescribe().getType().name().toLowerCase(), true ));
                    labelList.add(new LabelDescriptionWrapper(fieldMap.get(fieldName).getDescribe().getLabel(), 
                                                              fieldName, 
                                                              'text', null,
                                                              //fieldMap.get(fieldName).getDescribe().getType().name().toLowerCase(), 
                                                              true ));
                }
            }
            
            //call method to query
            List<sObject> sObjectRecords = getsObjectRecords(ObjectName, fieldSet, 20, '', Orderby, OrderDir);
            system.debug('labelList '+labelList);
            dtw.ldwList     = labelList;
            dtw.sobList     = sObjectRecords;
            dtw.fieldsList     = fieldSet;
            dtw.totalCount  = Database.countQuery('SELECT count() FROM '+ObjectName);
        }
        system.debug('dtw+++++++'+dtw);
        //returns wrapper datatype and fieldset fields properties.
        return dtw;
    }
    
    @AuraEnabled
    public static List<sObject> getsObjectRecords(String ObjectName, List<String> fieldNameSet, Integer LimitSize, String recId, String Orderby, String OrderDir) {
        
        OrderDir = String.isBlank(OrderDir) ? 'asc' : OrderDir;
        String query = 'SELECT '+String.join(fieldNameSet, ',')+' FROM '+ObjectName;
        if(String.isNotBlank(recId)) {
            recId = String.valueOf(recId);
            query += ' WHERE ID >: recId ';
        }
        
        query += ' ORDER BY '+Orderby+' '+OrderDir+' NULLS LAST';
        
        if(LimitSize != null && Integer.valueOf(LimitSize) > 0) {
            LimitSize = Integer.valueOf(LimitSize);
            query += ' Limit '+LimitSize;
        }
        system.debug('query++++++++++'+query);
        system.debug('query++++++++++'+Database.query(query));
        //this will return all the sObject records with all fieldset fields values.
        return Database.query(query);
    }
    
    @AuraEnabled
    //get api name of the fields of a fieldset
    public static String getFieldSet(String sObjectName, String fieldSetName) {
        String result = '';
        SObjectType objToken = Schema.getGlobalDescribe().get(sObjectName);//metadata 
        Schema.DescribeSObjectResult d = objToken.getDescribe();
        Map<String, Schema.FieldSet> FieldsetMap = d.fieldSets.getMap();//<fset name, label>
        system.debug('objToken+++++++++'+objToken);//returns the name of the sobject.
        if(FieldsetMap.containsKey(fieldSetName))
            for(Schema.FieldSetMember f : FieldsetMap.get(fieldSetName).getFields()) {
                if(string.isNotBlank(result)){
                    result += ',';
                }
                result += f.getFieldPath();
            }
        system.debug('result *****'+result);
        return result ;//returns the name of the fields in  a fieldset.
       
    }

    public class DataTableWrapper {
        @AuraEnabled
        public List<LabelDescriptionWrapper> ldwList;
        @AuraEnabled
        public List<sObject> sobList;
        @AuraEnabled
        public List<String> fieldsList;
        @AuraEnabled
        public Integer totalCount;
    }
    
    public class LabelDescriptionWrapper {
        @AuraEnabled
        public String label;
        @AuraEnabled
        public String fieldName;
        @AuraEnabled
        public String type;
        @AuraEnabled
        public typeAttributesWR typeAttributes;
        @AuraEnabled
        public boolean sortable;        
        
        public LabelDescriptionWrapper(String labelTemp, String fieldNameTemp, String typeTemp,  typeAttributesWR typeAttributesTemp, boolean sortableTemp) {
            label       = labelTemp;
            fieldName = fieldNameTemp;
            type       = typeTemp;
            typeAttributes = typeAttributesTemp;
            sortable  = sortableTemp;            
        }            
    } // end LabelDescriptionWrapper
    public class labelWR {
        @AuraEnabled
        public String fieldName;
        public labelWR(String fieldNameTemp){
            fieldName = fieldNameTemp;
        } 
    }
    
    public class typeAttributesWR {
        @AuraEnabled
        public labelWR label;
        @AuraEnabled
        public String target;
        
        public typeAttributesWR(labelWR labelTemp, String targetTemp){
            label = labelTemp;
            target = targetTemp;
        } 
    }
}