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
VT878VT878 

Error Message and Validation Rule

Hi friend,

I have two asks for you. 

1) Is possible use a Error Message of a Validation Field as label? 
    In other words, I have two validation rules with different error messages and in a Apex class I want use their Error Message as I use Custom Label with 'Label' key word.

2) When a Validation Rule is true and a DML statement fail, is possible take from DML Exception object how is the Validation Rule which is true?

I post these questions because I have differente Validation Rule and each of these has a different Error Message, and in a Contoller I must do different operations for each Validation Rule, and now the only solution that I can use is hard coding, for example:

   if (ex.getMessage == 'Error Message1')
        {...}
   else if (ex.getMessage == 'Error Message2')) 
        ​{...}
VineetKumarVineetKumar
  1. Using the MetaData API you can fetch the details of a validation rule by querying the ValidationRule object.
  2. You can only get if the error was a result of some validation. You cannot get the name of the particular validation that caused the error.