Revolutionize Your APEX Apps with Background Processes and Collections – See How It’s Done!
with Collections
Handling Oracle APEX Collections with Background Processes: A Quick Tip
It's important to understand that Oracle APEX collections are typically tied to a specific user session. Recently, I encountered an issue when I tried populating a collection through a background process. Once the process finished, I checked the collection, but no data was found. Oddly enough, the same logic worked perfectly when tested in SQL Developer.
The Quick Fix:
The key here is that background processes run with a different session ID. As a result, the collection is populated within the background process's session—not the user’s session. To address this, you need to ensure that you handle the session context properly.
First adjust your PL/SQL Procedure:
PROCEDURE fill_collection(
p_app_id in number
, p_page_id IN NUMBER
, p_session_id IN NUMBER) AS
v_sql CLOB;
BEGIN
-- Attach to APEX session !!
apex_session.attach( p_app_id => p_app_id
, p_page_id => p_page_id
, p_session_id => p_session_id);
v_sql := 'select * from dual';
apex_collection.create_collection_from_queryb2(
p_collection_name => 'MY_SESSION_MY_COLLECTION'
, p_query => v_sql
, p_truncate_if_exists => 'YES') ;
apex_session.detach;
EXCEPTION
WHEN OTHERS THEN
apex_session.detach;
RAISE;
END;
2nd Save your Session ID:
On Page itself (eventually Page 0) you need following on Page Load:
on Process we cannot use directly APEX_CUSTOM_AUTH.GET_SESSION_ID so we need this Item.
3rd)
Use in the Background Prozess (INVOKE API in my case) with parameter p_session_id etc.
Result)
After the Process was finished now our Interactive Grid was filled with data:
Example SQL:
IG Region with this Query gives me now a result..
select *
from apex_collections_dml
where collection_name = 'MY_SESSION_MY_COLLECTION';
Happy Trying!