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
Jan Kopejtko 2Jan Kopejtko 2 

SObject row does not allow errors message

Hey guys, I have a simple trigger before change:
 
trigger AssemblyDiaryEditValidation on Assembly_diary__c (before update) {
    Set<Id> aids = new Set<Id>();
	Set<Id> saids = new Set<Id>();
    Set<Id> sraids = new Set<Id>();
    for(Assembly_diary__c a :trigger.old) {
        aids.add(a.Id);
        saids.add(a.Service_Appointment__c);
    }
    ServiceAppointment[] sa = [Select Id From ServiceAppointment where Id in :saids];
    AssignedResource[] ar = [Select Id, ServiceResourceId From AssignedResource where ServiceAppointmentId in :saids];
    if(ar != null) {
        sraids.add(ar[0].ServiceResourceId);
    }
    ServiceResource[] sr = [Select Id, RelatedRecordId From ServiceResource where Id in :sraids]; 
    for (Assembly_diary__c b :trigger.old) {
    for(integer i = 0; i < Trigger.new.size(); i++) {
        if(sr != null) {
            if(sr[0].RelatedRecordId != UserInfo.getUserId()) {
                b.addError('Nejste oprávněni upravit tuto položku');
            }
        }
    }
    }
}

I get the error message SObject row does nto allow errors on line 19. I know that you can use the addError method for only those records that are avaliable in Trigger Context. But I thought i fixed it? How do I fix this?
Jay Wang 2Jay Wang 2
Hey Jan,

on line 15 you are iterating through trigger.old, however addError only works on Trigger.New for updates.

Please refer to: https://developer.salesforce.com/docs/atlas.en-us.apexcode.meta/apexcode/apex_triggers_exceptions.htm