How to Add a Dynamic Action That Will Redirect to Another Page by Executing JavaScript Code

How to add a dynamic action that will redirect to another page by executing JavaScript Code?

I finally found out how to do this. The Request of the button (specifically the dynamic action of the button) was not set to the button name because the 'Request/Button Name' of the 'Submit Page' true action was not set to the name of the button.

Make sure in the dynamic action to add this information under 'Settings'. The branch should be placed to 'Submit: before Processing' with the condition 'Request = Expression 1' with Expression 1 equaling the button name!

Redirect/Refresh Page Using Dynamic Action - Oracle Apex 21.1

I'm not sure that will ever work - you can't do a redirect from within pl/sql in a dynamic action process. The pl/sql code is running in the background and cannot redirect the page. There are 2 options

  1. Change true action on page 7 to submit page and add a branch to redirect to the url of your choice
  2. Redirect using javascript (google it, you'll find several links on that)

How to attatch Dynamic Action or Process to a Menu Popout list item - APEX 21.1

There isn't a built-in solution for this unfortunately. There is a good blog post by John Snyders that describes a way of building this yourself.

I have also built some similar functionality, which I have a demo of here. This uses a bespoke table to store the menu options, and makes an AJAX call when the menu is opened to get the appropriate menu entries to display. These can be conditional e.g. the "Calculate commission" menu entry is only applicable to employees with job='SALESMAN'.

Tables
create table  report_menus 
( menu_name varchar2(30),
constraint report_menus_pk primary key (menu_name)
);

create table report_menu_entries
( menu_name varchar2(30) not null enable,
entry_title varchar2(100) not null enable,
entry_target varchar2(1000) not null enable,
display_sequence number,
condition_type varchar2(30),
condition_expression varchar2(4000),
constraint report_menu_entries_pk
primary key (menu_name, display_sequence),
constraint report_menu_entries_fk1
foreign key (menu_name)
references report_menus (menu_name)
);

(Table REPORT_MENUS is there for completeness but doesn't do much in this demo).

Sample data

For my example I created a single menu called 'EMPLOYEE_MENU' with 2 options:

insert into report_menu_entries values 
( 'EMPLOYEE_MENU',
'Update',
'f?p=&APP_ALIAS.:3:&SESSION.::&DEBUG.:3:P3_EMPNO:<pk>',
1,
'NOT_EXISTS',
'select * from emp where empno=<pk> and job = 'PRESIDENT''');

insert into report_menu_entries values
( 'EMPLOYEE_MENU',
'Calculate commission',
'javascript:alert('Not yet implemented');',
2,
'EXISTS',
'select * from emp where empno=<pk> and job='SALESMAN''');

I have invented a special placeholder <pk> which I use wherever I want to plug in the primary key of the record we are on (i.e. the EMPNO in this demo). This is required in the target URL and in conditions that depend on the data.

Report region

In the report, I create a dummy column for the menu by selecting null as menu. I make this into a Link with a target URL of # (we don't need the target really) and these properties:



















PropertyValue
Link text<span class="fa fa-navicon" aria-hidden="true" title="report menu"></span>
Link attributesclass="report-menu" data-key="#EMPNO#" data-menu="EMPLOYEE_MENU"

Alert message after submit and validation page in oracle apex

There are different ways you could approach this. You're currently using a mix of Dynamic Actions and page-level validations and processes that aren't going to play well together. I suggest you move all the logic to Dynamic Actions. Here are step by step instructions to do what I think you're trying to do. You can learn from this and then integrate what you want back to your solution.

  1. Create a new blank page. Mine was page 17. You'll need to update references to "P17" with your page number if it's different. Disable the page level attribute Warn on Unsaved Changes to prevent prompts before the redirect to Google.
  2. Add a new HTML region to the page.
  3. Add a new item to the region. Set Name to P17_FIRST_NAME and leave the default Type of Text Field.
  4. Add a new item to the region. Set Name to P17_LAST_NAME and leave the default Type of Text Field.
  5. Add a new item to the region. Set Name to P17_RESULT, Type to Hidden, and Value Protected to No. This item will be used to transfer messages from the server to the client via Ajax.
  6. Add a button to the region. Set Name to RUN_PROCESS and Action to Defined by Dynamic Action.
  7. Create a new Dynamic Action that fires when the button is clicked. The easiest way to do this is to right-click the button and select Create Dynamic Action. Set the Name to RUN_PROCESS clicked.
  8. Select the Show action that was created by default for the Dynamic Action. Set Action to Execute PL/SQL Code and copy-paste the following code into the PL/SQL Code attribute.

    declare

    l_result_obj json_object_t := json_object_t();
    l_errors_arr json_array_t := json_array_t();
    l_error_obj json_object_t;

    begin

    if :P17_FIRST_NAME is null
    then
    l_error_obj := json_object_t();

    l_error_obj.put('pageItem', 'P17_FIRST_NAME');
    l_error_obj.put('message', 'First Name is required.');

    l_errors_arr.append(l_error_obj);
    end if;

    if :P17_LAST_NAME is null
    then
    l_error_obj := json_object_t();

    l_error_obj.put('pageItem', 'P17_LAST_NAME');
    l_error_obj.put('message', 'Last Name is required.');

    l_errors_arr.append(l_error_obj);
    end if;

    if l_errors_arr.get_size() > 0
    then
    l_result_obj.put('status', 'error');
    l_result_obj.put('errors', l_errors_arr);
    :P17_RESULT := l_result_obj.to_string();
    return;
    end if;

    null; -- do "success" processing here

    l_result_obj.put('status', 'success');
    l_result_obj.put('message', 'Hi ' || :P17_LAST_NAME || ' ' || :P17_LAST_NAME ||
    '! You will now be redirected to Google.');

    :P17_RESULT := l_result_obj.to_string();

    end;

    As you can see, the validations are now being done inside the process. The PL/SQL code is making use of the JSON types introduced with Oracle 12.2. If you're on an older version of the database, you can adapt the code to use APEX_JSON instead.

  9. While still in the Execute PL/SQL Code action, set Items to Submit to P17_FIRST_NAME,P17_LAST_NAME and Items to Return to P17_RESULT. This is how you can transfer values from the page into session state before the process executes and then back to the page after the process finishes executing.
  10. Create a new Dynamic Action that fires on the Change event of P17_RESULT. The easiest way to do this is to right-click the item and select Create Dynamic Action. Set the Name to P17_RESULT changed.
  11. Select the Show action that was created by default for the Dynamic Action. Set Action to Execute JavaScript Code and copy-paste the following code into the Code attribute.

    var result = JSON.parse($v('P17_RESULT'));

    apex.message.clearErrors();

    if (result.status === 'error') {
    for (var idx = 0; idx < result.errors.length; idx++) {
    result.errors[idx].type = 'error';
    result.errors[idx].location = ['page', 'inline'];
    result.errors[idx].unsafe = false;
    }

    apex.message.showErrors(result.errors);
    } else if (result.status === 'success') {
    apex.message.alert(result.message, function(){
    apex.navigation.redirect('https://google.com');
    });
    }

    The JavaScript code takes the result from the process and either displays error messages or an alert. I'm using apex.message.alert instead of apex.message.showPageSuccess because the former supports a callback when the message is dismissed. When the message is dismissed, apex.navigation.redirect takes the user to Google.

Here's what it should look like in the end:

Dynamic Action based processing

I hope there's enough information here for you to understand what's going on. Let me know if you have any questions. You'll find the documentation for apex.navigation and apex.message here: https://apex.oracle.com/jsapi

P.S. Here's an example of what the PL/SQL code would look like using APEX_JSON.

declare

l_error_count pls_integer := 0;
l_result_obj clob;

begin

apex_json.initialize_clob_output;

apex_json.open_object();

apex_json.open_array('errors');

if :P17_FIRST_NAME is null
then
l_error_count := l_error_count + 1;
apex_json.open_object();

apex_json.write('pageItem', 'P17_FIRST_NAME');
apex_json.write('message', 'First Name is required.');

apex_json.close_object();
end if;

if :P17_LAST_NAME is null
then
l_error_count := l_error_count + 1;
apex_json.open_object();

apex_json.write('pageItem', 'P17_LAST_NAME');
apex_json.write('message', 'Last Name is required.');

apex_json.close_object();
end if;

apex_json.close_array();

if l_error_count > 0
then
apex_json.write('status', 'error');
apex_json.close_object();

:P17_RESULT := apex_json.get_clob_output();
apex_json.free_output;
return;
end if;

null; -- do "success" processing here

apex_json.write('status', 'success');
apex_json.write('message', 'Hi ' || :P17_LAST_NAME || ' ' || :P17_LAST_NAME ||
'! You will now be redirected to Google.');
apex_json.close_object();

:P17_RESULT := apex_json.get_clob_output();
apex_json.free_output;

end;


Related Topics



Leave a reply



Submit