function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion

Getting 'System.Exception: SObject row does not allow errors:' on addError

Hi, I have a trigger that just needs to make two fields (Role__c and Product__c) unique for a given contact. When I try to write an error message I get the following System error.

'execution of BeforeUpdate caused by: System.Exception: SObject row does not allow errors:'. The code I am using is ...
trigger UniqueRoleProductTrigger on Contact_Role__c (before insert, before update)
if (Trigger.isBefore)
if (Trigger.isUpdate)
for (Integer i = 0; i <; i++)
Contact_Role__c newRole =[i];

Contact curContact =
[select id, AccountId from Contact where id = :newRole.Contact_Person__c];

Contact_Role__c[] RoleProduct =
[select id, Product__c, Role__c from Contact_Role__c
where id = :newRole.Id];

for (Contact_Role__c r: RoleProduct)
if ((r.Product__c == newRole.Product__c) && (r.Role__c == newRole.Role__c))
r.addError('Role, Product pair already exists. Can not have duplicate Role-Product pair for the same contact.');
} //end of if statement

} //end of for loop
}//end if update

 I tried as well, but still getting the error. Would greatly appreciate any help.


Best Answer chosen by Admin (Salesforce Developers) 
Got it . Instead of 'r.addError', it should be 'newRole.addError.'

All Answers

Got it . Instead of 'r.addError', it should be 'newRole.addError.'

This was selected as the best answer


I would suggest as well that you take those SOQL select statements and move them outside the main loop; otherwise you're going to blow a governor limit next time you do a bulk update....




Appreciate the help.


I received the same error and the reason was that I was trying to add the error to Trigger.old record. Changing it to resolved the issue, which makes sense.




I have a similar issue where I have my soql query inside of the for loop. Could you please give example of how to put the soql outside the loop since I am using some variable within the trigger object, for example addError method, etc


I'm not sure the code above is doing what you want.  It seems to skip the code when you are inserting a new Contact Role based on the "If (Trigger.isUpdate)" statement, so I think you could insert a duplicate Contact Role without issue.  Also, I don't see a where clause limiting the Contact Roles query to only those linked to the Contact you are dealing with in the "RoleProduct" query.  I suspect you won't be able to add a Role-Product combo that exists to a different Contact because of this, instead of making it a 3 way uniqueness criteria (Contact-Role-Product).


What you want is a bit complicated to "bulkify" since I think you need a map of Contact Ids to a map of Roles to a Product list.  Like Map<Id, Map<Text, List<Text>>> to track the ContactId with a map of Role to Product Ids.


Maybe we can take this offline if you want help to make it a bulk trigger and then post the results to save typing and back and forth posting on it.