In most cases error messages are shown on an APEX page when the user submits a form and a validation fails. Sometimes (not often) you may need to manually control the validations rather than using the declarative validations section. To do this you can use the
apex_error API. The following is an example of how to call the API.
This will generate the following on the page:
apex_error.add_error triggers a rollback and in most cases this is a good thing. In some very specific cases you may not want a rollback to occur. The rest of this article covers how to generate errors without a rollback occurring.
I recently had a rare edge case for a page submit and errors. The page was used to edit batch staging data. The requirements were:
- User submits page
- All the data is saved (in a staging table)
- Since this is staging area, the data must be saved and committed each time regardless of errors
- Do validations against staged data
- If errors were found, the page would show them (remember the data still needs to be saved)
- If no errors were found, the staged data would be posted and then branch to another page.
apex_error.add_error performs a rollback the simple solution is to save the staged data, perform an explicit
commit, and then do custom validations. Unfortunately this was not possible due to additional requirements that I left out of this article.
The following relies on the fact that you know about the
apex.message API. If you do not please read this article.
This is an overview of the final solution:
- On Submit PL/SQL page process that:
- Saved all the page’s data
- Called a custom validation function that returned a JSON object. If no errors were found this function would return
null. The results of this function were stored in the page item
- Created an
onPageLoadDynamic Action to display errors using the
The following pseudo code summarizes the above description:
err = apex.item('P1_ERR_JSON').getValue();
When an error occurred, the page looked like:
This solution is a summary of the final code. You can easily expand it to handle multiple error messages, associate with page items, etc.