Search code examples
lotus-noteslotusscript

Set a doc link of a new document in another DB


I have an action in one database that will create a new doc in another database. It would be very handy if (at least) the new document contains a doc link to the original document right after saving the new document.

For that purpose I create a calculated RT item on the new doc's form and insert a doc link in the Querysave event by script. But the RT item is always empty. What would be the basic steps to get this working ?

(Notes 8.5)


Solution

  • You can use rtitem.AppendDocLink(doc, "Title") to create the doc link you want.

    • rtitem is the RichText item of the new created document.

    • doc is the currently selected document in first database which has the button for creating the new document.

    There is a little challenge to get a satisfying solution. If we put the following lines into button which creates the new document

    ...
    Set docSelected = session.DocumentContext
    Set docNew = dbTarget.CreateDocument()
    Set rtitem = docNew.CreateRichTextItem("Doclink")
    Call rtitem.AppendDocLink(docSelected, "Title")
    Call docNew.Save(True, True)   ' <--- that is necessary :(
    ...
    Call workspace.EditDocument(True, docNew)
    

    then it creates a link in new document and it is visible immediately. BUT, it works only if we save the new document. This might be not a good idea because the document stays in database even when the user just closes the document without saving.

    So, we have to look for another solution.

    Let's turn in around and let the new document create the doc link after it was saved by user. For this, we have to give the new document the information about database and UniversalID of the link document. We do that in button which creates the new document

    Set docNew = dbTarget.CreateDocument()
    docNew.LinkDb = session.CurrentDatabase.FilePath
    docNew.LinkUniversalID = session.DocumentContext.UniversalID
    

    In new document's form we create a computed RichText field "Doclink". When is a good time to fill the field with the link? As I figured out is the Querysave event too early. The field "Doclink" gets damaged during standard document save. The solution is to wait until Queryclose and to set the link there. But, we should to do it only if user saved the document and only one time. That's why we have to monitor the Querysave event and set a flag there that user saved document. At Queryclose we create the link, delete the fields "LinkDb" and "LinkUniversalID" and save the document again if the save flag is set and document contains field "LinkDb".

    Here is the code for form:

    (Declarations)
    Dim bSaved As Boolean
    
    Sub Initialize
        bSaved = False
    End Sub
    
    Sub Querysave(Source As Notesuidocument, Continue As Variant)
        bSaved = True
    End Sub
    
    Sub Queryclose(Source As Notesuidocument, Continue As Variant)
        If bSaved Then
            Dim session As New NotesSession
            Dim doc As NotesDocument
            Dim dbLink As NotesDatabase
            Dim docLink As NotesDocument
            Dim rtitem As NotesRichTextItem
            Set doc = Source.Document
            If doc.HasItem("LinkDb") Then
                Set dbLink = session.GetDatabase(doc.ParentDatabase.Server, doc.LinkDb(0), False)
                Set docLink = dbLink.GetDocumentByUNID(doc.LinkUniversalID(0))
                doc.RemoveItem("Doclink")
                Set rtitem = doc.CreateRichTextItem("Doclink")
                Call rtitem.AppendDocLink(docLink, "Link")
                doc.RemoveItem("LinkDb")
                doc.RemoveItem("LinkUniversalID")
                Call doc.Save(True, True)
            End If
        End If
    End Sub
    

    It is important to delete the RichText field and create it new before we add the doc link.