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
HafaelDiasHafaelDias 

Displaying Validation error message in Visualforce pages

Hello!

 

I created a Visualforce page that have a Custom Object as a standardController. In that page I get one field from the Object and that field have a Validation rule. My question is:

 

How can I get the error message and display it in my own vf page?

 

'Cause when the Validation is activated, it shows a long and confusing (in the user point of view) message in the whole page. I'd like to show just a message in the Top of the page.

 

 Thanks!

 

Life Is Too Short to Remove USB Safely

Best Answer chosen by Admin (Salesforce Developers) 
davidjgriffdavidjgriff
You may want to use the Database.update(YourRecord,false) method and then check the results for any errors and pass them back to the page with ApexPages.addMessage().

All Answers

MTBRiderMTBRider

You can display error or informational messages to the user on a Visualforce page by adding the <apex:pageMessages/> tag to your visualforce page.  Usually you add this to the top of the page right under any commandbuttons that you have.

 

Then in your controller, you use the ApexPages.Message() and ApexPages.addMessage() methods to display the message like so:

 

ApexPages.Message msg = new ApexPages.Message(ApexPages.Severity.INFO, 'Here is some important information for you, Mr User!  So please read it!');
ApexPages.addMessage(msg);

 Let me know if that helps.

HafaelDiasHafaelDias

Thanks for the replying!!!

 

Unfortunately it wasn't what I was looking for. Let me explain better:

 

When I have a validation rule in a field and I 'm using it in a custom page, the error message do not have the same behavior when I using in a standard Object page, like:

 

I create a field in a Custom Object and put a Validation rule on it. When I'm in the Object standard page, the error message appears next of the field or in the top. But, when I'm using my custom page, the page is redirected to another error page with the message: 

 

Visualforce Error

 

System.DmlException: Update failed. First exception on row 0 with id xxxxxxxxxxxxxxxxx; first error: FIELD_CUSTOM_VALIDATION_EXCEPTION, "Error message: You can't do that!!": [Field]

 

I wanna get that error message and show in my page, without been redirected to that error page!

 

 

Btw, I didn't know this ApexPages.Message(). I'll use it in other occasion!! Thank for that!!!!

MTBRiderMTBRider

I don't think I have been in that situation before and have seen what you are seeing.  You may be able to use the .hasMessage and .getMessage (page 340 in the Apex development guide) methods to determine if the validation message is firing and then do what you want with it.

 

The best alternative may be to get rid of the field validation that you created through the Salesforce UI and just validate the field in the controller's Apex code.  Then you have more control of what happens in terms of the messaging to the users, what screen is shown to them, etc.

HafaelDiasHafaelDias

I'm trying don't validate in my controller.

 

I have some others validations to do and I'd like to let my code as simple as possible...

 

Thanks for the help MTBRider!

 

 

 

davidjgriffdavidjgriff

Do you have the <apex:pageMessages/> tag in your page?

HafaelDiasHafaelDias

Yes, I do!

 

But the problem isn't showing the message. It's to get the validation message error and then show it!

davidjgriffdavidjgriff
You may want to use the Database.update(YourRecord,false) method and then check the results for any errors and pass them back to the page with ApexPages.addMessage().
This was selected as the best answer
HafaelDiasHafaelDias

Sorry for all those days without posts but here I am!

 

Thank you davidjgriff, that was what I was looking for! That is my code:

 

Database.SaveResult[] results = Database.update(MyRecord,false);
        
        if (!results[0].isSuccess()) {
         Database.Error[] errs = results[0].getErrors();
         ErrorMessage=errs[0].getMessage();
         }else{
        
        SuccessfulMessage='Record Updated!';
       }

 

Probably It isn't the best code but it works!

 

Thanks for all.