0

I have the following issue. I have a page element of type popUp-LoV with the following SQL query:

SELECT u.Lastname || ', ' || u.Firstname AS displayed, u.UUID as ID

from STAFF s

left outer join USERS u on s.UUID=u.UUID

union

SELECT u2.Lastname || ', ' || u2.Firstname AS displayed, m.MAID AS ID

FROM MANAGER m

left outer join USERS u2 on m.UUID=u2.UUID

In this way, I would like to show the user people from two tables to choose from. However, I want that as soon as the user selects a person, the foreign key to the corresponding table is set. The foreign keys to these tables will be kept in separate columns. In other words I created two hidden page items, each has a foreign key constraint to the corresponding table. Depending on the return value of the list of value, I would like then to set the page items (one would be null) in the corresponding page item.

To achieve this, I first created a function that should check whether the returned value of the LoV is in a table as an ID.

create or replace function search_id (p_key_value in number)
return number
is
found_person number;
begin

select m.ID into found_person from MANAGER m where p_key_value=m.ID ;
if sql%rowcount = 1 then
return found_person;
end if ;
exception
when no_data_found then
return null;
end;

changed sql query for LoV:

SELECT u.Lastname || ', ' || u.Firstname AS displayed, u.UUID as ID
    from USERS u 
   
     and u.UUID in (select u.UUID
               from STAFF s
               left outer join USERS u on s.UUID=u.UUID)
                OR u.UUID in (select u.UUID
               from MANAGER m
               left outer join USERS u on m.UUID=u.UUID)

changed function:

create or replace function search_id(p_key_value in number)
return number
is
found_person number;
begin

select s.STID
into found_person
from STAFF s
where s.UUID=p_key_value;

if sql%rowcount = 1 then
return found_person;
end if ;
 exception
  when no_data_found then
    select m.MID
into found_person
from MANAGER m
where m.UUID=p_key_value;;
  end;

Dynamic action: I then created a dynamic action for the page item with the popup LoV. The dynamic action fires when the page item is changed (so when the user chooses a value from LoV).

The action is: execute pl/sql code with following pl/sql function:

    declare
found_person number;
begin
found_person:= search_id(V('P12_RESPONSIBLE')); 
apex_util.set_session_state('P12_MANAGER_ID',found_person);
apex_util.set_session_state('P12_STAFF_ID',found_person);
exception
when no_data_found then
null;
end;

Items to submit: P12_RESPONSIBLE Items to return: P12_MANAGER_ID,P12_STAFF_ID

user111
  • 137
  • 1
  • 15
  • Is there some reason to believe that there is no overlap between `staff.STID` and `manager.MAID`? I'd generally guess that both are independent sequence-generated keys so that you could have a staff with a `STID` of 123 and a manager with a `MAID` of 123. If so, you've got a fundamental problem that if the LOV item (which I am guessing is `P12_Responsible`) has a value of 123, you can't determine whether that is manager 123 or staff 123. – Justin Cave Apr 23 '21 at 09:25
  • The fact that you are doing a `left outer join` implies that both a `staff` row and a `manager` row can exist without a corresponding `users` row which seems odd. If every person is a user and the same user can't be both a `staff` and a `manager`, I'd probably return the `users.UUID` rather than the `stid` and `maid` since that would be unique. Depending on what you are actually doing with `P12_Responsible`, though, you might return the string `'staff - ' || s.stid` and `'manager - ' m.maid` and then simply parse the `P12_Responsible` to extract the numeric value into the right variable. – Justin Cave Apr 23 '21 at 09:29
  • hello you are right, I have changed the sql query accordingly to return the user id as the return value. Likewise, the function now looks to see what ID exists for the particular USERID and returns that. there is one thing i don't quite understand and that is how i can call the function and set the page elements accordingly. – user111 Apr 23 '21 at 10:04
  • OK. Can you update your question with the updated code? It's hard to discuss code based on descriptions because I might be assuming you changed something in a different way than you actually did. Is `P12_Responsible` the LOV? And then you are trying to populate `P12_Manager_ID` and, I'm guessing, `P12_Staff_ID`? – Justin Cave Apr 23 '21 at 10:14
  • OK. There is no longer a need to have a `union` in your LOV query. Just query the `users` table (probably add an `order by` though because I assume you want the users displayed in some sort of order). Do I guess correctly that `P12_Responsible` is the LOV and you are trying to populate two additional items `P12_Manager_ID` and `P12_Staff_ID`? – Justin Cave Apr 23 '21 at 10:28
  • The only thing is that it shows me all users, but I want only people from the Staff table or Manager table to be displayed. And yes, you are correct in your assumption:) – user111 Apr 23 '21 at 10:35
  • OK. So you want to update the LOV query again to remove the `union` and to add in a check that the user is either a `staff` or `manager`. – Justin Cave Apr 23 '21 at 10:37
  • ok I changed it and the query works fine. – user111 Apr 23 '21 at 10:45
  • Only question that remains is how to populate the other page items depending on the lov. Do I have to use dynamic action for this? – user111 Apr 23 '21 at 11:06
  • You can, sure. You could also have the LOV submit the page when a selection is made and put the logic in the items themselves or a before header process. – Justin Cave Apr 23 '21 at 11:09
  • I would like to use dynamic action for this, but the way I implemented it (see edited question), does not populate the page Items MANAGER_ID and STAFF_ID – user111 Apr 23 '21 at 11:29
  • The function you posted will throw an error if a manager is selected because there is no `return`. Checking `sql%rowcount` is also pointless since you know that either a single row will be returned by a `select into` or an error will be thrown `no_data_found` or `too_many_rows`. The exception handler in your dynamic action is pointless and just suppresses potential errors and prevents you from debugging them. Have you verified that your dynamic action is being called? Have you verified what `search_id_of_internal_sv` is returning? Is that the same as the `search_id` function you've posted? – Justin Cave Apr 23 '21 at 11:36
  • It seems odd that you'd assign the same value to both page items. If you're doing that, I struggle to see the purpose of having them rather than just using `P12_Responsible`. – Justin Cave Apr 23 '21 at 11:37
  • How can this be done? – user111 Apr 23 '21 at 11:45
  • You can use the toolbar to debug the application and look at the logs. You can enable debugging in the URL. You can add `apex_debug.message` and `apex_debug.enter` calls to your code. https://docs.oracle.com/database/apex-18.1/HTMDB/debugging-an-application.htm#HTMDB10000 – Justin Cave Apr 23 '21 at 11:48
  • the dynamic action works but it sets into the populated item always the return value 1 – user111 Apr 23 '21 at 13:06

0 Answers0