Oracle Apex中通过PL/SQL分支获取动态创建字段值的问题
Hey there, let's sort out this issue with grabbing values from your dynamically created fields in the PL/SQL branch! The :NAME syntax works great for standard static items, but dynamic items need a different approach since they aren't registered in the session the same way. Here are the most reliable methods to fix this:
1. Use Apex's Built-in g_fxx Arrays
When you create dynamic items using Apex's native APIs like apex_item.text() or apex_item.select_list(), their values get stored in global arrays apex_application.g_f01 through apex_application.g_f50. The number at the end corresponds to the p_idx parameter you set when creating the item.
For example, if you created your dynamic text fields with p_idx => 1, you can fetch their values like this in your loop:
DECLARE tabs INTEGER := :P2_CREATE_TABS; lines INTEGER := :P2_LINES; counter NUMBER := 1; content VARCHAR2(100); BEGIN INSERT INTO EmailContent (CUSTOMERNAME, ORGANISATIONNUMBER, CONTACTNUMBER, EMAIL) VALUES (:P2_CUST_NAME, :P2_ORG_NUM, :P2_CONTACT_NUM, :P2_EMAIL); -- Loop through your dynamic fields stored in g_f01 FOR tab_cou IN 1..tabs LOOP FOR line_cou IN 1..lines LOOP -- Grab the value from the global array content := apex_application.g_f01(counter); -- Do something with the value (e.g., insert into another table) -- INSERT INTO your_dynamic_table (content_col) VALUES (content); counter := counter + 1; END LOOP; END LOOP; END;
Just make sure the p_idx you used when creating the items matches the array you're referencing (g_f01 for p_idx=1, g_f02 for p_idx=2, etc.).
2. Fetch Values with apex_util.get_session_state()
If you gave your dynamic items explicit, unique names (like P2_DYNAMIC_1, P2_DYNAMIC_2), you can use this function to retrieve their values by name. This is handy if you don't want to rely on array indexes:
content := apex_util.get_session_state('P2_DYNAMIC_' || counter);
This works as long as your dynamic items have their name attribute set correctly when you create them.
3. Double-Check Your Dynamic Item Setup
Before diving into the PL/SQL, confirm your dynamic items are configured to submit their values:
- If you're using
apex_itemAPIs, ensure you're not missing thep_idxparameter (this is what links the item to theg_fxxarray). - If you're creating items manually (via HTML), make sure the
nameattribute is set, and the items are within a region that's included in the page submission. - Avoid setting dynamic items to "Read Only" unless you specifically don't want their values submitted.
内容的提问来源于stack exchange,提问作者Adi

