Cascading Select Lists and Super LOV’s and Automatic Selection

A request was made for a pair of cascading LOV’s to automatically select the value in the 2nd LOV if there was only one choice in the list.  This was fairly straightforward with a pair of select lists.  In the Default section for the 2nd LOV, I set the type to PL/SQL Function Body and wrote a function that selects the count of the records that should be in the second LOV.  If the count is 1 then select and return the id of the value to set, otherwise return null.

declare
  v_count number := 0;
  v_temp_id number := null;
begin
  if :P1_PARENT is not null then
    select count(*)
    into v_count
    from my_table
    where parent_id = :P1_PARENT;

    if v_count = 1 then
      select child_id
      into v_temp_id
      from my_table
      where parent_id = :P1_PARENT;
    end if;
  end if;

  return v_temp_id;
end;

 

To do this with the Skillbuilders Super LOV’s some extra steps are required. First, I created a hidden page item that would store my temporary ID (P1_ID_TEMP).  Then I created a dynamic action on the change of the parent LOV.  The first action of this dynamic action is a Set Value that uses the same PL/SQL Function Body as above to set the value of P1_ID_TEMP.

The second action of the dynamic action is to execute the following javascript code to set the value of the Super LOV:

$('#P1_CHILD').apex_super_lov('setValuesByReturn', $v('P1_ID_TEMP'));