Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Oracle Apex: Javascript code in PL/SQL Block

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.

like image 674
ApexDev Avatar asked Dec 14 '15 09:12

ApexDev


2 Answers

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; 
like image 101
brenners1302 Avatar answered Oct 22 '22 00:10

brenners1302


To embed the javascript code from the pl/sql procedure you will have to place the procedure at a "Before Header" point. But i do not think this is the best solution for what you are trying to achieve.

What you are trying to do is to add a validation right? If so why not use the apex validations. Create a validation with options like this:

  1. Identify the validation level:Page Item
  2. Identify the Page Item that is to be validated: Select the item you want.
  3. Select a validation type: PL/SQL
  4. Pick the type of validation you wish to create:Function Returning Error Text
  5. Validation Code:
    DECLARE  v_count          NUMBER;
  V_validation_msg VARCHAR2(500);
BEGIN
  SELECT prd_items INTO v_count FROM summary WHERE prd_items = 'Total';
  V_validation_msg:='The value of Total for BU is ' ||v_count|| '.\n' || 'You have to enter correct values to proceed further';
  IF 1= 1 THEN --add some condition here if you want
    RETURN V_validation_msg;
  ELSE
    RETURN NULL;
  END IF;
END;
  1. When Button Pressed: Your Submit button.
like image 1
Cristian_I Avatar answered Oct 22 '22 00:10

Cristian_I