liuyn
2024-03-11 a87f1c3df03078814ee97ad0c8ac200a232419e9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
public without sharing class ObjectHistoryShowController {
    @AuraEnabled
    public static Map<String, Object> getObjectHistoryList(String recordId){
        Map<String, Object> reData = new Map<String, Object>();
        try {
            Id rId = Id.valueOf(recordId);
            String objectApiName = rId.getSObjectType().getDescribe().getName();
 
            // 构建SOQL查询字符串
            String soqlQuery = 'SELECT Id, Name, Original_ID__c FROM ' + objectApiName + ' WHERE Id = :recordId LIMIT 1';
 
            // 执行SOQL查询
            List<SObject> records = Database.query(soqlQuery);
 
            // 处理查询结果
            if (!records.isEmpty()) {
                SObject queriedRecord = records[0];
                String originalId = (String)queriedRecord.get('Original_ID__c');
                System.debug('Queried Record: ' + queriedRecord);
 
                if(originalId <> null){
                    List<ObjHtyInfo> ohiInfoList = new List<ObjHtyInfo>();
                    for (Object_History__c ohItem: [SELECT Id, Name, Record_Original_Id__c, Field_API_Name__c, Object_API_Name__c, NewValue__c, OldValue__c, Update_User__c, Update_User__r.Name, Update_Datetime__c
                                                    FROM Object_History__c
                                                    WHERE Record_Original_Id__c = :originalId
                                                    ORDER BY Update_Datetime__c DESC
                                                    LIMIT 100]){
                    
                        ObjHtyInfo ohi = new ObjHtyInfo();
                        ohi.fieldAPIName = ohItem.Field_API_Name__c;
                        ohi.fieldAPINameLabel = getFieldInfo(objectApiName, ohItem.Field_API_Name__c).get('label');
                        if(getFieldInfo(objectApiName, ohItem.Field_API_Name__c).get('fieldType') == 'Datetime'){
                            if(ohItem.NewValue__c <> null){
                                DateTime newValueDatetime = DateTime.valueOf(ohItem.NewValue__c);
                                ohi.newValue = formatDatetimeGMT(newValueDatetime);
                            }
 
                            if(ohItem.OldValue__c <> null){
                                DateTime oldValueDatetime = DateTime.valueOf(ohItem.OldValue__c);
                                ohi.oldValue = formatDatetimeGMT(oldValueDatetime);
                            }
                        }else if(getFieldInfo(objectApiName, ohItem.Field_API_Name__c).get('fieldType') == 'Picklist'){
                            if(ohItem.NewValue__c <> null){
                                if(getFieldInfo(objectApiName, ohItem.Field_API_Name__c).containsKey(ohItem.NewValue__c)){
                                    ohi.newValue = getFieldInfo(objectApiName, ohItem.Field_API_Name__c).get(ohItem.NewValue__c);
                                }
                            }
 
                            if(ohItem.OldValue__c <> null){
                                if(getFieldInfo(objectApiName, ohItem.Field_API_Name__c).containsKey(ohItem.NewValue__c)){
                                    ohi.oldValue = getFieldInfo(objectApiName, ohItem.Field_API_Name__c).get(ohItem.OldValue__c);
                                }
                            }
                        }else{
                            ohi.newValue = ohItem.NewValue__c;
                            ohi.oldValue = ohItem.OldValue__c;
                        }
                        ohi.updateDatetime = formatDatetimeGMT(ohItem.Update_Datetime__c);
                        ohi.updateUser = ohItem.Update_User__r.Name;
                        ohiInfoList.add(ohi);
                    }
 
                    reData.put('ohiInfoList', ohiInfoList);
                }
            }else{
                System.debug('No record found with ID: ' + recordId);
            }
 
            
        } catch (Exception e) {
            System.debug('Error: ' + e.getMessage());
        }
 
        
        return reData;
    }
 
    public static Map<String, String> getFieldInfo(String objectApiName, String fieldName) {
        Map<String, String> fieldInfo = new Map<String, String>();
        try {
            // 获取对象类型
            SObjectType objectType = Schema.getGlobalDescribe().get(objectApiName);
 
            // 获取字段
            if (objectType != null) {
                SObjectField field = objectType.getDescribe().fields.getMap().get(fieldName);
 
                if (field != null) {
                    // 获取API名称
                    fieldInfo.put('apiName', field.getDescribe().getName());
                    // 获取Label
                    fieldInfo.put('label', field.getDescribe().getLabel());
                    // 获取字段类型
                    fieldInfo.put('fieldType', field.getDescribe().getType().name());
                }
            }
        } catch (Exception e) {
            // 处理异常,例如对象或字段不存在等情况
            System.debug('Error: ' + e.getMessage());
        }
 
        return fieldInfo;
    }
 
    public static Map<String, String> getPicklistValues(String objectApiName, String fieldName) {
        Map<String, String> picklistInfo = new Map<String, String>();
        
        // 获取 SObject 的 Describe 信息
        Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe();
        Schema.SObjectType objectType = gd.get(objectApiName);
 
        if (objectType != null) {
            // 获取字段的 Describe 信息
            Map<String, Schema.SObjectType> sObjGd = Schema.getGlobalDescribe();
            Schema.SObjectType sObjType = sObjGd.get(objectApiName);
            Schema.DescribeSObjectResult sObjDescribe = sObjType.getDescribe();
            Map<String, Schema.SObjectField>sObjFieldMap = sObjDescribe.fields.getMap();
            List<Schema.PicklistEntry> sObjValues = sObjFieldMap.get(fieldName).getDescribe().getPickListValues();
 
            for (Schema.PicklistEntry pv : sObjValues){
                picklistInfo.put(pv.getValue(), pv.getLabel());
            }
        }
 
        return picklistInfo;
    }
 
    private static String formatDatetimeGMT(Datetime dt) {
        //return (dt == null) ? '' : dt.formatGmt('yyyy-MM-dd\'T\'HH:mm:ss.SSS\'Z\'')
        return (dt == null) ? '' : dt.format('yyyy-MM-dd HH:mm:ss');
    }
 
    public class ObjHtyInfo{
        @AuraEnabled
        public String updateDatetime{ get; set; }
 
        @AuraEnabled
        public String fieldAPIName{ get; set; }
 
        @AuraEnabled
        public String fieldAPINameLabel{ get; set; }
 
        @AuraEnabled
        public String updateUser{ get; set; }
 
        @AuraEnabled
        public String oldValue{ get; set; }
 
        @AuraEnabled
        public String newValue{ get; set; }
    }
}