Monday, 14 November 2016

How To Delete Documents in a View using a LotusScript Agent


This blog post I write as a reminder to myself following a number of problems I've encountered when deleting documents in a view in which I use a LotusScript Agent. In LotusScript there are different ways to delete documents in a View using a LotusScript Agent. Below two examples to delete documents in a View using LotusScript. Both scripts work but the first method has a greater risk of errors causing the Agent to fail as I experienced myself last week.

In case the script below is used the first document  in the View is removed and in the next step the handle is refreshed by getting the first document in the view again.
This script may cause problems because the view index needs to be kept up to date while processing the script. In case the index is not kept up to date by Domino the next statement will get the same document that was just removed causing the Agent to fail as I have experienced myself this week. There is also a possibility (large amount of documents) for a view index corruption causing an infinite loop because the document can not be deleted (doc).

Set view = db.GetView("VIEWNAME")
Set doc = view.GetFirstDocument
While Not doc Is Nothing
   Call doc.Remove(True)
   Set doc = view.GetFirstDocument
Wend

The current manner I use is to get a handle to the next document in the View before the first document in the View is deleted. The View index never needs to be updated in the case because the script isn't forcing the index to be refreshed (the refresh will take place on its own schedule instead of on the script's schedule). Index corruption (large amout of documents) is also limited to a minimum.

Sub Initialize

' init
Dim s As New NotesSession
Dim db As NotesDatabase
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim nextdoc As NotesDocument
Dim View As NotesView
Dim agentName As String

agentName = "Delete documents from ViewName"

' Start Agent
Print "Start '" + agentName + "' ..."

' Get Database
Set db = s.CurrentDatabase

' Get View
Set view = db.GetView("VIEWNAME")
Set doc = view.GetFirstDocument
While Not doc Is Nothing
Print "Document deleted: " + doc.FIELDNAME(0)
Set nextdoc = view.GetNextDocument(doc)
Call doc.Remove(True)
Set doc = nextDoc
Wend

' Agent End
Print "End '" + agentName + "' ..."

End Sub

Another possibility is to make use of the NotesDocumentCollection class which has a 'RemoveAll' method that deletes all the documents in the collection based on a Search Formula. In case Readers or Authors fields are used the script needs to be tested because the Agent will not work in all sutuaties as I noticed during testing.

Sub Initialize

' init
Dim s As New NotesSession
Dim db As NotesDatabase
Dim dc As NotesDocumentCollection
Dim doc As NotesDocument
Dim searchFormula As String
Dim agentName As String
Dim errorMsg As String

agentName = "Delete documents"

' Start Agent
Print "Start '" + agentName + "' ..."

' Get Database
Set db = s.CurrentDatabase

' Build Query
searchFormula = {(Form = "FORMNAME") &
        (FIELDNAME = "FIELDVALUE")}

' Execute Query / Get Document Collection
Set dc = db.Search(searchFormula, Nothing, 0)

' Check if documents has been found
If dc.Count > 0 Then

' Remove documents from the database
Call dc.Removeall(True)
End If

' Agent End
Print "End '" + agentName + "' ..."

End Sub

No comments:

Post a Comment