0

I have after update trigger on contact. I am trying to write a friendly error to screen whenever x is happening.(in apex code) I followed this explanation: Difference between the multiple messaging options in Visualforce? and this one: https://albasfdc.com/2017/04/17/demystifying-adderror/
Are they still relvant?

When i Use just adderror from trigger context, nothing breaks and no error is thrown to screen.(maybe i should use vf Page? couldn't make it work... what should be the standrdcontroller, anyway to rerender after saving contact record) When i do something like:

trigger triggerTry on contact(after update){
   list<string>contactIds = new list<string>();
   for(Contact contact: trigger.new){
    contactIds.add(contact.id);
}
   list<contact> contacts = [select id from contact where id in : contactIds];
   contacts[0].adderror('error');
}

I get something like:
caused by: System.Exception: SObject row does not allow errors".

Any simple example for this situation?(Raising friendly user error from trigger context)

1
  • you have to use the addError directly on the contacts included in the Trigger.new list. Your soql query is useless. Commented May 26, 2020 at 15:15

1 Answer 1

0

You have to use adderror method to show the custom error message either in record level or at field level. Please refer the below code for reference

trigger AccountTrigger on Account (after update) {

for (Account acct: Trigger.new) {
    acct.Name.addError('Error added to Name on trigger');
    acct.addError('Error added to account on trigger');
    }
}

Output

You must log in to answer this question.

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