2

I created an addError() method in a trigger with a custom error message like so:

item.addError('Hey! You aren't authorized to do that!');

I have also received error emails like so:

Dear Admin,
Apex script unhandled trigger exception by user/organization...(etc)
FIELD_CUSTOM_VALIDATION_EXCEPTION, Hey! You aren't authorized to do that!: []

I suppose I have two questions:

  1. Am I getting this email because of the addError() message? I looked here and didn't see it mention anything about getting an email, but because it has the same message I have to assume it is correleated.
  2. If it is because of that addError() message, is there a way to turn the email off?
  • the error message says occurring in a trigger but you state you coded in a controller – cropredy Aug 12 '16 at 17:24
  • Sorry, I meant trigger. I will edit that now. – Curious Blueprints Aug 12 '16 at 17:24
  • in what context is the trigger invoked -- standard user interface, some VF controller, some REST class, etc? – cropredy Aug 12 '16 at 17:27
  • Normally, the trigger is invoked from saving a record on a VF page. – Curious Blueprints Aug 12 '16 at 17:27
2

No, addError doesn't send emails. You have code that does something with "allOrNone" set to true, without a try-catch block, and so it's failing.

The "Last Modified By" user is the one that receives exceptions for a given unit of code, and there's no way to disable that.

Start catching your exceptions, and you'll stop getting the emails.

  • Yep. It was a not-often used controller that I didn't think to look at. No try-catch. Thanks again. – Curious Blueprints Aug 12 '16 at 17:35
2

Using a VF controller, and doing DML, you need the following syntax to avoid the email

   public PageReference mySave() {
      try {
        update theRecord;
      }
      catch (DMLException e) {
        ApexPages.addMessage(new ApexPages.Message(ApexPages.Severity.ERROR,e.getMessage()):
     }
   }

The standard UI will catch DML errors, as will Data Loader - but custom Visualforce DML will not

0

You have to throw error, it will stop execution of further code. You will got mail by this approach.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.