5 Replies Latest reply: Jul 2, 2013 1:59 PM by Tyson Jouglet RSS

    What is the correct way for invoking application process from Javascript?

    TamasSzecsy

      Before APEX 4.0 I used to invoke application processes from javascript by doing something like this:

       

      var ajaxRequest = new htmldb_Get(null,&APP_ID.,'APPLICATION_PROCESS=RESET_ADATLAP_COUNT',0);

          ajaxResult = ajaxRequest.get();

       

      Although htmldb_Get is not supported and not documented there are a dozen blogs that point into this direction. But from APEX 4.x on, htmldb_Get still works, but for example htmldb_Get.add does not and I would like to switch to a documented solution.

       

      Could you please help me with this? I am not too familiar with Javascript or Ajax, so please be easy on shorthand! Thanks.

        • 1. Re: What is the correct way for invoking application process from Javascript?
          Tyson Jouglet

          This will work in any version of apex where jquery is installed.

           

          var queryString = {
            p_flow_id: $('#pFlowId').val(), // app
            p_flow_step_id: $('#pFlowStepId').val(), // page
            p_instance: $('#pInstance').val(), // session
            p_request: 'APPLICATION_PROCESS=RESET_ADATLAP_COUNT', //process
           f01: ['1','2'], // set a global array to something
            x01: 'SOME_VALUE' // set a global variable to something
          }
          
          $.ajax({
            type: 'POST',
            url: 'wwv_flow.show',
            data: queryString,
            dateType: 'text',
            async: true, // setting this to false will behave the same as htmldb_get, but async: true will not sit and wait for a response if the server is slow. it will be processed when the request is completed
            success: function(data){
               //TODO do something with the result(if there is any)
             }
          });
          

           

          If you are using apex 4.2 you can have most of this encapsulated into apex.server.process though th structure of the call seems a little wonky.

           

          apex.server.process ( 
            "RESET_ADATLAP_COUNT", 
            { x01: "SOME_VALUE",
              pageItems: "#P1_DEPTNO,#P1_EMPNO"
            }, 
            {
             success: function( pData ) { ... do something here ... }
          });
          

           

          Good Luck,

          Tyson

          • 2. Re: What is the correct way for invoking application process from Javascript?
            TamasSzecsy

            Thanx. One more thing. I am using session state protection for the page. How could I generate the checksum value for the above example? I am running on APEX 4.1.0.0.23.

            • 3. Re: What is the correct way for invoking application process from Javascript?
              Tyson Jouglet

              Dynamic Action of type execute PL/SQL would be the best fit. Make that application process a database procedure and whatever items are referenced will become parameters into the procedure. Technically you do not have to make that a procedure and you could copy the code from one place to the other, but i highly recommend not copying code  as that is how you get weird bugs in your app.

               

              Good Luck,

              Tyson

              • 4. Re: What is the correct way for invoking application process from Javascript?
                TamasSzecsy

                Tyson, generally this would be a good solution, but in the case of a custom Help it would mean I would need to create a Dynamic Action for all pages in my application and provide all items on the given page for the Dynamic Action. A template based solution would be  more generic and good way to do.

                 

                I had a look at the original APEX code for Help, it looks like this. It looks great, the only problem is that I do not know how to generate JSON results, since apex_util.json_from_sql is only available only from APEX 4.2 and the wwv_flow_item_help.show_help procedure is not visible to me.

                 

                function popupFieldHelp(pItemId, pSessionId){

                    // Show jQuery div based dialog if not running in screen reader mode, if not fall back to old popup

                    if (!$x('pScreenReaderMode')) {

                        apex.jQuery.getJSON(

                        'wwv_flow_item_help.show_help?p_item_id=' + pItemId + '&p_session=' + pSessionId + '&p_output_format=JSON',

                        function(pData){

                          var lDialog = apex.jQuery("#apex_popup_field_help");

                          if (lDialog.length===0) {

                            // add a new div with the retrieved page

                            lDialog = apex.jQuery('<div id="apex_popup_field_help">'+pData.helpText+'</div>');

                            // open created div as a dialog

                ....

                • 5. Re: What is the correct way for invoking application process from Javascript?
                  Tyson Jouglet

                  Hey Tamas,

                   

                  there is a substitution string called "CURRENT_ITEM_HELP_TEXT" you can just build that into your item template and not make an ajax call at all if that is what you are looking for. Just have the supporting JS/CSS in a page 0 or global page dynamic action.

                   

                  Thanks,

                  Tyson