2

I have a dynamic action doing the below ,
Submit page ,
Successful Alert message,
Log out and redirect to another website (www.google.com),
And I have two required item in page.

When pressed the button and the item is null ,the successful Alert message appears and after that the system shows the error (the item is required). How can I show the successful alert message only when the process and validation are done without errors ,and when ok is pressed in the alert message redirect to website and log out from the sessions

5
  • It appears that those items aren't required after all (if you want to ignore the warning). So set them to be non-required.
    – Littlefoot
    Jan 26, 2020 at 19:31
  • No the item it's required but when submit page the alert message appears before validation error , because that how I can change the alert don't appear when item is null.
    – jaw
    Jan 26, 2020 at 20:00
  • that mean how i can stop alert message and only appear when successfully process and validation
    – jaw
    Jan 27, 2020 at 5:16
  • It sounds like you do not need a dynamic action. Just have your button action to be submit page and create a branch to do the redirect to the other site. That is an easier approach and much cleaner. Jan 27, 2020 at 13:47
  • I know, but success message not appear because the redirect to onther website (I have a link in website when click on it ,redirect to public page to fill your data , after press finish return to web site), because that I need dynamic alert message.
    – jaw
    Jan 27, 2020 at 14:38

1 Answer 1

6

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;
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.