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; }
|
}
|
}
|