Mittwoch, 6. September 2017

Call Apex Modal Dialog from Javascript

         There are several possibilities to call Javascript Dialog in Javascript. For example jquery-confirm can be called directly from Javascript. But Apex Modal dialog can not be called directly. What can you do to access Apex Modal Dialog in Javascript?

1- Add the following function to Function and Global Variable Declaration


var getUrl = function() {
    var url = 'f?p=#APP_ID#:53:#SESSION#::NO:RP,53:P53_ITEM:#P_ITEM#';

    url = url.replace('#APP_ID#', $v('pFlowId'));
    url = url.replace('#SESSION#', $v('pInstance'));
    url = url.replace('#P_ITEM#', $v('P52_ITEM'));

    return url;
};


53 is the page number of the Apex modal dialog

P52_ITEM is one Page Item in the parent page


2- Create on click DA to execute the following javascript function

var url = getUrl();
apex.server.process("PREPARE_URL", {
    x01: url
}, {
    success: function(pData) {
        if (pData.success === true) {
            apex.navigation.redirect(pData.url);
        }
    },
    error: function(request, status, error) {
    }
});


PREPARE_URL is the name for the callback procedure that must prepare the URL

3- Create a AJAX callback process with the following code

declare
 result   varchar2(23333);
 begin
 result:=apex_util.prepare_url(apex_application.g_x01);
    apex_json.open_object;
    apex_json.write('success', true);
    apex_json.write('url', result);
    apex_json.close_object;
exception
    when others then
        apex_json.open_object;
        apex_json.write('success', false);
        apex_json.write('message', sqlerrm);
        apex_json.close_object;
 end;



result:=apex_util.prepare_url(apex_application.g_x01); prepare the URL with all parameters.


Kommentare:

  1. Hello Pierre

    Two things to mention.

    1- Try to avoid "hardcoding" as much as possible. What I mean is that you should try to have generic functions that are going to be reusable from page to page or application to application.
    In your example, you could wrap the DA code into a function that could be used everywhere in your application and not only on page 52
    Something like:
    function openModalDialog(pUrl){
    ...
    }

    then the DA could be replaced by a call to
    openModalDialog('f?p=&APP_ID.:53:&SESSION.::NO:RP,53:P53_ITEM:' + apex.item('P52_ITEM').getValue())

    2- I did a dynamic action plugin to open a modal which does the same as your code with a couple of extra options
    you can find here:
    https://apex.world/ords/f?p=100:710:::::P710_PLG_ID:CA.MAXIMET.APEXDIALOGOPEN
    and/or:
    https://github.com/maxime-tremblay/apex-plugin-dialogopen

    You won't need to add any page item, JS code or anything else on each page.

    Regards
    Maxime

    AntwortenLöschen
  2. Hey Maxime,

    thanks for give me a Feed back. I have make the Code sample that it can be use in more Page.

    1- as you say i implement a Function in Function and Globale Variable Declaration like so or we can put it in Globale Page 0. That means we cann use it in any Page

    var openModalDialog = function(process,url){

    apex.server.process(process, {
    x01: url
    }, {
    success: function(pData) {
    if (pData.success === true) {
    apex.navigation.redirect(pData.url);
    }
    },
    error: function(request, status, error) {
    }
    });

    };


    2- Than in DA just call that Function with 2 Parameter. The Name for the Callback and the Url

    openModalDialog("PREPARE_URL",'f?p=&APP_ID.:53:&SESSION.::NO:RP,53:P53_ITEM,P53_ITEM_:'+$v('P52_ITEM')+','+$v('P52_ITEM_'));

    I think it is more sample as you say Maxime.


    I never use that Plugin. But i think i will have a Look at that today.

    Thanks again
    Pierre

    AntwortenLöschen
  3. Hi Pierre,

    There needs to be a HUGE warning here. apex_util.prepare_url should never be exposed to an ajax call like that, or you defeat the whole security of your application.
    Anyone can build a link and obtain the checksum and visit pages with data that they should not be visiting.

    Please be really careful of where you implement this and the type of application.

    AntwortenLöschen
  4. Hey Jorge,

    thanks for give me your Feedback. You are true. It will defeat the whole security of the application. I have try the Solution for John and it works fine for me.

    Thanks again
    Pierre

    AntwortenLöschen