You need to sign in to do that
Don't have an account?

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 ...
I tried Trigger.new.addError as well, but still getting the error. Would greatly appreciate any help.
Thanks
'execution of BeforeUpdate caused by: System.Exception: SObject row does not allow errors:'. The code I am using is ...
Code:
trigger UniqueRoleProductTrigger on Contact_Role__c (before insert, before update)
{
if (Trigger.isBefore)
{
if (Trigger.isUpdate)
{
for (Integer i = 0; i < Trigger.new.size(); i++)
{
Contact_Role__c newRole = Trigger.new[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 Trigger.new.addError as well, but still getting the error. Would greatly appreciate any help.
Thanks
All Answers
Hey,
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....
-philbo
Thanks,
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 Trigger.new resolved the issue, which makes sense.
Hey,
I have a similar issue where I have my soql query inside of the trigger.new 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.