[SalesForce] AddError on Before Update

I am trying to display an error message if a field on Quote is updated to a certain value. I am doing it on before update but I am encountering an error
enter image description here
enter image description here

Here is a sample code block I am trying on my org

Best Answer

Been a while since I used it but the docs state Trigger.New...

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

Triggers can be used to prevent DML operations from occurring by calling the addError() method on a record or field. When used on Trigger.new records in insert and update triggers, and on Trigger.old records in delete triggers, the custom error message is displayed in the application interface and logged.

Now I've just tested for myself, the following works:

trigger ContactTrigger on Contact (before insert, before update) {
    contactUtils.testAddError(trigger.new);
}

public class contactUtils {
    public static void testAddError(List<Contact> conList){
        for(Contact con : conList){
            con.addError('nope.');
        }
    }
}

enter image description here

Related Topic