+ Start a Discussion
Jerun JoseJerun Jose 

Read validation rule error message through apex

Hi,

 

I have a somewhat strange situation.

I have a validation rule say V1 setup on the Opportunity object that work as expected. This validation checks if some Account fields are filled in for any Closed-Won opportunity.

 

I also have a visualforce page that is used to move the Opportunity stage to Closed Won. In that page, I want to display the error message from V1 rule if the rule formula fails. I cant use a try catch block because, before some integration is involved before the update and the missing data corrupts the other system data.

So basically, is there some describe method or something where we can access the Validation rule metadata and get the error message. The reason I want the error message from the rule is because it comes with the translations as well. I dont mind recoding the validation formula, but I am keen in accessing the translated error messages.

 

Thanks,

 

Jerun Jose

Anup JadhavAnup Jadhav

Hi Jerun,

 

You can always you <apex:pageMessage> to display the error message. If the validation rule works, then when you try to submit the form or "save" the object, it will display the appropriate validation error message.

 

Check out the visualforce documentation for more details.

 

Hope this helps!

 

- Anup

Jerun JoseJerun Jose

Thanks, but in my case the validation rule itself will not fire, since I am not doing a save before making the callout.

So although pagemessages will display the validation error eventually, I would have made the callout in the meantime which would have corrupted the external data.

 

That is why I am trying to move the validations ahead of the callout and before the save. Like I said, I dont mind redoing the logic for the validation formula, but I need to be able to pull the validation error message in the user's language.

 

Thanks,

 

Jerun Jose

Anup JadhavAnup Jadhav

In that I'd tie the callout to the save functionality instead of doing it the other way round. i.e Do the save, check for error message and then do the callout.

 

Hope this helps!

 

- Anup

Jerun JoseJerun Jose

The opportunity save will use some data from the callout. Okay, here the complete detail.

 

I have to sync oppty data with external system. On click of a button, I make a callout to the external system. Using the response I update the opportunity data. Now the external system has some mandatory info. If these are missing in the opportunity then I would like to throw an error message. I already have validation rules setup to preven these fields from going missing on a synced opportunity. Is it possible error messages from these validation rules on the VF page?

Anup JadhavAnup Jadhav

Yes, it is still possible to display the error messages using <apex:pageMessages> on the visualforce page. You have to ensure that the validation rule covers all of the fields that are mandatory on the opportunity object and in the corresponding external system table. Then when the "button" is clicked, try to save the opportunity first, and then do the callout to the external system.

 

This will ensure that the validation rule prevents saving the opp if the required fields are missing or contain incorrect data. It will also display the validation error message in the <apex:PageMessage> compoent (check out the VF documentation for more details). The added advantage is that you don't waste a callout to an external system if the details in the opp object are incorrect.

 

   - anup