When using the ‘warn on unsaved changes’ page attribute, APEX pages behave as you would expect. However, the inline help notes this option is not relevant for modal pages.
Note: This does not apply to an end user closing a Modal dialog page.
The following JavaScript offers a fairly self explanatory method of replicating this need, if applied to the JS action of a ‘cancel’ button on a modal form
if (apex.page.isChanged())
apex.message.confirm('There are unsaved changes. Do you want to continue?'
,function(result){
if(result===true){
apex.navigation.dialog.cancel(true);
}
});
else
apex.navigation.dialog.cancel(true);
However, this does not apply when the X button is pressed on the top-right of a modal.
Placing this logic within a shared function for the application is a little tricky. Back in 2017 Jorge Rimblas posted a suggested solution that wasn't quite robust enough to do the job
https://rimblas.com/blog/2017/04/warn-before-closing-apex-modal-dialogs/
but commenter Samuil provided a workaround (search for secondComing)
When the function was placed in a JS file available to the app, it could then be referenced in the Dialog-Attributes field in each relevant modal using
beforeClose: warnOnClose
This was ok in 20.2, but stopped functioning correctly after 22.1.
Instead, it doesn't re-display the confirm() prompt after clicking 'cancel', nor does it close the modal if your press ‘ok’.
Does anyone have a contemporary solution, or any suggestions?
Cheers