Oracle Apex: Responding (success/failure) to a call to Apex.server.process

Requirement

React to a Javascript call to apex.server.process

In my example, at the end I'll call an inline dialogue after processing.

Solution

Create a Javascript dynamic action. In my example it is initiated with a button.



Javascript Code.

var $wP;
var displayError = function(error) {
    console.error("Promise is rejected:", error);
};

var call_block1 = function() {
    $wP = apex.widget.waitPopup();
    return "End of Block 1";
};

var call_block2 = async function() {
    return apex.server.process( "MY_CALLBACK", {pageItems: "#P_TEXT_IN"}, {dataType: "text"} );
};

var call_block3 = function() {
    $wP.remove();
    return "End of Block 3";
};

async function call_refresh() {   
    try {
        var result1 = await call_block1();
        console.log('Phase: ', result1);
        var result2 = await call_block2().then(function(pData) {
                              var v_result = JSON.parse(pData).result;
           
                              if (JSON.parse(pData).success == true){
                                    apex.item("P_TEXT_OUT").setValue(v_result);
                                    alert("Success! Result = " + v_result);
                              } else {
                                    alert("Error! " + v_result);
                              }});
   
        console.log('Phase: End - Block 2');
        var result3 = await call_block3();
        console.log('Phase: ', result3);
    } catch(e) {
        $wP.remove();
        console.error(e);
        alert('Error!');
    } finally {
        console.log('Do something no matter what');
        var dummy=apex.theme.openRegion( 'STATIC1' );
    }
};

call_refresh();

Explanation

This defines and executes a procedure called "call_refresh".

It accepts and returns a variable which is displayed as an alert for demo purposes.

It demonstrates how code can be deliberately executed after waits, where to put code in case of error, where to put code on success, and even code that gets executed regardless of failure. In my case to open an inline  modal dialog with static ID of STATIC1.

Here is the code for the application process called MY_CALLBACK.

DECLARE
   v_parameter_in   VARCHAR2(100);
   v_result         VARCHAR2(100);
BEGIN
  v_parameter_in := :P_TEXT_IN;

  SELECT v_parameter_in || '-COMPLETED'
    INTO v_result
    FROM DUAL;

    apex_json.open_object;
    apex_json.write('success', true);
    apex_json.write('result', v_result);
    apex_json.close_object;
   
EXCEPTION WHEN OTHERS
THEN
    apex_json.open_object;
    apex_json.write('success', false);
    apex_json.write('result', SQLERRM);
    apex_json.close_object;
END;


This shows how you would make use of incoming variables (although there ware other ways to pass variables to an application process). Play with the SQL to force errors and see how it behaves.



Acknowledgement

Pass value to and from a callback process - Oracle Forums

Comments