I have created a trigger on the Opportunity object for before insert and before update, through which I am sending the Trigger.newMap variable as an argument to my apex class where I am checking the existence of a particular combination against the records in another Object called record combinations and trying to add an error, if a particular pattern of record doesn't exist. But when I try to create an Opportunity or update it I am getting the below exception.
OppRecordCombo: execution of BeforeUpdate caused by: System.FinalException: SObject row does not allow errors ()
My trigger is below
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);
}
}
And my apex class is as below
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());
}
}
}
Can anyone please let me know why is this happening and how can this be corrected?