Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to show only extracted error message from Custom Validation on a Visualforce Page?

I have added few custom validations using Configuration for an object. I am inserting that object record through visualforce page. I have added <apex:pageMessages/> on my visualforce page. I have also written code block for catching the exception and to show the error message ob VF page. Please find code block below :

catch(DMLException excp)
{
    ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, excp.getMessage() );
    ApexPages.addMessage(msg);  
    return null;                            
} 

Still I am not able to get only error message from the custom validation. It shows me error like below :

Insert failed. First exception on row 0; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, error_message_from_custom_validation_comes_here

Is there any solution for this?

like image 575
kshitij Avatar asked Jul 23 '12 05:07

kshitij


People also ask

How do I display validation error message in Visualforce page?

Show error message in Visualforce Page using ApexPages. addmessage Sometime we need to show error message on Visualforce page. We can implement this requirement by creating new instance of ApexPages. message and then adding message to Apexpages using ApexPages.

How do I display an error in Apex class in Salesforce?

You can display error messages using the addError method on sobject or its fields.


1 Answers

You need to get the DML message like so:

ApexPages.Message msg = new ApexPages.Message(Apexpages.Severity.ERROR, excp.getdmlMessage(0) );
ApexPages.addMessage(msg);

Using Exception Messages

like image 92
Greg Grinberg Avatar answered Sep 22 '22 10:09

Greg Grinberg