Friday, 6 April 2018

The Power of LotusScript Part III - Quick Tip - LotusScript Routines OpenDocumentInUI - OpenDocumentOtherDBInUI


In this third part of the new blog series 'The Power of LotusScript' a handy tip to open a document in the current database using LotusScript as well as to open a document in another database using LotusScript. In my Notes applications I use the LotusScript routines below for this which I added in a Script Library. Below a short description and the sample code of the LotusScript routines.
On the Notes Form I initially add a field including an Action Hotspot in which eventually the LotusScript is added in the click event for opening the document in the current database or other database. In the (Options) of the Action Hostspot I add the name of the Script Library where the LotusScript routines have been added (Use 'NameScriptLibrary').
Finally in the current database and the other database in which the relevant document must be opened a (hidden lookup) View must be present sorted by the field name (key) used in the LotusScript code in the Action Hotspot (click event) of the added Field  for opening the document .
Remark: In the example below to open a document in another Notes database I use the value of a field on a Profile Form in the current database, the replicaID of the other Notes database.

The Power of LotusScript
Part I - Create New Document From UIdocument Using LotusScript
Part II - LotusScript Agent Send Email to Multiple Receipients (Per Recipient) Including a Doc Link Based on a Search Formula
Part III - Quick Tip - OpenDocumentInUI - OpenDocumentOtherDBInUI

A. Open Document in UI
As mentioned above we need to add a little bit of LotusScript code in the click event of the Action Hotspot of the added field on the Notes form to open the document in the current database. The Lotusscript code which has to be added in the click event of the Action Hotspot is as follows:

Sub Click(Source As Button)
' init
Dim s As New NotesSession
Dim db As NotesDatabase

Set db = s.CurrentDatabase

' open link in ui
Call openDocumentInUI( "($LookupViewCurrentDatabase)", "FieldName")
End Sub

Add the LotusScript sample code below in a Script Library in the Notes application.

Sub openDocumentInUI(viewName As String, fieldName As String)

' init
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim s As New NotesSession
Dim db As NotesDatabase
Dim view As NotesView
Dim doc As NotesDocument
Dim fieldValue As Variant

' check if document is selected
If Not ws.CurrentDocument Is Nothing Then

' document is selected
Set uidoc = ws.CurrentDocument

' open db and view
Set db = s.CurrentDatabase
Set view = db.GetView(viewName)
fieldValue = uidoc.Document.GetItemValue(fieldName)

' get document
Set doc = view.GetDocumentByKey(fieldValue(0))

' check if document has been found
If Not doc Is Nothing Then

' open document in workspace
Call ws.EditDocument(False, doc)
End If
End If

End Sub

B. Open Document in Other Database in UI
For opening a document in another Notes database we also need to add a little bit of LotusScript code in the click event of the Action Hotspot of the added field on the Notes form. The Lotusscript code which has to be added in the click event of the Action Hotspot is as follows:

Sub Click(Source As Button)
' init
Dim s As New NotesSession
Dim db As NotesDatabase
Dim pdoc As NotesDocument
Set db = s.CurrentDatabase
Set pdoc = db.GetProfileDocument("ProfileForm")
' open link in ui
Call openDocumentOtherDbInUI(pdoc.FieldReplicaIDNotesOtherDatabase(0), "($LookupViewOtherDatabase)", "FieldName")
End Sub

Add the LotusScript sample code below in a Script Library in the Notes application.

Sub openDocumentOtherDbInUI(repID As String, viewName As String, fieldName As String)
' error handler
On Error GoTo errorHandler
' init
Dim ws As New NotesUIWorkspace
Dim uidoc As NotesUIDocument
Dim s As New NotesSession
Dim db As NotesDatabase
Dim otherDb As New NotesDatabase("", "")
Dim view As NotesView
Dim doc As NotesDocument
Dim fieldValue As Variant
' check if document is selected
If Not ws.CurrentDocument Is Nothing Then
' document is selected
Set uidoc = ws.CurrentDocument
' open db's and view
Set db = s.CurrentDatabase
Call otherDb.Openbyreplicaid(db.server, Replace(repID, ":", ""))
Set view = otherDb.GetView(viewName)
fieldValue = uidoc.Document.GetItemValue(fieldName)
' get document
Set doc = view.GetDocumentByKey(fieldValue(0))
' check if document has been found
If Not doc Is Nothing Then
' open document in workspace
Call ws.EditDocument(False, doc)
End If
End If
Exit Sub
errorHandler:
Call AddToStackTrace()
End Sub

I hope that someone can (re)use these routines for their own applications or at least got an idea based on these routines for their own development. In my next blog posts more LotusScript Functions and Agents that I have implemented during the past period in various Notes applications. At this moment I am considering a Demo application with the different Functions, Agents and Routines that will be discussed in this blog series. If there is a need for this, please let me know.

2 comments:

  1. I'm finding these useful as my current contract is heavy in LotusScript use, so more and the Demo application would be great, as I've been most interested in what you are doing.

    ReplyDelete
    Replies
    1. Thanks very much for your reply Brain. There will be more sample Functions, Agents and Routines. I will also setup a demo application for download.

      Delete