How to resolve the adderror in trigger?

226    Asked by elonji_3393 in Salesforce , Asked on May 1, 2023

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

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

Answered by Elvera Peasley

You need to adderror in Trigger.new or Trigger.newMap, unless you're in a delete trigger, in which case you use Trigger.old or Trigger.oldMap.


Given your code, you can get the record from the Trigger.newMap variable:
// Or, ideally, pass in Trigger.newMap as well.
Map newQuotesById = new Map(quotes);
if(arv.contains(bc)) {
  newQuotesById.get(q.Id).addError('Error');
}


Your Answer

Interviews

Parent Categories