Wednesday, June 1, 2016

Developer: System.Exception: SObject row does not allow errors

"SObject row does not allow .."  error was encountered while doing validation in a BeforeDelete trigger.

Solution

Very often; error in triggers are handled the traditional way that object.adderror('ErrorMessage').
trigger ClosedOpportunityPreventDeletion on Opportunity (before delete) {
if (system.Trigger.isDelete){
Opportunity[] Opps = [select id, (select id, Opportunity__c from PCFS__r) from Opportunity where id in :Trigger.oldMap.keySet()];

for (Opportunity o : Opps){
if(o.PCFS__r.size()>0){
o.adderror('You cannot delete this Opportunity as it has one or more Customer Forms associated with it');
}
}
}
However, when using adderror in a before delete it is important to maintain Context, the error can only be applied to those records that are in context.

Use the OldMap to get the actual record and apply the adderror message against that record. That should solve the problem. See sample below.


  Opportunity actualRecord = Trigger.oldMap.get(o.Id);
actualRecord.adderror('You cannot delete this O


No comments:

Post a Comment