1

I have this trigger to verify this condition , when and status become clos , I can't modifie the object .

Now when I tried to modifie I have this error :

We hit a snag.
Review the errors on this page.
Update : execution of BeforeUpdate caused by: System.FinalException: SObject row does not allow errors Trigger.Update: line 13, column 1

The line 13 is :

  OBJ.Statut__c.addError('can t be modified');

My trigger

trigger Update on OBJ__c (before update) {

     Id currentUser = UserInfo.getUserId();
    Id currentUserProfileId = userinfo.getProfileId();
  
        for(OBJ__c OBJ: trigger.old) 
        {
            if(OBJ.Statut__c == 'Clos' ) 
            {
                OBJ.Statut__c.addError('can t be modified');
            }

        }
}
2
  • 1
    At first blush, this looks like something that would be better to do as a validation rule (rather than as a trigger).
    – Derek F
    Commented Dec 6, 2022 at 12:19
  • @DerekF , I tried the validation rule but did not work for me , that way I used the trigger , now the problem it resoled thank to the solution proposed Commented Dec 6, 2022 at 13:11

1 Answer 1

2

You can't add errors to Trigger.old when Trigger.new is available. You can only add errors on Trigger.old in delete triggers. Instead, use Trigger.newMap to identify the correct record:

    for(OBJ__c OBJ: trigger.old) {
        if(OBJ.Statut__c == 'Clos' ) {
            Trigger.newMap.get(OBJ.id).Statut__c.addError('can t be modified');
        }
    }
1
  • thank you for your help , I used your proposition and it worked for me Commented Dec 6, 2022 at 13:08

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .