Thursday, May 9, 2019

Generate APEX URL in JavaScript

When developers want to generate APEX URL from a JavaScript the thing that most of them will do is to concatenate URL string like this:

var vItemValue = 'New Item Value';
// Example 1 - get URL to set item value on page 1 with value from variable vItemValue
var vUrl = "f?p=" + $v( "pFlowId" ) + ":1:" + $v( "pInstance" ) + "::" + $v( "pdebug" ) + "::P1_ITEM:"+vItemValue;
// Example 2 - get URL to set item value on current page with value from variable vItemValue
var vUrl = "f?p=" + $v( "pFlowId" ) + ":" + $v( "pFlowStepId" ) + ":" + $v( "pInstance" ) + "::" + $v( "pdebug" ) + "::P1_ITEM:"+vItemValue;
// redirect to generated URL
apex.navigation.redirect(vUrl);
But there's one (unfortunately) undocumented feature that you can use to make it easier. It's possible to do it by using apex.util.makeApplicationUrl function that excepts a JavaScript object as the only parameter. In that object, you can define all that is needed to generate an APEX URL.

Here is an example:

var vItemValue = 'New Item Value';
// Example 1 - just get URL to go to the page 1
vUrl = apex.util.makeApplicationUrl({pageId:1);
// Example 2 - go to page 1 and set value of item P1_ITEM
vUrl = apex.util.makeApplicationUrl({
pageId:1,
itemNames:['P1_ITEM'],
itemValues:[vItemValue]
});
// Example 3 - you can also set multiple items
var vItem2Value = 'New Item 2 Value';
vUrl = apex.util.makeApplicationUrl({
pageId:1,
itemNames:['P1_ITEM', 'P1_ITEM_2'],
itemValues:[vItemValue, vItem2Value]
});
// Example 4 - all options
vUrl = apex.util.makeApplicationUrl({
appId: 100, // default is $v("pFlowId") - current app
pageId:1, // default is $v("pFlowStepId") - current page
session: $v( "pInstance" ), // default is $v("pInstance") - current session
request: 'TEST_REQUEST', // default is $v("pRequest") - current request
debug: 'YES', // default is $v("pdebug") - debug YES/NO
itemNames:['P1_ITEM'], // item names array (no value by default)
itemValues:[vItemValue], // item values array (no value by default)
printerFriendly: 'YES' // no value by default
});
Note: the function will not generate the checksum for pages or items with session state protection turned on.

Enjoy!

Tested in APEX 19.1.0.00.15