Hello there.
I am working on an application that searches an employee’s document using the employee’s serial number. When a user inputs a serial number, the agent will search through the view to find the document with the same serial number and extracts the information from the employee’s document to a newly created document. But I am currently experiencing a problem.
The agent works perfectly looping through the first 240 documents, but once it reaches the 241 document, it keeps giving me “Object variable not set” messages. I checked through the Lotusscript debugger to see what’s wrong, and it turns out the NotesDocument variable I was using becomes empty when it reaches the 241th document. The first 240 documents are correctly assigned to the variable though.
I tested again by assigning other documents later than the 241th document, such as 270, 400, but it keeps giving me the same error messages.
I also tested it on a different database, but the agent works correctly. So I am wondering what’s wrong with database? Is it the database’s fault somewhere in the 240th document and above that I cannot access? Or is there something wrong with my codes?
Here is a portion of the codings that I have done:
For x = 1 To view.AllEntries.Count
Set doc = view.GetNthDocument(x)
Set col = view.Columns(2)
item = col.ItemName
serialNo2 = Join(doc.GetItemValue(item))
If(serialNo2 = serialNo1) Then
Set uidoc2 = workspace.EditDocument(True, doc)
serialNo= uidoc2.FieldGetText("C_StaffSerialNo")
empName = uidoc2.FieldGetText("C_StaffName")
employDate = uidoc2.FieldGetText("C_DateJoined")
title = uidoc2.FieldGetText("C_PRGAbbrev")
salary = uidoc2.FieldGetText("C_SalaryAmt")
empName = Strconv(empName,3)
title = Strconv(title, 3)
Call uidoc.FieldSetText("serialNo", serialNo)
Call uidoc.FieldSetText("StaffName", empName)
Call uidoc.FieldSetText("DateEmployed", employDate)
Call uidoc.FieldSetText("PositionTitle", title)
yarra = Split(salary, dollar)
salary = Ccur(yarra(1))
salary = Format(salary, "Fixed")
Call uidoc.FieldSetText("Salary", salary)
Call uidoc2.Close
Exit For
Else
End If
Next x
Any form of help is greatly appreciated. Thanks!
Subject: NotesDocument fails at document No. 241 when looped.
A couple of points about your code that may help.
First, you should avoid using the getNthDocument method if possible when sequentially traversing a document collection. It’s quite expensive. Start at the beginning and use the getnextdocument method. It’s significantly faster.
(see designer help: “Using getNthDocument to iterate through a loop is strongly discouraged for performance reasons.”)
Rather than traversing a notes document collection, consider finding the document by serial number by using a view keyed on that serial number. It’s far and away the simplest and most efficient means of getting one particular document, especially if you retrieve a view entry rather than a document.
When writing script, always use options declare. Trust me, just do it.
As for why the 241st document fails, I don’t know. You might try making sure that you do a view.refresh before accessing the view properties. However, simplifying the code as suggested above is much more likely to aid your problem resolution.
Kind regards
Subject: NotesDocument fails at document No. 241 when looped.
Check the database where this agent fails and see if you have a replication/save conflict document. If you do, get rid of it. I had a similar thing happen to me in an agent and in my case, it was the replication/save conflict document causing the issue.
–Mark
Subject: RE: NotesDocument fails at document No. 241 when looped.
Since you have little to no control over if and when save or replication conflicts might be created, the more robust approach would be to harden your code against the error condition, that might arise.
Subject: RE: NotesDocument fails at document No. 241 when looped.
Well, now that this user knows that this issue might be caused by a save conflict, then yes, the code can be modified to check for this.
–Mark
Subject: NotesDocument fails at document No. 241 when looped.
No offense, but my head hurts after reading that code. As Simon points out, you’re doing several inefficient things there. Optimally, you should have a lookup view sorted on the serial number you’re keying on and just use view.GetDocumentbyKey(). But even using your current view, you’re doing several things the hard/inefficient way - GetNthDocument, getting the item name form the viewcolumn formula, opening each doc, opening a new UIdoc to extract text strings to then stuff back into the already open UIDoc. Here’s a different version utilizing viewentry.ColumnValues that should be (much?) faster:
Dim vec As NotesViewEntryCollection
Dim ventry As NotesViewEntry
Dim key As String
Set vec = view.AllEntries
Set ventry = vec.GetFirstEntry
Do Until ventry Is Nothing
key = Join( ventry.ColumnValues(2))
If key = serialNo1 Then
Set doc = ventry.Document
With doc
Call uidoc.FieldSetText("serialNo", .C_StaffSerialNo(0))
Call uidoc.FieldSetText("StaffName", Strconv( .C_StaffName(0), 3))
Call uidoc.FieldSetText("DateEmployed", .C_DateJoined(0))
Call uidoc.FieldSetText("PositionTitle", Strconv( .C_PRGAbbrev(0), 3))
yarra = Split( .C_SalaryAmt(0), dollar)
salary = Ccur(yarra(1))
salary = Format(salary, "Fixed")
Call uidoc.FieldSetText("Salary", salary)
End With
End If
Loop
Hope this helps,
-jerry