Skip to main content
This blog will show you how to get around a problem with the Lose Focus event in Oracle APEX.  It isn’t as though the event doesn’t fire, but in some cases, it fires too slowly for APEX to correctly get all the values on a page.  When this happens, validations will be triggered incorrectly on page submit.

Simple Setup

We will create a simple form to duplicate the lose focus issue.  Start by making a new page with a static region called Entry Form.  Within the region, create the following page items:

  • P4_ACCOUNT_NUMBER
    • Text Field
    • Required Template
    • Value Required turned On
  • P4_DEPARTMENT
    • Text Field
    • Required Template
    • Value Required turned On
  • P4_AMOUNT
    • Number Field
    • Required Template
    • Value Required turned On

Next, add two buttons:

  • CANCEL
    • With a redirect to some other page
    • Close button position
  • CREATE_BTN
    • With a submit page action
    • Create button position

Lose Focus Dynamic Action

Right click on P4_ACCOUNT_NUMBER and create a Dynamic Action:

  • Lose Focus Event on P4_ACCOUNT_NUMBER
    • Add a True Action
      • Set Value
        • Static Assignment
        • Value = ‘Logistics’
        • Select Type = Item(s)
        • Items = P4_DEPARTMENT

Time for a Test

Run the page and enter the Account Number field and then tab or click out of it.  The word ‘Logistics’ should appear in the Department field.

Leave the Amount field blank and click on the Create button.  The only error that will show will be for the Amount field not having a value.

Let’s try another test.  To begin, clear out all the fields and then enter the Account Number field.

  • Instead of tabbing out or clicking on a random place on the page to lose focus, Directly click and hold the Create button for 2 seconds before releasing the mouse click.
    • The Lose Focus event will fire correctly and fill the Department Field.

To get things to fail, we will do one more test.  Clear out all the fields again and enter the Account Number Field.

  • Now directly click the Create button again, but click very quickly.
    • The value of ‘Logistics’ will be in the Department field, but a null validation will fire for the Department field.
      • Don’t make any changes to the form and click the Create button once again.
      • APEX will suddenly recognize that there is a value and you will not see a null validation for the Department field.

What is Going On?

My best guess is that there is not enough of a delay built into APEX between the Lose Focus event and the button click.  As we just demonstrated, if you click and hold the button for a second, Logistics appears in the Department field, but APEX is able to see that a value exists.

The Fix

To get this Lose Focus event to fire correctly in all circumstances, we will add a Dynamic Action on the Create button.

  1. Right click on the Create button and choose Create Dynamic Action.
  2. Add a JavaScript True Action and enter the code below:
let widget = var delay = 100;
function submitCreateEntryForm() {
apex.submit({request:"CREATE_BTN"});
}
setTimeout(submitCreateEntryForm, delay );

Explanation

The JavaScript True Action on the Create button will add a 100 millisecond delay before executing the submit page action when the Create button is clicked.  The 100 millisecond delay will be unnoticeable by users and it will guarantee that APEX will see the values correctly if a user direct clicks a button from a Lose Focus event.

Learn More

Disclaimer:
We do not take responsibility for any unintended or unwanted consequences in your instance of Oracle, Oracle APEX, or related products as a result of reading our blogs or following our guides. Though the information is fully tested and generally safe to use, our lawyers really have a thing against admitting potential wrongdoing. If it makes you feel any better, one time I convinced them the blue screen of death only happens if they didn’t view something blue every 4 hours.  I told them it was the result of a blue color bottleneck.