Wednesday, August 15, 2018

Replace deprecated apex_util.string_to_table (APEX 5.1/18.1)

Sometimes the Oracle APEX documentation announces some packages will become deprecated in a release. It's not that those packages are suddenly gone, but you should not use them anymore. Your code will run fine still, but in the future, APEX might take it out completely, so it's best to replace them with the new package.

One of those packages announced in Oracle APEX 5.1 that are deprecated, and which I used a lot, was apex_util.string_to_table.

For example, in APEX Office Print (AOP) we read the session state of page items and we have some code like this:

declare
  l_string          varchar2(4000) := 'P1_X:P1_Y';
  l_page_items_arr  apex_application_global.vc_arr2;
begin  
  l_page_items_arr := apex_util.string_to_table(p_string => l_string, p_separator => ':');
  for i in 1..l_page_items_arr.count
  loop
    sys.htp.p(l_page_items_arr(i)||':'||apex_util.get_session_state(l_page_items_arr(i)));
  end loop;
end;    

As the function is deprecated and APEX is already on release 18.1, it's good to start replacing those calls. The new function you can use is apex_string.split.

The above code becomes then:
declare
  l_string          varchar2(4000) := 'P1_X:P1_Y';
  l_page_items_arr  apex_t_varchar2;
begin  
  l_page_items_arr := apex_string.split(p_str => l_string, p_sep => ':');
  for i in 1..l_page_items_arr.count
  loop
    sys.htp.p(l_page_items_arr(i)||':'||apex_util.get_session_state(l_page_items_arr(i)));
  end loop;
end;

Depending on your application, you might need to be careful. For example with AOP, we support customers with versions of Oracle APEX 5.0, 5.1 and 18.1. We can't really force customers to move to higher APEX versions, so the way we solve it is by using conditional compilation of our code. If we see you are on APEX 5.1 or above we will use apex_string.split if not, and you are still on an earlier version, we will use apex_util.string_to_table.

Here's an example of what the code with conditional compilation looks like:
  $if wwv_flow_api.c_current >= 20160824
  $then
    l_page_items_arr := apex_string.split(p_str=>l_string, p_sep=>':');
  $else
    l_page_items_arr := apex_util.string_to_table(p_string=>l_string, p_separator=>':');
  $end

Note the conditional compilation you also need to do on the variable if they are different, or you can choose to conditional compile on the entire function.

To conclude, I recommend with every new release of Oracle APEX to look for deprecated components and search for those and make notes to change those when needed.