Coding Duchess
Coding Duchess

Reputation: 6919

Oracle APEX page item set in an ajax callback does not persist

I have the following code in my ajax callback (PL/SQL):

:P1_CNT := TO_NUMBER(:P1_CNT) + 1;
apex_util.set_session_state
     (p_name  => 'P1_CNT'
     ,p_value => :P1_CNT
     );

Which seems to work just fine. But then a process is called that checks that page item and page item comes up as 0 even though in the callback it is set to 1. How can I fix that?

The code that calls the ajax is javascript below, executed from a custom dynamic action:

for ( var i=0; i<records.length; i++) {
        apex.server.process
            ("my_ajax_callback"
            ,{x01:records[i][1]}
            ,{type:'GET', dataType: 'text', success: function( text) {}}
            );
}


apex.page.submit( 'COMPLETE_PROCESS_RECORDS' );

Where COMPLETE_PROCESS_RECORDS is the process that executes once all the records in the loop have been processed by ajax callback. the ajax callback evaluates each record passed to it and processes some and discards others. P1_CNT is incremented every time a record was processed further.

Upvotes: 1

Views: 2932

Answers (1)

Dan McGhan
Dan McGhan

Reputation: 4659

You haven't shown us the code that calls the Ajax callback, but basically, there's server-side (PL/SQL in this case) and client-side (JavaScript) code. For the server to get values from the client-side, you have to send them in when calling the Ajax callback. That's what the pData parameter is for: https://docs.oracle.com/en/database/oracle/application-express/19.2/aexjs/apex.server.html#.process

You can access the values you send to the server-side code in different ways depending on how you send them in. For example, if you send in a value with x01, you can refer to it in your PL/SQL code with apex_application.g_x01.

Of course, sometimes you need to get values from the server-side to the client-side. For this, typically you'd send an HTTP response from your PL/SQL code. Here's an example that sends a JSON object:

apex_json.open_object();
apex_json.write('hello', 'world');
apex_json.close_object();

You would then need to update your client-side code to look at and use the HTTP response to map the values to whatever part of the page/DOM you need.

This is so typical that the APEX team made it very simple if you're using the Dynamic Action framework instead of raw JavaScript. There's an action named Execute PL/SQL that has attributes named Items to Submit and Items to Return that can do the heavy lifting for you.

Execute PL/SQL

Upvotes: 2

Related Questions