Search This Blog

Friday, January 11, 2013

NotesUIWorkspace.CurrentDocument - simple but not obvious

Hello

Recently I got an interesting bug in one application.

The case is next: as a lazy developer I do not like to do something twice. For example, you have some function that has to process document either from the view action or form action. As we know, there is a difference in getting target NotesDocument from the context of NotesView or NotesUIDocument.

I wrote a lotus script code that had to be capable to get target document from everywhere (from the view, from the notesuidocument etc). Here is my code with an error inside.

Dim w As New NotesUIWorkspace
Dim db As NotesDatabase
Dim uiview As NotesUIView
Dim uidoc As NotesUIDocument
Dim col As NotesDocumentCollection
Dim selectedDoc As NotesDocument

Set db = w.Currentdatabase.Database
Set uiview = w.CurrentView
Set col = uiview.Documents

If col.Count = 0 Then

If Not (uiview.CaretNoteID = "0" Or Len(uiview.CaretNoteID) > 4) Then
On Error Goto errh
Set selectedDoc = db.GetDocumentByID(uiview.CaretNoteID)
On Error Resume Next
End If

Else
Set selectedDoc = col.GetFirstDocument
End If

If selectedDoc Is Nothing Then
Set uidoc = w.Currentdocument
If Not uidoc Is Nothing Then Set selectedDoc = uidoc.document
End If

If Not selectedDoc Is Nothing Then
' !!!
' here we run required function on selectedDoc
' !!!
End If

Do you see any error here?  :-)

It worked fine till I get a few error reports with error description that was very unusual. You may agree with me, that if you work with Lotus Notes for a long time then in the most of cases when you get an error description you already know (more or less) how to fix it and where. But in this case I got really strange error descriptions. Besides that every time when I asked QA to reproduce this error for me - he couldn't.

And here is the reason.

This code will not work properly if you have many documents opened in UI and you switched between tabs.
Check screenshots below:

1) I have two databases (1 and 2)


2) Every database has an agent "Show UI Document Database" with such script inside:




3) Now I open 1st database, then 2nd database and then create a new document in 2nd database



4) Now I run agent "Show UI Document Database" while I am located in the document (in the 2nd database)


This is correct!

5) Then I switch to the tab with the 1st database without closing any window


6) I run the agent "Show UI Document Database" in the 1st database


I did not expected this

You see? I still got the document from the 2nd database as a CurrentDocument of NotesUIWorkspace.

So, be careful with such solutions :-)

No comments:

Post a Comment