function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
okaylah52okaylah52 

How to display an exception message from a trigger exception?

Hi All,

I am a newbie.

Before inserting or updating an opportunity, I have a trigger that fires and checks for a certain condition, i.e. the opportunity company must have at least a contact. If the trigger throws an exception, how can I catch the exception in the Opportunity panel/page so that i can display an alert instead of an ugly message seen below?

Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger CheckContactTrigger caused an unexpected exception, contact your administrator: CheckContactTrigger: execution of BeforeInsert caused by: MissingCompanyContactException.MissingCompanyContactException: The company "Rajan Co" must have a contact.: Trigger.CheckContactTrigger: line 7, column 18

 If an alert is not possible, then how can I make the error message looks nicer (just show the exception message) like below?

Error: Invalid Data.
Review all error messages below to correct your data.
The company "Rajan Co" must have a contact.

If you have a better idea, please share it here.

Thanks.
RajanJasujaRajanJasuja
Try this,

Trigger.old[0].addError('can\'t delete this account.');

But it will show the error on next page.

okaylah52okaylah52
worked like a champ. thanks.