Oracle Apex: Javascript code in PL/SQL Block

ApexDev picture ApexDev · Dec 14, 2015 · Viewed 21.3k times · Source

Is it possible to have JavaScript code in the PL/SQL block. I want to execute the pl/sql block containing JavaScript code on submit in oracle Apex page process.

DECLARE
  v_count   NUMBER;

  BEGIN
        select count(*) into v_count
        from summary
        where prd_items = 'Total';

 HTP.p ('<script type="text/javascript">');
 HTP.p (   'alert(''The value of Total for BU is ' ||v_count|| '.\n'
      || 'You have to enter correct values to proceed further \n'');');
 HTP.p ('</script>');
END; 

I have Submit button in my page region and this pl/sql block is page processing item and execute on page submit(Conditional:Submit).

But I am not able to pop-up the alert box. Please advise.

Thank you.

Answer

brenners1302 picture brenners1302 · Dec 15, 2015

Is it possible to have JavaScript code in the PL/SQL block?

  • YES

But, what you're trying to do wont work which is passing javascript function AFTER SUBMIT.It'll only work if you change the point of execution to AFTER HEADER.

Alternatively, if you just want to validate the values entered and doesn't want to use apex validation, you can use APEX_ERROR package.Try this.

DECLARE
  v_count   NUMBER;

  BEGIN
        select prd_items into v_count
        from summary
        where prd_items = 'Total';
        -- I dont really know what you want to 
        --accomplish with this query but Im pretty sure 
        --It will not return a number
        -- if you want to count the number of prd_items it should be like this
        --select COUNT(*) 
        --into v_count
        --from summary
        --where prd_items = 'Total';


    APEX_ERROR.ADD_ERROR(
      p_message            => 'The value of Total for BU is '||v_count||'.<br>'||
                              'You have to enter correct values to proceed further',
      p_display_location   => apex_error.c_inline_in_notification 
    );  

END; 

EDIT: if you want to show the error if count not equal to 100 then do something like this:

DECLARE
  v_count   NUMBER;

  BEGIN

     Select COUNT(*) 
     into v_count
     from summary
     where prd_items = 'Total';

  IF v_count != 100 THEN
    APEX_ERROR.ADD_ERROR(
      p_message            => 'The value of Total for BU is '||v_count||'.<br>'||
                              'You have to enter correct values to proceed further',
      p_display_location   => apex_error.c_inline_in_notification 
    );  


 END IF;
END;