【问题标题】:I am new to Apex development and trying to compile this code but failing我是 Apex 开发的新手并尝试编译此代码但失败了
【发布时间】: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


【解决方案1】:

自定义对象的字段历史对象需要两个下划线,试试Instance__History。对您来说最简单的方法可能是使用http://workbench.developerforce.com/ 查看所有对象?你曾经使用过“描述”操作吗?

https://help.salesforce.com/s/articleView?id=sf.tracking_field_history.htm&type=5

Salesforce 将对象的跟踪字段历史存储在关联的 名为 StandardObjectNameHistory 或 CustomObjectName__History 的对象。 例如,AccountHistory 表示更改的历史记录 客户记录字段的值。相似地, MyCustomObject__History 跟踪 MyCustomObject__c 的字段历史记录 自定义对象。

【讨论】:

    猜你喜欢
    • 2017-04-10
    • 1970-01-01
    • 1970-01-01
    • 2022-06-13
    • 2016-03-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多