Based on a Code-Snippet found at StackOveflow I implemented a warning-message that will be displayed if the user tries to leave the page without saving his changes made on the form.

Only three things are required to acomplish this goal.

  • create an Item that got set if an item got changed or edited
  • a dynamic action that detects a change made
  • few lines of Javascript on page-level

 

First things first, we need an hidden item somewhere on the page. It is named here P42_CHANGED and has a default value of "N".

Secondly, a Dynamic Action is required that changes the Value of P42_CHANGED to "Y" as soon as any form element gets changed.

Note that you have to exclude the item itself from the jQuery-Selector. Otherwise you'll encounter a problem when you try to reset the Value to "N".

The value is set with a true-action that is configured as shown below.

It is important to set the Event-Scope of the Dynamic Action to Dynamic!

 

At last, the slightly modified code I found at StackOverflow is put into the JavaScript declaration area on the page itself.

window.onbeforeunload = function () {
   if (apex.item("P42_CHANGED").getValue() == "Y") {
     return "Are you sure you want to leave without saving?"
   }
}

 

You may try it out yourself going to the Live-Example.