+ Start a Discussion
Joseph BaroneJoseph Barone 

VisualForce List verify data before inserting list

Hello All,

I have created a VisualForce page with a list that allows the end user to enter the values into the fields all at once.  At the end of each row, they have an option to add another row or to save all the data.  

In standard view, I have a verification rule on one of the Field X that makes sure if Field Y equals a specific value, then Field X cannot be Null.  This works great in the Standard view; however throws an exception error when entering through the VF list.  

I am not sure how to write a code for the VF list that verifies that Field X is not Null when Field Y equals a specific value.  Moreover, I have my list listNewTime and I do not know how to access the data temporarily being stored there prior to running the insert command.  

Thank you in advance. 
Best Answer chosen by Joseph Barone
MJ Kahn / OpFocusMJ Kahn / OpFocus
It sounds like the exception is happening when your controller does its insert statement, because your validation rule is detecting the error. If you wrap a try/catch block around the insert statement, and then catch the exception, you can display a more friendly error message on your page, rather than the ugly exception message that gets displayed for uncaught exceptions.

For an example, see the Visualforce Developer's Guide (https://resources.docs.salesforce.com/sfdc/pdf/salesforce_pages_developers_guide.pdf (https://resources.docs.salesforce.com/sfdc/pdf/salesforce_pages_developers_guide.pdf" target="_blank)), in the section titled "Building a Custom Controller." Toward the bottom of that section, there's an example of a save() method that does an upsert operation, with a try/catch block around it.

If you do this, don't forget to include an <apex:pageMessages/> block in your page. If you don't include it, your page's messages won't display.

 

All Answers

MJ Kahn / OpFocusMJ Kahn / OpFocus
It sounds like the exception is happening when your controller does its insert statement, because your validation rule is detecting the error. If you wrap a try/catch block around the insert statement, and then catch the exception, you can display a more friendly error message on your page, rather than the ugly exception message that gets displayed for uncaught exceptions.

For an example, see the Visualforce Developer's Guide (https://resources.docs.salesforce.com/sfdc/pdf/salesforce_pages_developers_guide.pdf (https://resources.docs.salesforce.com/sfdc/pdf/salesforce_pages_developers_guide.pdf" target="_blank)), in the section titled "Building a Custom Controller." Toward the bottom of that section, there's an example of a save() method that does an upsert operation, with a try/catch block around it.

If you do this, don't forget to include an <apex:pageMessages/> block in your page. If you don't include it, your page's messages won't display.

 
This was selected as the best answer
Joseph BaroneJoseph Barone
Thank you!  This worked perfectly.