This script should grab the first document of a collection and then look at each of the rest of the selected documents comparing them to the first doc.Then when the last doc is compared to the first, move from the first doc to the second doc and do the comparing again.
The way the inner while loop in the script is now written, it always gets the same doc (the second doc) and stays in an endless loop.
I understand why it always and now need help with the best way to modify it to do what I previously explained.
Thanks in advance for the help.
Here’s the script:
Sub Initialize
…
Set session = New NotesSession
Set db = session.CurrentDatabase
Set m_collectionInbox = db.UnprocessedDocuments
Set docMain = m_collectionInbox.GetFirstDocument 'get the example email
Set docConflict = m_collectionInbox.getnextDocument(docMain) 'get next email to compare
While Not docMain Is Nothing
Set itemMain = docMain.GetFirstItem("Body") 'get the Body (Rich text field) item of the main document
While Not docConflict Is Nothing
Call process_items
Set docConflict = m_collectionInbox.getnextDocument(docMain)
Wend
Set docMain = m_collectionInbox.getnextDocument(docMain) 'get next email to compare
Wend
End Sub
Sub process_items
Set itemConflict = docConflict.GetFirstItem(“Body”)
If Strcompare(outputMain, outputConflict) = 0 Then
FrmIsDifferent = False 'strings are the same
End If
…other processing if strings are the same
End Sub
Subject: Recursion!
My code is slightly different than yours, but try it in email and look at the agent log to see best how it works.
If you take recursion, you would actually go through the documents backwards, but they would all be compared to each other. Basically keep calling the same sub until it finds the very last document. It will then back up a step, and compare the second to last document to last document. It will then back up another step and compare the third to last document to the second to last document and the third to last document to the last document. It will keep doing this until it gets back to the first document and compares that to all the ones below it.
Once you try this code, you’ll see how to adapt it to yours. If you’d like a better explanation for recursion (although it will probably make more sense if you see it in action first) or would like more detailed code, let me know. Or maybe someone else can explain recursion better than me.
Below find the example code:
Declarations:
Dim agentLog As NotesLog
Dim coll As NotesDocumentCollection
Initialize:
Initialize
Dim sess As New NotesSession
Dim currdb As NotesDatabase
Set currdb = sess.CurrentDatabase
Set coll = currdb.UnprocessedDocuments ' this has to be declared in declarations for this to work
Dim firstDoc As NotesDocument
Set agentlog = New NotesLog("Recursive Process")
agentlog.OpenAgentLog
Set firstDoc = coll.GetFirstDocument
Call Process (firstDoc)
Sub Process(doc As NotesDocument)
Dim nextDoc As NotesDocument ' this one has to be declared locally to the sub for this to work
Set nextDoc = coll.GetNextDocument(doc)
' see if we get a next document, if so, pass it through to be processed, this pass through will go till we are on the very last document
If Not (nextDoc Is Nothing) Then
Call Process(nextDoc)
End If
' once it gets all the way to the bottom it will start going backwards to the top, processing all docs below
' all docs that called down to the next doc will pick back up here once the doc below it is finished processing
' start loop with next doc (as comparison point)
Do Until nextDoc Is Nothing
'*** do comparison between doc and nextDoc here
agentlog.LogAction "comparing " & doc.Subject(0) & " to " & nextdoc.Subject(0)
' keep looping
Set nextDoc = coll.GetNextDocument(nextDoc)
Loop
' once it is done processing the docs for the current document, it will return to the one that called it
' which will cause the one that called it to compare to all the docs below it
End Sub
Subject: RE: Recursion!
Thank you so much for replying! I’ll give it a try and let you know how it works. This helps so much! Thanks again! (it may not be until next week that I’ll get back in touch - THANKS!)
Subject: RE: Recursion!
I don’t usually use recursion because of all the sub function calls. If you have only a few hundred it probably won’t be bad but if you have a few thousand it could possibaly crash. Depends on your system.
You can use your code I modified the inner while loop to point to the right document collection that you wanted to check below:
While Not docMain Is Nothing
Set itemMain = docMain.GetFirstItem(“Body”) 'get the Body (Rich text field) item of the main document
While Not docConflict Is Nothing
Call process_items
Set docConflict = m_collectionInbox.getnextDocument (docConflict)
Wend
Set docMain = m_collectionInbox.getnextDocument(docMain) 'get next email to compare
Set docConflict = m_collectionInbox.getnextDocument(docMain)
Wend
This will loop through all of the documents that you wanted to check and the inner loop will get the next document of the outer loop.
Subject: RE: Recursion!
If you can keep your object references cleaned up in your loops it will help keep recursion from crashing. That’s not always possible, though, depending on how your recursion wraps up your data.
Subject: RE: Recursion!
Thank you so much for responding and offering a great correction. The agent will be used on databases that have tens of thousands of emails so I appreciate your input.
I implemented it and quickly found that an additional check was needed for the last record.
Set docMain = m_collectionInbox.getnextDocument(docMain)
If Not docMain Is Nothing Then
Set docConflict = m_collectionInbox.getnextDocument(docMain)
End If
After the last email is found, the first getnextDoc command sets docMain to Null and so the second getnextDoc command fails when trying to get a next doc from Null. Is there a better way to check for this instead of using an IF statement?
Thanks for your time and help!
Subject: RE: Recursion!
You could do something like this to skip the “IF” statment:
Do Until doc Is Nothing
Set nextDoc = view.GetNextDocument(docMain)
Set docConflict = nextdoc
Do Until docConflict Is Nothing
.
.
.
Set docConflict = view.GetNextDocument(docConflict )
Loop
Set doc = nextDoc
Loop
This will compair every document to the other and at the end of the list will terminate the loop.