Oracle APEX cannot save a value into a session

Viewed 4109

I have a page item P2_ITEM_TYPE_ID that gets set in Pre-Render, using a process calling PL/SQL:

BEGIN
   select ITEM_TYPE_ID INTO :P2_ITEM_TYPE_ID from TABLE1 where ITEM_ID = :P2_ITEM_ID;
   exception
      when no_data_found then
        :P2_ITEM_TYPE_ID := null;
END;

Currently the record for that ITEM_ID does not exist in TABLE1 so the exception NO_DATA_FOUND is thrown and originally the ITEM_TYPE_ID is set to null.

Now I want to set the ITEM_TYPE_ID by clicking on one of the cards on the page, grabbing its ID and setting page item P2_ITEM_TYPE_ID to that ID.

I have a dynamic action that runs the following javascript when card is clicked:

var $item_type_id = this.data;
console.log($item_type_id); //prints out correect ID
apex.item("P2_ITEM_TYPE_ID").setValue($item_type_id);
 console.log(apex.item("P2_ITEM_TYPE_ID").getValue()); //prints out correct value

//set session state of P2_ITEM_TYPE_ID
apex.server.process ( "SAVE_HIDDEN_VALUE_IN_SESSION_STATE", 
{
     x01: $item_type_id,
     pageItems: "#P2_ITEM_TYPE_ID"
}, 
{dataType: 'text'} );

I know the code works, as the correct values get printed to the console, but when I check the session of the page, P2_ITEM_TYPE_ID is blank in session. What could be the problem?

It is almost as if something is preventing th value from changing. I have identical setup on another page with one small difference - the code in the pre-render does not include the exception part because there is always a record in TABLE1 for that ITEM_ID:

BEGIN
   select ITEM_TYPE_ID INTO :P3_ITEM_TYPE_ID from TABLE1 where ITEM_ID = :P3_ITEM_ID;
END;

But the rest of the code is identical and session of P3_ITEM_TYPE_ID changes without an issue

2 Answers

You don't need apex.server.process for this, you can post the value of an item to the server with an Execute PL/SQL Code action that follows the javascript action you already have. Put the item name (e.g. P2_ITEM_TYPE_ID) in the Items to Submit attribute (set PL/SQL Code to just null;).

If everything is the same on those pages, except the EXCEPTION part, well - there's a workaround: use an aggregate function, e.g.

select max(ITEM_TYPE_ID) INTO :P2_ITEM_TYPE_ID from TABLE1 where ITEM_ID = :P2_ITEM_ID;

It will prevent the query from returning NO_DATA_FOUND if there's no value for that :P2_ITEM_ID and will store NULL into the :P2_ITEM_TYPE_ID.

Related