Issue with GetDocumentByKey?

Hello,

I’m having a problem with the agent to write to the document in other database(A_db) from the current database, and copy the current document to A_db. The agent is triggered in the postsave event in the form. The current document has “Title” field(text) and “RevNum” field(number). The key for GetDocumentByKey is

doc.Title(0) & “-” & CStr(doc.RevNum(0) - 1)

The A_db has the document with the same title as the current database, and RevNum that has one less number than the document in current database. In fact, this agent works most of the time. However, it doesn’t work only for the certain documents. When the agent works, it writes “1” to “MoveToArchive” field in the selected document to remove the document from the MainView, then copy the current document to db_A. For these problematic documents, seems like it writes “1” to both the selected document, and the document that is just copied to db_A. Therefore, both documents are removed from “MainView”…

Theoretically, GetDocumentByKey would grab only one document, so I don’t understand why it grabs two documents. It doesn’t error out either. Here is the code. I would appreciate if anyone can help me. Thank you very much in advance.

Dim w As NotesUIWorkspace
Dim session As New NotesSession
Dim db As NotesDatabase
Dim A_db As New NotesDatabase( “”, “” )
Call A_db.Open( “ServerA”, “DatabaseA.nsf” )
Dim doc As NotesDocument
Dim uidoc As NotesUIDocument

Set w = New NotesUIWorkspace
Set db = session.CurrentDatabase
Set uidoc = w.CurrentDocument
Set doc = uidoc.document

If Not (doc Is Nothing) Then

'Write 1 to MoveToArchive field in to be deleted doc
Dim view As NotesView
Dim key As String
Dim ToBeDelDoc As NotesDocument
If doc.RevNum(0) > 0 Then

key = doc.Title(0) & “-” & CStr(doc.RevNum(0) - 1)
Set view = A_db.GetView( “MainView” ) '1st column is Title-RevNum, categorized
Call view.Refresh
Set ToBeDelDoc = view.GetDocumentByKey(key , True )
If Not(ToBeDelDoc Is Nothing) Then
ToBeDelDoc.MoveToArchive = “1”
Call ToBeDelDoc.Save(False, False)
End If

End If

'copy document to A_db
Call doc.CopyToDatabase( A_db )

End If

Subject: $ConflictAction?

Thank you very much for your advice!

  1. A_db, db_A, yes they are the same thing.

  2. Index property for the view - Refresh: Auto, after first use, Discard: if inactive for 45 days, and restrict initial index build was unchecked.

  3. I’m not familiar with indexer task…

  4. The agent might be executed in DatabaseA - I will specify the db, instead of db=session.CurrentDatabase just to eliminate this possibility.

  5. I believe doc is pointing to the proper document since it returned the correct values always.

One thing I noticed today is, the docs with problems have $ConflictAction = “1”. Could it cause the issue?

I will re-write the code as you recommended. If you or anyone can advise me about $ConflictAction field, I would appreciate a lot!

Thank you very much again!

Subject: Possible replicaton conflict docment

Unexpected document could be a replication conflict document. $ConflictAction = “1” is a Form attribute that results in a replication conflict document being generated when two users edit and save the same document and the changes cannot be resolved. If you look at the Form infobox (properties), you’ll see the settings for ‘Conflict Handling’ (Create Conflicts = “1”, Merge Conflicts = “2” …). You should see the Replication conflict document as a response document in a view that has the document in question (as long as the View property ‘Show response documents in a hierarchy’ is selected). You can also create a view that shows all you replication conflict documents by using the following view selection formula:

SELECT (@IsAvailable($Conflict))

AND make sure the view property ‘Show response documents in a hierarchy’ is NOT selected

Subject: Possibility: View is out of date

Note: there is some confusion in terminology: A_db, db_A. Are these the same?

Although you look up the document with a GetDocumentByKey preceded with a vw.Refresh call, the view may not be up to date. There may be several reasons.

Can you look at the view properties of the view ‘MainView’? How are the Index properties set on the Advanced tab? If the view is set to be updated at most every x hours, then a Refresh call sooner than the specified interval WILL NOT UPDATE the index, and so might return the wrong document.
This is relatively easy to fix, by choosing a smaller interval or no interval.

Another reason may be that the indexer task is overwhelmed by stuff to do. In that case, tuning of the Domino environment is in order, or a rearchitecture this part of the application.

Another reason may be this code is executed in DatabaseA

Another reason may be the way the current document is obtained. I see no check for that in the code, it just takes whatever is the CurrentDocument from NotesUIWorkspace. This might not be the same document as the one whose PostSave is running…

Also, personally I have learned to abstain from the extended class syntax to set or retrieve NotesItem values. Using them for set results in ugly uppercase itemnames, using them for read results in Variants which can result in unexpected behaviour.

Thus the code would become:

If Not (doc Is Nothing) Then

'Write 1 to MoveToArchive field in to be deleted doc
Dim view As NotesView
Dim key As String
Dim ToBeDelDoc As NotesDocument
Dim vRefnum As Variant
Dim lngRefnum As Long

vRefnum = doc.GetItemValue(“RevNum”)(0)
If IsNumeric(vRefnum) Then
If doc.RevNum(0) > 0 Then
lngRefnum = Clng(vRefnum)
key = doc.GetItemValue(“Title”)(0) & “-” & CStr(lngRevNum - 1)
Set view = A_db.GetView(“MainView”) ’ 1st column is Title-RevNum, categorized
Call view.Refresh
Set ToBeDelDoc = view.GetDocumentByKey(key, True)
If Not(ToBeDelDoc Is Nothing) Then
ToBeDelDoc.MoveToArchive = “1”
Call ToBeDelDoc.Save(False, False)
End If

End If

'copy document to A_db
Call doc.CopyToDatabase( A_db )

End If