APEX_ERROR use case
When you enter data on a page we often think about APEX Validations when we want to validate our inputs and give errors to the user. (i.e. Value is required. Dates are not within range, etc…)
However, sometimes you may not be able to report all errors with a validation. Or perhaps the code you need to call performs validations and processing all in once transaction like in the case of a 3rd party API. Enter APEX_ERROR.ADD_ERROR
From the documentation.
ADD_ERROR Procedure: This procedure adds an error message to the error stack that is used to display an error on an error page or inline in a notification. It can be called in a validation or process to add one or more errors to the error stack.
For example, if you’ve used some of the Oracle eBusiness Suite API, you know that it could take a lot of code to setup and call one of these API. Then, the API may return with errors that you may want to display back on your page. If you don’t handle them correctly, your APEX page will just display a single error or exception.
Lets say you have an error record type that will hold multiple errors:
type message_rec_type is record ( message_text varchar2(4000) -- ... other attributes ... ); type message_tbl_type is table of message_rec_type index by binary_integer;
Then, if our API call returns a bunch of errors in a message_tbl_type structure, we can put those on our APEX page like so:
for i in 1 .. l_msg.COUNT loop apex_error.add_error(p_message => l_msg(i).message_text , p_display_location => apex_error.c_inline_in_notification); end loop;
For a quick and dirty example, I created a Page Process with this code:
apex_error.add_error( p_message => 'Your date range overlaps with other periods.' , p_display_location => apex_error.c_inline_in_notification ); apex_error.add_error( p_message => 'The category combination is invalid for the selected item.' , p_display_location => apex_error.c_inline_in_notification );
Submit the page and the result is: