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
Mats ErikssonMats Eriksson 

Error Message format for Validation Rule



I have a validation rule on a field in the case object which fires under certain circumstances. The problem is not the formula, it works as expected but the error message is not optimal for end-users:


Error: Invalid Data.
Review all error messages below to correct your data.
Apex trigger ChangeCaseFieldInformationOnFirstSave caused an unexpected exception, contact your administrator: ChangeCaseFieldInformationOnFirstSave: execution of AfterInsert caused by: System.DmlException: Update failed. First exception on row 0 with id 5002000000ISzWNAA1; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, Please enter a serial numer for the device.: [Serial_Number__c]: Class.ChangeCaseFieldsOnFirstSave.ChangeCaseFields: line 42, column 3

Embedded in the red block is my own error message (in bold). Thats and ONLY that is what I would like to show! Not the other garbage aimed at programmers and not end users.


Why is this? Is it because I have a trigger on the case object as well? A trigger that is calling a class to do some housekeeping not related to the validation rule.


EDIT: When the case has been created, other validation rules work as expected. It's only the above one that behaves like this.


I thought a validation rule would fire before the insert...


Any insights?




Hi Mats,


Exactly Mats, your error comes from your trigger after insert of your case. Then, when it updates your case, it looks like your validation rule is false, thus you get your error. I think you may have two ways to handle your problem.

First, go talk with your developer to add try and catch blocks in his apex code. In catch block, use adderror method to display only error message. Go to Apex reference. Search for "adderror", rank 2 "Trigger Exception". It details how to use adderror message and prevent DML exception with trigger.


Another way to fix your issue is switching After Insert to Before Insert. trigger. But I need more detail of your requirement.


First solution, try and catch blocks should fix it.


Hope it will help.