Something wrong with my code?

I wrote the following agent to minimize the number of documents in my Inbox. It’s designed to move any document more than 14 days old into the “a. Read mail” folder. Trouble is, not all documents get processed. Some of the older ones (from 2008) don’t appear to get touched at all. Can you review this code and tell me what I may be doing wrong? Thanks. Here it is:

Sub Initialize

' Move documents more than 14 days old from the Inbox

'  to the "a. Read mail" folder.  Looks at the $Created date

Const DAYS  = -14



Dim s As New NotesSession

Dim db As NotesDatabase

Dim view As NotesView

Dim doc As NotesDocument

Dim nextDoc As NotesDocument



Dim presentDateTime As NotesDateTime

Dim createdDateTime As NotesDateTime



Set db = s.CurrentDatabase

Set view = db.GetView("($Inbox)")



Set presentDateTime = New NotesDateTime( "Today" )

Call presentDateTime.AdjustDay( DAYS )



Set doc = view.GetFirstDocument

While Not(doc Is Nothing)

	Set createdDateTime = New NotesDateTime( "" )

	createdDateTime.LSLocalTime = doc.Created

	If presentDateTime.TimeDifference( createdDateTime ) > 0 Then

		' Print presentDateTime.DateOnly & " vs. " & createdDateTime.DateOnly

		Set nextDoc = view.GetNextDocument(doc)

		Call doc.PutInFolder("a. Read mail")

		Call doc.RemoveFromFolder("($Inbox)")

		Set doc = nextDoc

	Else

		Set doc = view.GetNextDocument(doc)

	End If

Wend

End Sub

Subject: Re: TimeDifference to find older docs

I believe the problem is that you’re removing the document from the folder, and then using that same document to say, get me the next document in the folder. Well, it’s not in the folder anymore, so the next document in the folder is a little ambiguous.

You can use the document selection of the agent to find your target documents, and just put the whole collection in a folder with a single call – that’s much more efficient. In fact, you don’t even need LotusScript – a Simple Action agent will work just fine.

Subject: TimeDifference to find older doc

Thanks Andre. I went with LS because the simple action agent choked on the number of documents to gather in the document selection. It occurred to me that using LS (even though it would be much slower), would do the job. I was even careful to include the following code:

Set nextDoc = view.GetNextDocument(doc)

Call doc.PutInFolder(“a. Read mail”)

Call doc.RemoveFromFolder(“($Inbox)”)

Set doc = nextDoc

to avoid the problem that you referred to.

Subject: You have to debug your agent.

You’re right; the way you’re getting the next document should avoid any problem caused by the document no longer being in the inbox. Using view.AutoUpdate = False might also help.

However, you didn’t respond to my other point, viz: there’s no reason to iterate through the view for this task. Just use the agent’s Document selection event to do a full-text search for documents that meet your criterion, and move them en masse using NotesDocumentCollection.PutAllInFolder (I think it’s called).