How to create a comments field for Lotus Notes documents
SearchDomino.com member Ray Green explains how to create a comments field for Lotus Notes documents by setting up two simple fields and adding some LotusScript code.
To create a field for users to add notes or comments to a Lotus Notes document, you must first set up two fields.
The first, we'll call Cust_NotesTemp. It only has code in the Exiting Event. This is an editable field into which a user can enter notes or comments.
When they exit the field, the Exiting Event code trims the field of junk, stamps the new note or comment with the current date and time, adds a line feed. It then links the new comment together with the other comments already present in the Lotus Notes document, and saves it into the Cust_Notes field. When finished, it clears out the Cust_NotesTemp field.
One consideration is that the user may save the Lotus Notes document after entering comments into it, but before exiting the Cust_NotesTemp field. As such, the Exiting Event must be triggered when the document is saved by forcing the field exiting event, no matter where the cursor is. Move the cursor to some field at the beginning of the document that doesn't need any entering or exiting event code -- Call uidoc.GoToField("??? any field").
Now set up the second field and call it Cust_Notes. This field only has code in the Entering Event. This will store all notes or comments with dates/times, as well as the user's name that created each note. This is an editable, but restricted, field that only a user with manager access can edit. This prevents Lotus Notes users from changing their own comments or those of other users. Finally, add the following LotusScriptcode:
'Cust_NotesTemp Field - Exiting Event Sub Exiting(Source As Field) Dim session As New NotesSession Dim db As NotesDatabase Dim workspace As New NotesUIWorkspace Dim uidoc As NotesUIDocument Set db = session.CurrentDatabase Set uidoc = workspace.CurrentDocument 'check that the user has added a new note and trim it of junk custnotestemp = uidoc.FieldGetText("Cust_NotesTemp") If custnotestemp = "" Then Exit Sub Call TrimNotes(custnotestemp) 'see TrimNotes subroutine below 'pick up any existing notes, and add a return before stamping a new date/time and user name 'then add the new notes after the last note custnotes = uidoc.FieldGetText("Cust_Notes") notestime = Format(Now,"DD/MM/YY HH:MM") notesuser = session.CommonUserName If custnotes = "" Then custnotes = notestime & " : " & session.CommonUserName & " : " & custnotestemp Else If Instr ( 1 , custnotes , notestime & " : " & notesuser ) <> 0 Then custnotes = custnotes & " " & custnotestemp Else custnotes = custnotes & Chr(10) & notestime & " : " & session.CommonUserName & " : " & custnotestemp End If End If 'save the new and old notes and clear out the temp notes field Call uidoc.FieldSetText("Cust_Notes",custnotes) Call uidoc.FieldSetText("Cust_NotesTemp","") End Sub 'Cust_Notes Field - Entering Event This field is used to store all notes made on a customer account, with the User Name, Date & Time the note was made. The contents can't be changed by anyone other than a user defined with Manager Access. Sub Entering(Source As Field) Dim session As New NotesSession Dim db As NotesDatabase Dim workspace As New NotesUIWorkspace Dim uidoc As NotesUIDocument Dim acl As NotesACL Dim entry As NotesACLEntry Set db = session.CurrentDatabase Set uidoc = workspace.CurrentDocument Set acl = db.ACL user = session.CommonUserName & "???" 'replace ??? with organization name Set entry = acl.GetEntry( user ) If entry.IsRoleEnabled( "[MANAGER]" )Then Exit Sub Else Call uidoc.GoToField("Cust_NotesTemp") End If End Sub 'Subroutine to Trim the Notes that are saved of all "junk" and unecessary characters Sub TrimNotes(fixthis) t= fixthis trimmed = "" found = "N" 'look for chr(13) or chr(10) and if found look for another and if found remove the second one For x = 1 To Len(t) 'found the 1st CR or LF - accept it If (Mid ( t , x , 1 ) = Chr(13) Or Mid ( t , x , 1 ) = Chr(10)) And found = "N" Then found = "Y" trimmed = trimmed & Chr(10) Goto getnext End If 'found a 2nd or subsequent CR or LF - throw it away If (Mid ( t , x , 1 ) = Chr(13) Or Mid ( t , x , 1 ) = Chr(10)) And found = "Y" Then Goto getnext End If trimmed = trimmed & Mid ( t , x , 1 ) found = "N" getnext: Next t = trimmed 'change double spaces to single space a = " " b = " " If (a<>b) Then i=Instr(t,a) done=(i=0) While Not done t=Left(t,i-1)+b+Mid(t,i+Len(a)) n=i+1 i=Instr(n,t,a) If i=0 Then i=Instr(t,a) done=(i=0) Wend End If 'change // to / a = "//" b = "/" If (a<>b) Then i=Instr(t,a) done=(i=0) While Not done t=Left(t,i-1)+b+Mid(t,i+Len(a)) n=i+1 i=Instr(n,t,a) If i=0 Then i=Instr(t,a) done=(i=0) Wend End If 'change double hyphens -- to single - a = "--" b = "-" If (a<>b) Then i=Instr(t,a) done=(i=0) While Not done t=Left(t,i-1)+b+Mid(t,i+Len(a)) n=i+1 i=Instr(n,t,a) If i=0 Then i=Instr(t,a) done=(i=0) Wend End If fixthis = Trim(t) End Sub
I've found a much easier solution.
First, create two fields and call one Comments (make this an editable text field) and Comments_D (make this a computed text field). Next, use the following as the formula for the Comments_D field:
rtn := @If(@IsDocBeingSaved & Comments != "" ; @Text(@Date(@Today)) + " - " + Comments + @NewLine + Comments_D; Comments_D); FIELD Comments := ""; rtn
Place the form in edit mode, enter some text, save the document and finally close the document. I must admit, this does not do the "clean up" that the author's LotusScript does, but that could be done with a couple of @ReplaceSubstring or @Replace commands added in.
Please allow me to add an appeal to everyone trying to do Lotus Notes/Domino development. Learn the @Functions! I see so many examples of programmers trying to do everything in LotusScript when a couple of simple @Functions can do the trick.
—Randy D.
******************************************
Randy D. is on the right track. I use the Formula language a lot and I have a form with three fields to update comments into one field and then clear the previous two so that they are blank for the next user.
To accomplish this, create two editable fields (UpdateSubject, which is optional, and UpdateDetail) and one computed field (UpdateLog). Then, create a button on the form and put this code behind it:
@IsDocBeingEdited = 0; @Command([EditDocument]; "1" ); updatetext := @NewLine+ "- Request updated on " + @Text(@Now) + " by " + @Name([CN]; @UserName)+ " - Subject: " +UpdateSubject+ " - Detail: " +UpdateDetail; @SetField("UpdateLog"; UpdateLog + updatetext); @Command([FileSave]); @SetField("UpdateSubject"; null); @SetField("UpdateDetail"; null); @Command([FileSave])
This grabs the username, current date and time to add into the comment. The user types a subject in the UpdateSubject field (this is optional) and the comment in the UpdateDetail field. Then click the button and it immediately adds the comment to the log below. It saves the changes and then clears the Subject and Detail fields. It then saves the form again. You can use @Command([FileCloseWindow]) command at the end to close the document.
—Linda T.
Do you have comments on this tip? Let us know.
Related information from SearchDomino.com:
This tip was submitted to the SearchDomino.com tip library by member Ray Green. Please let others know how useful it is via the rating scale below. Do you have a useful Lotus Notes, Domino, Workplace or WebSphere tip or code snippet to share? Submit it to our monthly tip contest and you could win a prize.