Thursday, December 2, 2010

Debugging traditional WebQueryOpen/WebQuerySave code

I just had a eureka moment when it comes to debugging WQO/WQS code. Once upon a time I decided it was impossible to debug code that required NotesSession.DocumentContext, through the (local) Domino Debugger. It's not :)

You need a global NotesDocument variable that you can bind the DocumentContext to. The reason that it has to be global is that it has probably has to be available across multiple methods/scopes. It should be put inside a script library that doesn't use other script libraries. In most of my apps, I have a script library stack, where the most general code is put inside a script library that most other script libraries use (either through "inheritance" or through Use). This is where I put the global declarations.

Then you need a function that returns either NotesSession.DocumentContext, or a temporary document. This should be put inside the same script library as the aforementioned global declaration.
Function getDocumentContext() As NotesDocument
On Error GoTo returnEmptyDocument
'// Returns NotesSession.DocumentContext if availabe, else a temporary document
Dim s As New NotesSession()
Set getDocumentContext = s.documentContext
If getDocumentContext Is Nothing Then GoTo returnEmptyDocument

Exit Function
If contextDoc Is Nothing Then Set contextDoc = s.currentDatabase.createDocument()
Set getDocumentContext = contextDoc
Exit Function
End Function
contextDoc is declared as a global variable.

Wherever in your code where you fetch NotesDocument.DocumentContext, you have to change that to call the above function, getDocumentContext().

To test code, all you have to do is to create an agent, bind getDocumentContext to a NotesDocument variable (or modify the global var), set the needed fields, and you're ready to debug the code.

Call getDocumentContext()
Call contextDoc.replaceItemValue( "someNeededField", "someTestValue" )
Call methodThatsDesignedForWQOOrWQS()
Share and enjoy!