Ah, sunny Milton Keynes. There’s no place like it. Nestling in the heart of England, halfway between London and Birmingham, my home town has plenty to recommend it.
Yes, many of my countrymen like to poke fun at the Concrete Cows that are Milton Keynes’ most famous landmark. However, the one irrefutable benefit of living here, especially if you tend toward the geeky, is that Milton Keynes is also the home of the National Museum of Computing, hosted in Bletchley Park.
One thing about Milton Keynes is the interminable roundabouts throughout the city. Just keep turning left at every roundabout and you’ll be guaranteed to end up back where you started.
The same however, cannot be said of APEX – at least, not without a little bit of work.
In the example that follows, we have a page in an APEX application that can be invoked from a number other pages. The target page has a back button to return to the page you just came from. The question is, how do you make the target page re-direct back to the correct calling page ?
It must be said that the solution that follows can best be described as crude but effective. I’d be interested to hear if you come up with a more elegant solution.
Anyway, here goes….
The target page
I’ve created an application which has a single blank page called Welcome_MK. This will be the target page.
First of all, let’s put something on the page.
In the component view, in the Regions section, hit the Create Icon. BTW – just in case you’re wondering, the component view looks like this :
Now to create the target page :
From here, click Next.
In the next page, select HTML and click Next
The title is Welcome to MK. Click Next
In the HTML Region Source, enter what should be the city’s motto :
Welcome to Milton Keynes where all roads lead to….another roundabout
Click Create Region
Add a hidden item
Back in the Component View, we need to create a hidden item. This will hold the number of the page we’ve come from to get here :
In the Items section hit the create icon
In the Create Item dialog select an item type of Hidden.
Click Next
Item Name will be PX_PREV_PAGE where ‘X’ is the number of the target page in your application. In my case it’s Page 1 so… P1_PREV_PAGE
For Region, select the Region you’ve just created ( in my case, Welcome to MK(1) 11).
Click Next
Accept the default values and once again, Click Next
Source Used – set to Always, replacing any existing value in session state
Click Create Item
Create the Back button
Back in the Component View, go to the Button Section and hit the create icon
Select the same region for your button as the one you’ve just created your hidden item in.
Click Next
Select Create button in a region position and Click Next
Button name is BACK
Label is the same
Click Create Button
Create the branch
Now for the “clever” bit. We’re going to create a Branch to dynamically re-direct to whatever page number is in the P1_PREV_PAGE hidden item.
In the Component View under Branches click the create icon.
Branch Point – On Submit : Before Computation( Before Computation, Validation, and Processing)
Branch Type – Branch to Page Identified by Item ( using Item Name)
Click Next
Identify Item – the hidden item you just created
Click Next
When Button Pressed – select the Back Button you created
Hit Create Branch
Now we’ve got the target page, we need a page to call it from…
The Calling Page
Click Create New Page
Select Page Type : Report
Select Interactive Report
I can’t think of a suitable page name so I’ll just accept the defaults and click Next
Nope – don’t want to use tabs – Next
Now for the select statement. Let’s keep it simple :
SELECT 'Turn Left' FROM dual;
Link to Single Row – No
Next…
Finish.
Now we want to make the one column in the report a link to our target page….
In the component view under Regions, click on the Interactive Report link.
In the Column Attributes section, click on the Yellow Pencil icon to edit the TURNLEFT column.
Under the Column Link Section, select an appropriate icon for the Link Text. I decided on the magnifying glass.
Target is a Page in this Application and enter the Page number of the target page.
Item 1 – the Name is the name of the hidden column we created in the target page.
The value is the number of this page.
Hit Apply Changes
….and again.
I realise that, at this point, Alan Turing is probably turning in his grave. The place that he made famous mentioned in the same breath as the callous and unthinking use of what he would call “magic numbers” ( that’s hard-coded values to anyone younger than me…which is probably most of you).
Time to give our quick ‘n’ dirty solution a test drive.
Start by running the report ….
Click on the magnifying glass icon (or whichever one you chose for your link) …
Now for the moment of truth…click the Back button.
Well, it does work. Like I say, it’s not the most elegant solution, but it’ll do for now.
This is probably the one thing that has frustrated me with Apex. When I find I’m putting a hidden “BK” item on almost every page, and every link now has to make sure it sets it (otherwise the user clicks “Back” and gets hit with the “page zero” bug), it makes me think that it’d be better to try to make the application all one-way: each page only has one entry point. Unfortunately, it isn’t always practical to have it this way. A better way of managing this would be a great feature request for Apex, I think.
Jeffrey,
I’m so relieved to hear that. I thought I was doing something horrible when there was a much simpler way to do it. Apparently not (or, hopefully, not yet).
Mike
Yeah – I’m not aware of any other simple way to do this. I first heard of this technique at an Apex presentation from Penny Cookson. It would be interesting to come up with some kind of more general solution. e.g. keeping the history of page IDs in a single application-level item, and calling a package function to manage it as a stack.
Although this article is two years old, I would like to suggest an improvement for future visitors:
Instead of hard-coding the page number in the link definition, use “&APP_PAGE_ID.” instead. This way you don’t have to change the page number in case you change your page id.
An easier way is to change all of the branches on the called page to return to a URL rather than a page. The URL to return to is: f?p=&APP_ID.:&PXXX_RETURN_PAGE.:&SESSION. where XXX is replaced with the hidden page item holding the return page number. You may also want to use the &APP_PAGE_ID variable to populate the return page number when calling the page.
Still relevant information even in release 5.0.3. Thanks for posting all those years ago. I did implement with Al W’s tips but APEX changed it into this anyway: Type page in this application Page: &P6_SOURCE_PAGE.
Great article. Followed OP’s advice and the advice of farbklex1 about using &APP_PAGE_ID. Worked like a charm.