Friday 8 September 2017

System.FinalException: SObject row does not allow errors

Arpit Sharma
During Development various type of exception occurred, One of the exception that I have faced is System.FinalException: SObject row does not allow errors. In this tutorial we will discuss reason of this exception.

Points that need to be considered

Where Occurs - This exception occurs during execution of trigger.

Reason of Occurrence - Salesforce provide addError method to mark a record with custom message and prevent any DML operation from occurring.

But this method is applicable only on records that is part of trigger context variable means Trigger.old and Trigger.new. It means We can use this method only in triggers and only on Trigger.old and Trigger.new context variable.

So Let's take an example if we have trigger on account object and we want to stop processing of particular record on particular condition then we can use addError method.

Here is example 

Correct Example
trigger AccountTrigger on Account(before update) {
    for(Account acc : Trigger.new){
        if(acc.Name == 'Working Code'){
            acc.addError('This add error working fine.');
        }
    }
}

Incorrect Example
trigger AccountTrigger on Account(before update) {
    List<Account> accList = [SELECT Id,Name 
                             FROM Account 
                             WHERE Id IN :Trigger.old];
    for(Account acc : accList){
         if(acc.Name == 'Non Working Code'){
             acc.addError('This add error statment will not execute trigger will throw exception i.e.System.FinalException: SObject row does not allow errors');
         }  
    }
}

In First Example we are using addError method on record of trigger context variable but in second case we first reterive record from database then add error on database record which is not allowed in salesforce and salesforce throws error System.FinalException:SObject row does not allow errors.


About the Author

Arpit Sharma / Author & Editor

Certified Salesforce Developer | Salesforce Developer at Cognizant | Blogger