
LOTUSSCRIPT
GetLastDocumentByKey
Sriram Panchapakesan 06.01.2002
Rating: -3.64- (out of 5) Hall of fame tip of the month winner




|
You Can View User Feedback To This Tip
How often we want to get the handle of the last document from the view based on some key value(s).. equivalent of NotesViewHandle.getdocumentByKey(). Here is the LS function getLastDocumentByKey(),which solves that purpose.
Code
Function getlastDocumentByKey ( CatValue As Variant, ViewName As String, db As Notesdatabase ) As Notesdocument
'''' This function finds the last document that matches the specified key
Dim CatView As NotesView
Dim CatEntryColl As NotesViewEntryCollection
Dim LastEntry As NotesViewEntry
Dim LastDoc As NotesDocument
Set CatView = db.GetView ( ViewName )
If CatView Is Nothing Then
Print "Error in geting last document. View not found."
Set getlastDocumentByKey = Nothing
Exit Function
End If
Call CatView.Refresh
Set CatEntryColl = CatView.GetAllEntriesByKey ( CatValue, True )
If CatEntryColl.Count = 0 Then
Print "No documents for the specified key."
Set getlastDocumentByKey = Nothing
Exit Function
End If
Set LastEntry = CatEntryColl.GetFirstEntry
While Not LastEntry Is Nothing
If LastEntry.IsDocument Then
Set LastDoc = LastEntry.Document
End If
Set LastEntry = CatEntryColl.GetNextEntry ( LastEntry )
Wend
Set getlastDocumentByKey = Lastdoc
End Function
USER FEEDBACK TO THIS TIP
- This will *not* get you the last document in the view as the ordering of documents in collections is undefined except when doing full text searches. Instead you have to do the following:
1. Find the first document with the key in the view.
2. Traverse the view until you meet a document with another key. The previous document was the last with the given key.
The code shows you how. It can also handle multi-value keys (arrays).
Code:
Function GetLastDocumentByKey(view As Notesview, key As Variant) As NotesDocument
' Gets the last document for the given key in a view.
Dim curdoc As NotesDocument
Dim previousdoc As NotesDocument
Dim keycounter As Integer
Set curdoc = view.GetDocumentByKey(key, True)
While Not (curdoc Is Nothing)
Set previousdoc = curdoc
Set curdoc = view.GetNextDocument(curdoc)
If Isarray(key) Then
For keycounter = 0 To Ubound(key)
If curdoc.Columnvalues(keycounter) <> key(keycounter) Then
Set curdoc = Nothing
Exit For
End If
Next keycounter
Else
If curdoc.Columnvalues(0) <> key Then Set curdoc = Nothing
End If ' Isarray(key)
Wend ' Not (curdoc Is Nothing)
Set GetLastDocumentByKey = previousdoc
End Function ' GetLastDocumentByKey
Morten Clausen
- hi, I would like to clarify the feedback given by Morten Clausen. The original code posted by me will "definitely" fetch the last document for the specified key. The feedback point mentioned by "Morten Clausen" is true only for the 'NotesDocumentCollection' NOT for 'NotesViewEntryCollection'. The major advantage of NotesViewCollection over NotesDocumentCollection is that it gets all the entries in the view (whether it's "document" or "category" or "total" or "average" row) in the order it appears in the view. - Sriram P
 |

|
Rate this Tip
|
To rate tips, you must be a member of SearchDomino.com. Register now
to start rating these tips. Log in if you are already a member.
|


');
// -->
DISCLAIMER: Our Tips Exchange is a forum for you to share technical advice and expertise with your peers and to learn from other enterprise IT professionals. TechTarget provides the infrastructure to facilitate this sharing of information. However, we cannot guarantee the accuracy or validity of the material submitted. You agree that your use of the Ask The Expert services and your reliance on any questions, answers, information or other materials received through this Web site is at your own risk.
|
 |
|
|
 |
|
 |