Saving unsaved changes using uQueryCanDeactivate avNotifyDeactivation

Visual LANSA Framework

Saving unsaved changes using uQueryCanDeactivate / avNotifyDeactivation

In version epc831 or later of the Framework it is possible to add a redefined method to command handlers called uQueryCanDeActivate.

This method is invoked when the end-users try to move from the command handler to somewhere else in the Framework, or when they try to close down the Framework. It is run when the user clicks on another command tab, a new instance of the business object or another business object or application.

It is particularly useful for saving unsaved changes. The routine can check whether changes need to be saved. If so, the user can be asked "Do you want to save your changes before continuing?" (Yes/No). If they answer Yes, their changes can be saved.

At the time the routine is run, the command handler still has all its values as they were before the user attempted to move away. So checking for unsaved changes, and saving those changes, is easy.

To use it, you need to set the avNotifyDeActivation property in the command handler's initialize routine

* Handle Initialization

 

Mthroutine Name(uInitialize) Options(*REDEFINE)

 

* Do any initialization defined in the ancestor

 

Invoke #Com_Ancestor.uInitialize

 

* Activate Check for unsaved changes (Unsaved changes logic)

set #Com_Owner avNotifyDeactivation(TRUE)

 

Endroutine

 

Then add a redefined uQueryCanDeActivate routine to your command handler:

 

* The Framework initiates this when the user moves to another command tab, or business object instance,  or business object, or application, or closes the framework.

* (The framework may initiate this method multiple times)

 

MTHROUTINE NAME(uQueryCanDeactivate) OPTIONS(*REDEFINE)

* Define_Map For(*Result) Class(#vf_elBool) Name(#Allow)

 

#Allow := True

 

if '(#pty_NeedsSaving *eq TRUE)'

 

* If something needs saving, ask the user if they want to save it

 

USE BUILTIN(MESSAGE_BOX_SHOW) WITH_ARGS('YESNO' 'YES' *Default *Default 'The notes have been changed. Would you like to save your changes before continuing?') TO_GET(#MSG_RET)

 

if '#MSG_RET *eq YES'

 

* Save everything

<< my save logic>>

 

endif

 

#pty_NeedsSaving := False

 

endif

 

endroutine

 

A more complicated version could set #Allow to false if there was an error during the save, and in that case the user would not go to where they clicked, (or the Framework would stay open if they were attempting to close it).

Also see:

Reason Code

Comments/Warnings