1
votes

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)

2
It would really help if you would show your code.Richard Schwartz

2 Answers

2
votes

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.

1
votes

one option is to save the NotesDocumentUNID, the database path and the server from the old document in three fields in the new Document.

Then create in the design of the new document an button, which opens the old Document in the old database over these two fields.

create new document

Dim workspace As New NotesUIWorkspace
Dim session As New NotesSession
Dim oldDB As NotesDatabase
Dim oldDoc As NotesDocument
Dim newDoc As NotesDocument
Dim newDB As NotesDatabase

Set oldDB = session.CurrentDatabase
Set oldDoc = workspace.CurrentDocument.Document
Set newDB = session.GetDatabase("SERVER", "FILE")
Set newdoc =newdb.CreateDocument

newdoc.oldDocUNID= olddoc.UniversalID
newdoc.oldDBpath=oldDB.FilePath
newdoc.oldDBServer = oldDB.Server

Call newdoc.save(True,False,True)

Create Button to open oldDoc

Sub Click(Source As Button)
Dim workspace As New NotesUIWorkspace
Dim session As New NotesSession
Dim newdoc As notesdocument
Dim oldDB As notesdatabase
Dim oldDoc As notesdocument


Set newdoc = workspace.CurrentDocument.Document
Set oldDB = session.GetDatabase(newdoc.oldDBServer(0),newdoc.oldDBpath(0))
Set oldDoc = oldDB.GetDocumentByUNID(newDoc.oldDocUNID(0))
Call workspace.EditDocument(True,oldDoc)


End Sub

DocLink option

Dim workspace As New NotesUIWorkspace
Dim session As New NotesSession
Dim oldDB As NotesDatabase
Dim oldDoc As NotesDocument
Dim newDoc As NotesDocument
Dim newDB As NotesDatabase
Dim rtitem As NotesRichTextItem

Set oldDB = session.CurrentDatabase
Set oldDoc = workspace.CurrentDocument.Document
Set newDB = session.GetDatabase("SERVER", "FILE")
Set newdoc =newdb.CreateDocument

Set rtitem = newdoc.CreateRichTextItem("body")
Call rtitem.AppendDocLink(olddoc, "title")


Call newdoc.save(True,False,True)