Home > Domino Tips > Developer > LotusScript > GetLastDocumentByKey
Domino Tips:
EMAIL THIS
 TIPS & NEWSLETTERS TOPICS 

LOTUSSCRIPT

GetLastDocumentByKey


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


Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


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.




Digg This!    StumbleUpon Toolbar StumbleUpon    Bookmark with Delicious Del.icio.us   


RELATED CONTENT
LotusScript
How to clear a Lotus Notes local cache with LotusScript
Create a non-specified date picker with LotusScript code
LotusScript accesses clipboard to view copied Notes documents
LotusScript action button manages Lotus Notes mail files
LotusScript sorts a Lotus Notes document collection
Display Lotus Notes user group membership details in a tree view
Alternate version of @Command forwards subform via LotusScript
Process large arrays in Notes forms without undue coding or testing
How to use LotusScript to modify a Lotus Notes view selection
Display a custom message box using a LotusScript-generated button

RELATED RESOURCES
2020software.com, trial software downloads for accounting software, ERP software, CRM software and business software systems
Search Bitpipe.com for the latest white papers and business webcasts
Whatis.com, the online computer dictionary

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.



Domino & Lotus Notes Security Solutions: Authentication, Antispam, Encryption and Antivirus
HomeTopicsITKnowledge ExchangeTipsAsk the ExpertsMultimediaWhite PapersDomino IT Downloads
About Us  |  Contact Us  |  For Advertisers  |  For Business Partners  |  Site Index  |  RSS
SEARCH 
TechTarget provides enterprise IT professionals with the information they need to perform their jobs - from developing strategy, to making cost-effective IT purchase decisions and managing their organizations' IT projects - with its network of technology-specific Web sites, events and magazines.

TechTarget Corporate Web Site  |  Media Kits  |  Reprints  |  Site Map




All Rights Reserved, Copyright 1999 - 2008, TechTarget | Read our Privacy Policy
  TechTarget - The IT Media ROI Experts