1

I am trying to addError on the opportunities based on one condition. But getting System.FinalException: SObject row does not allow errors error while saving the record. Below I have given the code snippet. Please let me know how I can resolve the issue.

if(Trigger.isUpdate && Trigger.isBefore){
        map<Id,Opportunity> newOpptyMap = (map<Id,Opportunity>)trigger.newMap;
        OpportunityTriggerHelper.checkforStageDuration(newOpptyMap,Trigger.OldMap);
    }

The function is below where I am adding the error statement

public static void checkforStageDuration(Map<Id,Opportunity> oldMap, Map<Id,Opportunity> newMap){
        Map<Id,Opportunity> recordsToCheck = new Map<Id,Opportunity>();
        for(Id oppId : newMap.keyset()){
            if(newMap.get(oppId).StageName != oldMap.get(oppId).StageName){
                recordsToCheck.put(oppId,newMap.get(oppId));
            }
        }
        List<Opportunity_Stage_History__c> oppStageHistory = [select Id,Expected_Stage_Duration__c,Opportunity__c,Duration__c,Reason_For_Delay__c from Opportunity_Stage_History__c where Opportunity__c IN :recordsToCheck.keyset()];
        for(Opportunity_Stage_History__c oppHistory : oppStageHistory){
            if(oppHistory.Duration__c>oppHistory.Expected_Stage_Duration__c && oppHistory.Reason_For_Delay__c==null){
                newMap.get(oppHistory.Opportunity__c).addError('The Opportunity has crossed it\'s Stage Duration, Please fill the Reason for Delay !!');
            }
        }
    }
6

The problem is that you are calling addError on Trigger.OldMap instead of Trigger.NewMap. When declaring the signature for checkForStageDuration you have it (oldMap, newMap) but when you are passing in parameters in your trigger, you are passing in (newMap, oldMap).

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.