【发布时间】:2021-09-23 20:51:41
【问题描述】:
public class InstanceTriggerHandler {
public static void createInstanceHistoryRecord(Map<ID,Instance__c> newMap, Map<ID,Instance__c> oldMap) {
List<Instance_History__c > listOfIntHistoryToCreate = new List<Instance_History__c >();
List<Schema.FieldSetMember> trackedFields = SObjectType.Instance__c.FieldSets.Case_View.getFields();
if (trackedFields.isEmpty()) return;
for(Instance__c ins:newMap.values()) {
for (Schema.FieldSetMember fst : trackedFields) {
String fieldName = fst.getFieldPath();
String fieldLabel = fst.getLabel();
Instance__c oldInstance = (oldMap != null) ? oldMap.get(ins.id) : new Instance__c();
if (ins.get(fieldName) != oldInstance.get(fieldName)) {
String newValue = String.valueOf(ins.get(fieldName));
String oldValue = String.valueOf(oldInstance.get(fieldName));
if (oldValue != null && oldValue.length()>255) {
oldValue = oldValue.substring(0,255);
}
if (newValue != null && newValue.length()>255) {
newValue = newValue.substring(0,255);
}
final Instance_History__c instanceHistory = new Instance_History__c();
instanceHistory.Field__c = fieldLabel;
instanceHistory.Instance__c = ins.Id;
instanceHistory.Field_API__c = fieldName;
instanceHistory.Original_Value__c = oldValue;
instanceHistory.New_Value__c = newValue;
listOfIntHistoryToCreate.add(instanceHistory);
}
}
}
if(!listOfIntHistoryToCreate.isEmpty()) {
insert listOfIntHistoryToCreate;
}
}
}
Apex 给我一个错误。
“无效类型:Schema.Instance_History”。
我已经检查了我的自定义对象,它确实在引用它的所有代码中都有双下划线。有人可以帮忙吗?
【问题讨论】:
-
您为什么要创建自己的 History 对象,而不是启用 Field History Tracking 并让 Salesforce 为您完成?
标签: salesforce apex