Search code examples
lotus-noteslotus-dominolotusscriptlotus

Object variable not set issue while adding text in to a notes document field


I did create a new form, where I have a table with 2 column's.

The last row of this table is the status.

I would like to do this: While my Notes Agent is running, it will go trough few action stepts. If one of the steps done, it should add the result of this action into a cell that I have definded.

Something like this:

----------------------------------------
| First action was done successfully.  |
| Second action was done successfully. |
| Third action was done successfully.  |
----------------------------------------

I tried the following code:

Sub Initialize
    Dim sess As NotesSession    
    Dim db As NotesDatabase
    Dim doc As NotesDocument    
    Dim item As NotesItem

    Set db = sess.CurrentDatabase    
    Set doc = db.GetDocumentById("100B")

    'do some code stuff.....

    Set item = doc.AppendItemValue( "field_Status", "First action was done successfully." & Chr(13) )
    Call doc.Save( False, True )

    'do some code stuff.....

    Set item = doc.AppendItemValue( "field_Status", "Second action was done successfully." )
    Call doc.Save( False, True )
End Sub

Unfortunately I get always this message:

Object variable not set

Im not sure what is missing and if this is actually the right way how to realize my idea in code. I hope you have an idea / hint for me. Thank you.


Solution

  • First of all: NEVER write even one line of code without error handler. That said: The easiest error handler would be

    On Error Goto ErrorHandler
    
    ...your complete code goes here
    
    EndOfRoutine:
      Exit Sub
    ErrorHandler:
      Messagebox Err & "," & Error & " in line " & Erl
      Resume EndOfRoutine
    

    This would have given you this error line:

    Set db = sess.CurrentDatabase  
    

    But wait: WHICH Object variable is not set?

    Its "sess", as you only wrote:

    Dim sess as NotesSession
    

    Assigned, but not set... you need either:

    Dim sess as New NotesSession
    

    Or if you want to keep your line then just before the assignment of db:

    Set sess = New NotesSession
    

    Additional advice: finding a document via its NoteID is not a good Idea, and hardcoding it is even worse: the NoteId changes in different replicas. The same document will have a different NoteId in another replica of the database (e.g. A local replica or in a cluster). And if someone deletes that document and recreates it, then you need to update your code. Use a sorted view and GetDocumentByKey with a specific key you set in your document or a Profile document to store this information. Or at least use DocumentUniqueId, as this Is the same in every replica (though the recreation issue stays the same)