记录触发流程中DML操作错误处理

问题描述 投票:0回答:1

我已经在帐户对象上创建了记录触发流程。我的要求是,当帐户记录上的特定字段更新时,所有相关的自定义对象记录状态都会发生变化。现在,自定义对象记录也有一些验证规则、触发器和流程。所以我想在记录触发流程中进行错误处理。假设记录更新中有 10 条记录,7 条成功,3 条失败,然后 7 条记录更新成功,3 条错误邮件发送给管理员。我怎样才能达到这个要求

我期待记录触发流程中的 try catch 功能

salesforce salesforce-lightning dml lightning
1个回答
0
投票

例如:如果我们使用 Apex 触发器来实现:

在帐户上触发 AccountTrigger(更新后){ 列表 customObjectsToUpdate = 新列表();

for (Account acc : Trigger.new) {
    if (acc.Custom_Field__c != Trigger.oldMap.get(acc.Id).Custom_Field__c) {
        List<Custom_Object__c> relatedCustomObjects = [SELECT Id, Status__c FROM Custom_Object__c WHERE Account__c = :acc.Id];
        
        for (Custom_Object__c obj : relatedCustomObjects) {
            obj.Status__c = 'Updated'; // Example update logic
            customObjectsToUpdate.add(obj);
        }
    }
}

try {
    update customObjectsToUpdate;
} catch (Exception e) {
    // Handle exceptions, notify admins via email
    String errorMessage = 'Error updating Custom Objects: ' + e.getMessage();
    System.debug(errorMessage);
    // Send email notification to admins using Apex Email Services or custom logic Error Handling: Notify admins via email when there are errors, such as validation rule failures on the Custom Object records.
}

}

© www.soinside.com 2019 - 2024. All rights reserved.