Monday, April 16, 2018

Opening Modal Dialog page using JavaScript

In APEX, when there is a requirement for opening pages dynamically JavaScript must be used. Normal pages can be easily opened in JavaScript’s by mentioning the URL using window.location, but Modal Dialog cannot be opened. URL will not be framed correctly for Modal Dialog pages.

Below code can be used for opening dialog pages dynamically using JavaScript’s in APEX.

Sample Code:

/*Frame the URL parts separately for passing parameters dynamically.
URL with App_ID,Page_ID,Session_ID */
var c = "f?p=&APP_ID.:1398:&SESSION.";
/*Pass the Input Parameters with values
To escape the special characters which are not allowed in URL, JavaScript escape() function can be used. */
var d = '::NO::P1398_REGION_CODE,P1398_REPORT_OPTION,P1398_DPD_DAYS,P1398_ASSET_CLASS,P1398_REPORT_MONTH,P1398_CONTRACT_TYPE,P1398_SORT_ORDER,P1398_PREV_MNTH_MINUS_1,P1398_PREV_MNTH_MINUS_2,P1398_PREV_MNTH_MINUS_3,P1398_CONTRACT_FY_START_DATE,P1398_SEGMENT_CODE,P1398_SUB_SEGMENT,P1398_SEGMENT_CODE_NAME,P1398_SUB_SEGMENT_NAME:'+strRgnCode+","+$x("P1392_REPORT_OPTION").value+","+$x("P1392_DPD_DAYS").value+","+$x("P1392_ASSET_CLASS").value+","+$x("P1392_REPORT_MONTH").value+","+$x("P1392_CONTRACT_TYPE").value+","+"DPD_ASC"+","+$x("P1392_PREV_MNTH_MINUS_1").value+","+$x("P1392_PREV_MNTH_MINUS_2").value+","+$x("P1392_PREV_MNTH_MINUS_3").value+","+$x("P1392_CONTRACT_FY_START_DATE").value+","+$x("P1392_SEGMENT_CODE").value+","+$x("P1392_SUB_SEGMENT").value+","+escape(strSegmentName)+","+escape(strSubSegmentName);
/*For opening dialogs/pages using JavaScript apex.navigation.dialog must be used*/
var e = ("javascript:apex.navigation.dialog('f?p=&APP_ID.:1398:&SESSION.").length;
var f = c.slice(0,e)+d+c.slice(e);
f=f.replace(":::::",":");
console.log(f);   
window.location.href=f;

7 comments:

  1. This is awesome! Thank you so much! This has been frustrating me for hours, but this bit of code was great.

    ReplyDelete
  2. A big thank you! Maybe you could simplify your example with only one input parameter and value, because at first sight it's scary ;-)
    PS: I replaced $x("Pxx_item_name").value by $v("Pxx_item_name")

    ReplyDelete
  3. I noticed that in case one of the value has an underscore then it gets cropped.hence 404 error
    for eg: strRgnCode = "ENF_HJ_GH"
    then the link contains like ENF_ only

    ReplyDelete
  4. it's not _ its '&' which is causing the url to get cropped after navigation

    ReplyDelete
  5. Waao, that's really amazing. It solved a lot of problems for my reusable dialog pages.

    ReplyDelete
  6. thank you.. so so much....that was really very helpful...

    ReplyDelete