For those of us who have been developing in APEX for a while will understand that the value of an item in APEX is not always what it seems. That’s right; an item has two values in APEX. The first value is the session value that we can store in the database (server side), the second value is the value the end user sees on his screen in the browser (client side). On some occasions these values are the same. On others they are not. The difference in value can lead to some confusion, especially for the new APEX developers.
Let’s start with an example so we understand the problem. This is actually based on a real use case. I changed the item names and queries to make it more universal, but the principle remains the same. The developer in question had a page with two Select lists containing the following queries:
SELECT 1 d, 1 r
SELECT 1 d, 1 r
SELECT 2 d, 2 r
The second select list (called P55_SELECT_LIST_2), also had a default value of 2.
When the user selects a different value on the select list then the second select list had to change values accordingly.
In order to achieve this the developer had created a Dynamic Action. As event he had a change Event of the P55_SELECT_LIST item. The first action was a set value containing the following query (again made simple for the example):
WHERE 1 = :P55_SELECT_LIST
And page items to submit he had selected the item P55_SELECT_LIST, and affected items he put P55_SELECT_LIST_2
The last action he set was a refresh action of our P55_SELECT_LIST_2 item.
Our page setup now looks like this:
The developer tried his application, and when he changed the first select list he saw the second select list being refreshed. Yet, instead of displaying the correct value (1), it displayed the default value.
So what went wrong?
Let’s see what exactly happened to figure that out:
- The page was loaded. The first select list had nothing selected, the second select list had “2” selected in the browser. Both items were empty in the session
- The value of the first select list was changed by the end user
- This triggered the change event defined in the dynamic action
- The first action “Set value” was executed. The value in the browser was changed. The session value of the item P55_SELECT_LIST was set to one, because it was set with items to submit. An AJAX request was made to set this value. The value item P55_SELECT_LIST_2 remains NULL.
- The refresh action was executed; an AJAX request was made to get new values for the select list. Since the SELECT_LIST_2 item has no value in the session, the default value is taken, which is 2!