gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 07:51:47 26 4
gpt4 key购买 nike

我在Opportunity对象上创建了一个触发器,用于在插入之前和更新之前,通过该触发器将Trigger.newMap变量作为自变量发送给我的顶点类,在其中我根据另一个Object中的记录检查特定组合的存在记录组合并尝试添加错误(如果不存在特定的记录模式)。但是,当我尝试创建或更新机会时,出现以下异常。

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);
}
}
我的顶点类如下
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());
}
}
}
谁能让我知道为什么会这样,如何解决?

最佳答案

  • 不要将trigger.newMap传递给您的方法。这个变量是Map<Id, Opportunity>when you are before insert ids don't exist yet(嗯,嗯)。您没有 map 键,因此变量将为空,并且逻辑将被跳过。改为传递trigger.new
  • 不要SELECT ... FROM Opportunity WHERE Id IN : ...。您从触发器那里传递了数据的"new"状态。如果您查询,将获得此保存操作之前的旧数据,而不是用户现在编辑的数据。还是什么都不是(因为如果您是before insert-资料在数据库中尚不存在)。处理原始的trigger.new
  • 您不能对查询的内容进行addError()。现在没有改变。 FinalException不是一个好名字,但是它告诉您将addError放在trigger.new的元素上
  • 关于exception - OppRecordCombo : execution of BeforeUpdate caused by: System.FinalException: SObject row does not allow errors (),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64708268/

    26 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com