I had a customer who requested that a document be automatically closed by the system. To do this, I used the Notes Timer (and a field in the DBProfile) so users can change the timeout interval.
I created a subform called TimerSF.
Single Field "DocTimeout" Computed Number @Text(@GetProfileField( "DBProfile" ; "DocTimeout")); Declarations Dim elapsedTime As Integer Dim elapsedTimer As NotesTimer %INCLUDE "lsconst.lss" Sub Postopen(Source As Notesuidocument) Dim backdoc As notesdocument Set backdoc = source.Document Set elapsedTimer = New NotesTimer (60, "Elapsed time since opening document") elapsedTime = 0 On Event Alarm From elapsedTimer Call elapsedTimerHandler If backdoc.docTimeout(0) = "" Or Cint(backdoc.docTimeout(0)) = 0 Then elapsedTimer.Enabled = False Print "Timer Disabled" Else elapsedTimer.Enabled = True Print "Timer Enabled" End If End Sub Sub Postrecalc(Source As Notesuidocument) elapsedTime = 0 'Reset Counter if doc is being edited End Sub Sub elapsedTimerHandler (Source As NotesTimer) Dim workspace As New NotesUIWorkspace Dim uidoc As NotesUIDocument Dim backdoc As notesdocument Set uidoc = workspace.CurrentDocument Set backdoc = uidoc.Document elapsedTime = elapsedTime + 1 If elapsedTime > Cint(backdoc. DocTimeout(0)) Then Call uidoc.Save Call uidoc.Close Print "Doc Closed as Timeout period Expired" Else Print Cstr(backdoc.DocTimeout(0)) + " < " + Cstr(elapsedTime) End If End Sub
- Don't use a field on the form for loading the profile value -- do it in the code and store the value in a global. More efficient and hack-proof.
- If you do use a field, at least make it Computed for Display -- there's no need to store this value.
- Don't save changes to a document without first checking whether it's in edit mode.
- Are we really sure we want to save changes without prompting the user?
- This may crash the user's workstation in the following scenario: UIdoc.save fails because of input validation. Messagebox with validation error message. User doesn't click OK within the interval to the next timer event (one minute). Notes crashes because the timer event happens when the previous timer event isn't done yet. To prevent this, the code should disable the timer before doing anything that might prompt the user.
- Timer event uses NotesUIDocument.CurrentDocument -- this returns the current document that's open onscreen. Since the user can open additional windows, the timer document may be open but not be the current window. The event code may therefore save and close a document in another window that it wasn't meant to mess with.
Do you have comments on this tip? Let us know.