APEX – Getting back to where you came from

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 :

The APEX Page Component View

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 PointOn Submit : Before Computation( Before Computation, Validation, and Processing)
Branch TypeBranch to Page Identified by Item ( using Item Name)

Lots of trees in MK...branches everywhere

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.

About these ads

3 thoughts on “APEX – Getting back to where you came from

  1. 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.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s