7

We have a situation where it is possible for a trigger to throw an exception that will be shown to the end user.

This isn't ideal as the user sees a lengthy message containing what is essentially a stack trace.

Is there a clean way for a trigger to raise an error to be shown to the user in a standard page layout without the stack trace kind of output?

E.g. similar to the ApexPages.Message in controllers

I've included a screenshot of an example message - the message content isn't important, it's the "Apex trigger ContactTrigger caused an unexpected exception...", etc that you should focus on.

Example Message Format


As an expansion of the answer given below:

try {
  // thing that throws an exception
}
catch ( Exception e ) {
  theSObjectYouAreManipulating.addError( 'This is the error you show to the user!' );
}

This results in a much more standard and clean error message.

7

Have you considered adding try/catch logic around any code that might cause an exception and then using the addError() method to display a more friendly error message to the user?

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.