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
Dman100Dman100 

trapping errors thrown by validation rules in a trigger

I have a trigger that occasionally throws an error due to a validation rule.  I have a try/catch block in my trigger, but the validation rule isn't caught in the catch block.  Is there a way to gracefully handle the error thrown by the validation rule when the trigger fires?

 

If there is no way to trap for the validation rule errors in a trigger, can anyone explain why?  Currently, a big red apex error message is generated, which is not very user friendly to the end-user.  I'm just trying to handle the error gracefully, if possible.

 

Thanks for any help.

Message Edited by Dman100 on 12-10-2009 11:27 AM
spraetzspraetz
I just tried it and I can catch Validation Exceptions with a try/catch block in a trigger.  Can I see the code you're using?  On what object are operating?
Dman100Dman100

The trigger fires on the OpportunityLineItem, but the validation error is occurring on the Opportunity which is the object I'm updating.  The try/catch doesn't trap the validation rule that is getting thrown on the Opportunity when the update occurs.  I've found a workaround, but I'm interested in someone else can successfully trap validation rule errors in this type of scenario?

pmozz01pmozz01

I am experiencing the same issue with a trigger that fires when a custom object is updated and it tries to update some opportunities that meet a validation rule.  Can you share how you solved this? 

 

Thanks!