QUESTION: How do I programmatically redirect to a different page within the application?
My button's Dynmaic Action based upon an Execute PLSQL Code block errors on redirection: "Ajax call returned server error ORA-20876: Stop APEX Engine for Execute PL/SQL Code."
ANSWER: Based upon the conversations below, and some testing, it is clear that PL/SQL can be used to redirect. However, it cannot be used in all places on a page where PL/SQL can be used. For example, the APEX_UTIL.REDIRECT syntax (with the stop_apex_engine) can be used without the error shown above when used in a "Page Processing" activity, such as a Page Branch, Pre-Rendering process, etc. However, it cannot be used in dynamic actions associated with things like click, change, etc. Obviously, this is not an exhaustive list of what will and won't work. As FAC586 suggests below, if you see the "AJAX" portion of the message with the "Stop APEX Engine..." part, you are probably in a code block that does not support PL/SQL redirection and might want to look into an alternative approach. His suggestion to look at branching resulted in the following sample.
So, for those who want to "conditionally" redirect, here is a primer. It can be pretty simple. Steps 1-5 are the critical part to understanding the solution. This example contains one Selection List and one code block. It should take less than 5 minutes to replicate.
In this example, the “condition” is what value I select from “Destination” list. Your condition(s) may be more complex, but this should suffice to illustrate the requirements. In this case, selecting “Primary” redirects me to the primary target page and selecting “Secondary” redirects me to the secondary target page.
Step 1: Create an application called TESTBED
Step 2: Add a Primary (Page 2) and Secondary (Page 3) page to the new application. Put whatever type of control or label you want on it to make it identifiable in the web browser (or refer to the URL when testing).
Step 3: On the Home page, select the Processing tab in the left navigation pane
Step 4: Right-click the Processing list item, select Create Branch and create a branch called OnGoToDestination
Step 5: Select the Behavior Type of Function Returning URL (Redirect) and add the following function that acts conditionally based upon what is in :P1_DESTINATION.
DECLARE
--Default The Destination To Null
urlDestination VARCHAR2(240) :=Null;
BEGIN
--If The Desired Target Is The Primary Target
IF :P1_DESTINATION = 'Primary' THEN
--Return The Link To Page 2 (TARGET_PRIMARY)
urlDestination := 'f?p=' || v('APP_ID') || ':2:' || v('SESSION');
--Else (Nested) or ElseIf Other Target (NOTE: ELSEIF is the preferred syntax, but it was generating syntax errors)
ELSE
--If The Desired Target Is The Secondary Target
IF (:P1_DESTINATION = 'Secondary') THEN
--Return The Link To Page 2 (TARGET_PRIMARY)
urlDestination := 'f?p=' || v('APP_ID') || ':3:' || v('SESSION');
END IF;
END IF;
--Return The Destination
RETURN urlDestination;
END;
The following steps are used to create the "condition" in :P1_DESTINATION needed to trigger the branch above (your steps will be modified to provide adequate feedback for the conditions you choose):
Step 6: Editing the Home page, right-Click Content Body and Create Region called Navigation Region
Step 7: Editing the new Navigation region, right-Click Navigation and Create Page Item called P1_DESTINATION
Step 8: Set the P1_DESTINATION identification type to Select List in the properties
Step 9: Under the List of Values property set the Static Values to “STATIC:Primary,Secondary“ without the quotes.
Step 10: Right-Click P1_DESTINATION and Create Dynamic Action called DestinationOnChange
Step 11: Under DestinationOnChange Dynamic Actions Create a TRUE Action of Submit Page type
Save and run the application
Once you have "conditional" redirection figured out in this sample, you can modify your conditions to suit your particular needs. Good Luck!
Rob