4
votes

How can we call javascript code from a PLSQL code in dynamic action of Oracle apex. Actually, my requirement is to select a Role(P2_ROLE) from a dropdown list. And than find all the usernames attached to this role using sql query. Once the usernamess are retrieved, all the rows in interactive grid should get coloured for these list of users. The IG contains a column name USER_NAME.

If The role is present in the IG, than I can make it work by writing the below code in dynamic action ---> Javascript code

     $(document).ready(function() {
$("td:nth-child(28)").each(function() {
    if ($(this).text() === apex.item( P2_ROLE ).getValue()) {
        $(this).parent().children().css({'background-color': '#FF9999'});
    }       
});
});

But the issue is the Role that is picked up is not displayed in the interactive grid.

So as a workaround, I want to fetch all the usernames specific to that role using PLSQL and match them with the USER_NAME column in interactive grid using JAVASCRIPT.

But I am not sure how can I call JAVSCRIPT code in a PLSQL code. Basically I want to do something like this :

DECLARE
Cursor c_user_name is
 select distinct user_name  from wf_user_roles where role_name = :P2_ROLE;
l_USER_NAME varchar2(1000);
BEGIN
 OPEN C_USER_NAME ;
  LOOP
   FETCH C_USER_NAME into l_USER_NAME;
   EXIT WHEN C_USER_NAME%NOTFOUND;

  -- Call this JAVASCRIPT code now  
  /*
  $(document).ready(function() {
   $("td:nth-child(28)").each(function() {
    if ($(this).text() === l_USER_NAME) {
        $(this).parent().children().css({'background-color': '#FF9999'});            

     }        
   });
  });
 */

 END LOOP; 
 END;

Can somebody please help me regarding this.

1

1 Answers

3
votes

You can use apex.server.process:

https://docs.oracle.com/cd/E71588_01/AEAPI/apex-server-namespace.htm#AEAPI30050

With this function you can call a PL/SQL process with javascript and do something after this code return something.

Example: https://community.oracle.com/thread/4094475

UPD.

JAVASCRIPT

apex.server.process("new_assign_roles", 
   { x01: 'a_value', x02: 'b_value', x03: 'c_value' },
   {
      success: function(pData) {
         //you can do anything here
         console.log(pData);
         apex.message.alert(pData.v_result);
      }
   }
);

ON DEMAND PROCESS

DECLARE

p_a VARCHAR2(1000) := APEX_APPLICATION.g_x01; 
p_b VARCHAR2(1000) := APEX_APPLICATION.g_x02; 
p_c VARCHAR2(1000) := APEX_APPLICATION.g_x03; 

v_result VARCHAR2(4000) := p_a||' - '||p_b||' - '|| p_c;

BEGIN 
-- you can do anything here  

    apex_json.open_object;  
    apex_json.write('success', true);  
    apex_json.write('v_result', v_result);  
    apex_json.close_object;  

END;

You can see this example here:

https://apex.oracle.com/pls/apex/f?p=145797:33

login with:
workspace: stackquestions
user: user_test
pwd: stackquestions
application: 145797
page: 33

20191125AL > User user_test is locked.