One great thing about working with the APEX dev team is that I regularly find out that APEX is even better than I think it is. This happened again recently.
Whenever you have to manually generate a page link (usually in a PL/SQL region, though there are other places where this comes up), you ought to call the function apex_util.prepare_url(…). This is a straightforward function that takes the URL that you pass it and does a few things–most importantly, it adds any checksums you need and properly wraps the URL if it’s opening a modal dialog page. The problem I’ve always had with it, however, is the argument to the function–it’s the full f?p=…. APEX URL, so I frequently write things like:
apex_util.prepare_url('f?p='||:APP_ID||':50:'||:SESSION||':::50:P50_ID,P50_NAME:'||l_id||','||apex_escape.html(l_name))
(Incidentally, this is a made-up URL. If you can derive P50_NAME’s value from P50_ID, you shouldn’t pass it in the link; I just wanted to show the pain of passing multiple values.) This is very long, ugly, and prone to error–I often forget exactly how many colons I need between the session ID and the clear cache parameter, for instance. Oh, and the observant among you will notice that we’re not passing the current debug status, which can be really annoying when testing the application; I could add it, but honestly, I hate the amount of concatenating I’m already having to do, and just can’t bring myself to add in more.
So, naturally, I put in an enhancement request to add a second footprint to this function, with separate parameters for each part of the URL and intelligent defaulting behavior, so I wouldn’t need to specify the app id, session, etc. Made a lot of sense to me. A day or two later, I got an email saying that Patrick Wolf had coldly rejected my request–because APEX already had this (guess it made sense to others as well). The reason I hadn’t been able to find it was that it’s not in apex_util; it’s over in apex_page.get_url(…). So the above code really should be:
apex_page.get_url( p_page => 50, p_clear_cache => 50, p_items => 'P50_ID,P50_NAME', p_values => l_id||','||apex_escape.html(l_name)) );
The checksums and mobile code are taken care of just like prepare_url, and all items that I don’t specify are defaulted exactly the way they should be. Once more, I find myself wondering: why didn’t I know about this sooner?
Thanks for sharing!
Is there still a reason to use PREPARE_URL instead of GET_URL ?
That’s actually a good question, and honestly, I can’t think of any reason, other than being too lazy to type in the names of the parameters. Which is a reason, but it isn’t a good one.
What about checksum?
As I mention at the end, get_url generates the checksum as well.
Good sharing!!
how can I condition the link in the column, that when its value is null it appears and otherwise it doesn’t.
nvl2 is my preferred method for that sort of thing.