OppRecordCombo: BeforeUpdate 的执行由: System.FinalException: SObject row does not allow errors ()

OppRecordCombo: execution of BeforeUpdate caused by: System.FinalException: SObject row does not allow errors ()

我在机会对象上为插入前和更新前创建了一个触发器,通过它我将 Trigger.newMap 变量作为参数发送到我正在检查是否存在的顶点 class如果不存在特定的记录模式,则针对另一个称为记录组合的对象中的记录进行特定组合,并尝试添加错误。但是当我尝试创建机会或更新机会时,出现以下异常。

OppRecordCombo: execution of BeforeUpdate caused by: System.FinalException: SObject row does not allow errors () 

我的触发器在下面

trigger OppRecordCombo on Opportunity (before insert, before update) {
    if((Trigger.isBefore && Trigger.isInsert) || (Trigger.isBefore && Trigger.isUpdate)) {
        OppRecordComboClass opp = new OppRecordComboClass();
        opp.checkComboValidate(Trigger.newMap);
    }
}

而我的顶点class如下

public without sharing class OppRecordComboClass {
    //private List<Opportunity> opportunities;
    String recordComboString;
    String oppComboString;
    List<String> recordComboStringList = new List<String>();

    public OppRecordComboClass() {
    }

    public void checkComboValidate(Map<Id, Opportunity> opportunities) {
        try {
            List<Record_Combination__c> recordcombo = [SELECT Client__r.Name,
                                                            Client_SFDC_ID__c,
                                                            Agency__r.Name,
                                                            Agency_SFDC_ID__c
                                                            FROM Record_Combination__c];

            for(Record_Combination__c recComb : recordcombo) {
                recordComboString = recComb.Client__r.Name + recComb.Client_SFDC_ID__c + recComb.Agency__r.Name + recComb.Agency_SFDC_ID__c;
                recordComboStringList.add(recordComboString);
            }

            List<Opportunity> oppCheckList = [SELECT Id,
                                                    Account.Name,
                                                    AccountId,
                                                    Agency_Name__r.Name,
                                                    Agency_Name__c,
                                                    Allow_New_Account_Combination__c
                                                    FROM Opportunity
                                                    WHERE Id in :opportunities.keySet()];

            for(Opportunity opp : oppCheckList) {
                if(opp.Allow_New_Account_Combination__c == false) {
                    oppComboString = opp.Account.Name + opp.AccountId + opp.Agency_Name__r.Name + opp.Agency_Name__c;
                    System.debug('This is the opp combo string akki ' + oppComboString);
                    if(!recordComboStringList.contains(oppComboString)) {
                        //Opportunity triggerOpp = opportunities.get(opp.Id);
                        opportunities.get(opp.Id).addError('This Customer and Agency combination doesn\'t exist in OB. Do you still want to create this Opportunity');
                    }
                }
            }
        } catch(Exception e) {
            System.debug('An exception occured at the line ' + e.getLineNumber() + ' exception is ' + e.getMessage());
        }
    }
}

任何人都可以告诉我为什么会发生这种情况以及如何纠正这种情况?

  1. 不要将 trigger.newMap 传递给您的方法。这个变量是 Map<Id, Opportunity>when you are before insert ids don't exist yet(好吧,呃)。您没有地图的键,因此变量将为空并且您的逻辑将被跳过。改为传递 trigger.new
  2. 不要SELECT ... FROM Opportunity WHERE Id IN : ...。你有数据的“新”状态,从触发器传递。如果您查询,您将获得此保存操作之前的旧数据,而不是用户现在编辑的数据。或者什么都没有(因为如果你是 before insert - 数据库中还没有东西)。在 trigger.new.
  3. 上处理原作
  4. 您不能 addError() 您查询的内容。它现在没有改变。 FinalException 不是一个好名字,但它告诉您将 addError 放在 trigger.new
  5. 的元素上