0

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?

0

Trigger is on the before insert and before update. And in your method you're using opportunities map with ID as a key. However in before insert, ID you will not get(its generated after before trigger execution).

Also as per the exception message - You're trying to add an error to record which is not part of trigger context.

4
  • Thank you for the answer. Is there a way to get around this? I want show users an error message if they are trying to create a combination that isn't a part of the existing record combination table. – Akshay Vasu Nov 6 '20 at 5:12
  • you can throw the custom exceptions, which wil be shown to user – Ysr Shk Nov 6 '20 at 5:15
  • 1
    Thank you, I'll try to do that and see if I can get around this. – Akshay Vasu Nov 6 '20 at 5:16
  • Any idea on how to display the exception message to the user on the standard opportunity layout once it caught by exception handler? – Akshay Vasu Nov 6 '20 at 5:59

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.