Skip to main content
Mak One asked in #Apex

I have seen suggestion with below code for catching Validation Error:

try {

   update user;

}

catch (Exception e) {

   String errorMessage = e.getMessage();

   if(!errorMessage.contains('FIELD_CUSTOM_VALIDATION_EXCEPTION')) {

      ApexPages.addMessage(new ApexPages.Message(ApexPages.severity.ERROR, errorMessage));

   }

}

Is there any better way for this as my custom error message may also contain String: 'FIELD_CUSTOM_VALIDATION_EXCEPTION'

Is there any specific Exception Type provided by Salesforce for Validation Error. Or is there any better way to catch ValidationError?

1 answer
  1. May 25, 2015, 12:04 PM
    Hello Mak,

    You can use this way to show a proper error message on validation exception

    if(e.getMessage().Contains('FIELD_CUSTOM_VALIDATION_EXCEPTION'))

    {

    ApexPages.addMessage(new ApexPages.message(ApexPages.severity.ERROR,+e.getMessage().substringAfter('EXCEPTION,').substringBefore(': []')));

    }

    --

    Praveen Murugesan

    Mark this as best answer if its helps.

     
0/9000