Wednesday, 30 August 2017

Complete Document Edit History Tracking Using LotusScript

Using LotusScript it is possible to save all changes made by users in a Notes document. Below a possible solution on which I was made noticeable and which I have implemented in a standard Notes application. In this case a DocumentHistory Field is updated if a value of a Field on the Notes Form has been changed by a user. The Date, the Name of the user, the Fieldname, the original value and the new value are stored in the DocumentHistory Field. The code is easy to customize to suit your own wishes.

A. Field DocumentHistory
First create a Field DocumentHistory on a Notes Form.
On the Field Properties Basic Tab set the Field DocumentHistory to a Multi-Value Text Field, computed with the formula DocumentHistory.

On the Field Properties Advanced Tab select Comma and Semicolon in the field Separate values when user enters. Select New Line in the field Display separate values with.

B. Form - Globals - Declarations
First a dynamic array in the Form Declarations section needs to be declared.

Dim FieldValues() As String 

C. PostModeChange
Next add the following code in the Postmodechange Event of the Form.

Sub Postmodechange(Source As Notesuidocument) 
'build array of current filed values 
Dim session As New NotesSession 
Dim db As NotesDatabase 
Dim doc As NotesDocument 
Dim form As NotesForm 
Dim fieldnum As Integer 
Dim counter As Integer 
Set db = session.CurrentDatabase 
Set doc = Source.Document 
Set form = db.GetForm(doc.Form(0)) 
fieldnum = Ubound(form.fields) 
Redim FieldValues(fieldnum,1) 
counter = 0 
Forall field In form.fields 
FieldValues(counter,0) = field 
FieldValues(counter,1) = source.fieldgettext(field) 
counter = counter + 1 
End Forall 
End Sub 

Remark: If document are automatically opend in Edit Mode the above code should be added to the PostOpen Event of the Form.

D. QuerySave
Next add the following code in the QuerySave Event of the Form. If the is already some code in the QuerySave Event place the code below after all the other code.

If Not Source.IsNewDoc Then 
Call DocumentHistory 
End If 

E. Subroutine DocumentHistory
Finally add the following Sub in the Form Declarations section or a Script Library.

Sub DocumentHistory 
Dim session As New NotesSession 
Dim workspace As New NotesUIWorkspace 
Dim source As NotesUIDocument 
Dim fieldnum As Integer 
Dim entry As String 
Set source = workspace.CurrentDocument 
For fieldnum = 0 To Ubound(FieldValues) 
If FieldValues(fieldnum,1) <>source.fieldgettext(FieldValues(fieldnum,0)) Then 
entry = Date$+Chr(9)+ session.CommonUserName+Chr(9) +"Changed Field: " + FieldValues(fieldnum,0)+ " from " + FieldValues(fieldnum,1)+ " to " + source.fieldgettext(FieldValues(fieldnum,0)) 
Call source.FieldAppendText("DocumentHistory",";"+entry)
End If 
End Sub

The final result is a nice document history of all the changes made by users.

No comments:

Post a Comment