Help with DocLink Code

I’m trying to write a script in a computed field on a form that creates a doclink in the field. I have another field (called linkTo) that uses an @dblookup to pull in the UNID of the document I want to create a link to. Here is the code I have:

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim Form As NotesForm

Dim rtitem As NotesRichTextItem

Dim linkTo



Set db = session.CurrentDatabase

Set doc = db.Forms

linkTo = doc.GetItemValue (linkTo)



Call rtitem.AppendDocLink ( linkTo, db.Title)

End Sub

I’m getting the error “Type Mismatch” when I try to open a document, and no link is being created. I’ve been developing in Notes for almost a year now, but this is my first project using Lotus Script so any help would be greatly appreciated!

Subject: Help with DocLink Code

The line giving that error is :

Set doc = db.Forms

db.Forms returns an array of forms and doc is a Notesdocument.

Anyway I don’t think you need that line: Trye with:

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

DIm currDoc AS NotesDocument

Dim doc As NotesDocument

Dim rtitem As NotesRichTextItem

Dim linkTo

Set db = session.CurrentDatabase

Set currDoc = session.DocumentContext

linkTo = currDoc.GetItemValue ( “linkTo”) (0)

Set doc = db.GetDocumentByUnid ( CSTR ( linkTo ) )

if ( currDoc.hasItem ( “link” ) ) Then

Set rtitem = currDoc.GetFirstItem ( “link” )

Else

Set rtitem = New NotesRichTextItem ( “link” )

End if

Call rtitem.AppendDocLink ( doc, db.Title)

Call currDoc.Save ( False, False )

End Sub

'if the script is launched in the client you must change the line

Set currDoc = session.DocumentContext

’ by:

Dim wks As New NotesUIWorkspace

Set currDoc = wks.CurrentDocument.Document

Subject: RE: Help with DocLink Code

thanks for the reply! When I try saving the form with your code in the field I get a type mismatch error on this line: Set rtitem = New NotesRichTextItem ( “link” )

Is this because “link” has not been defined?

Subject: RE: Help with DocLink Code

no no it is beacuse I made a mistake sorry:

change it to:

Set rtItem = New NotesRichTextItem ( currDoc, “link” )

Now it should be better :smiley:

Subject: RE: Help with DocLink Code

Ok, so here’s what I got:Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim currDoc As NotesDocument

Dim doc As NotesDocument

Dim rtitem As NotesRichTextItem

Dim linkTo

Set db = session.CurrentDatabase

Set currDoc = session.DocumentContext

linkTo = currDoc.GetItemValue ( "linkTo") (0)

Set doc = db.GetDocumentByUnid ( Cstr ( linkTo ) )

If ( currDoc.hasItem ( "HPTDLink" ) ) Then

	Set rtitem = currDoc.GetFirstItem ( "HPTDLink" )

Else

	Set rtItem = New NotesRichTextItem ( currDoc, "HPTDLink" )

End If

Call rtitem.AppendDocLink ( doc, db.Title)

Call currDoc.Save ( False, False )

End Sub

My editable rich text field where I want the link to go is called “HPTDLink”. I am currently getting a “object variable not set” error, but I’m not seeing any unset variables, other than HPTDLink. Do I need to Dim this field name?

Subject: RE: Help with DocLink Code

Where exactly does this code go??

Is this code inside a button, in an agent or in the form??

Subject: RE: Help with DocLink Code

It’s within a field in the form. I suppose I could put it in an agent instead if you think that would work better. Thanks again for all the help, I’ve learned more in the past 2 days than I have in 2 weeks fighting through it!

Subject: RE: Help with DocLink Code

ok in this case you should put in in the queryopen/postopen event of the form.

But in fact there is an easier way to do it:

In the original document create a field named “Link”, computed field with formula: @DocumentUniqueID.

In the other form where the doc link must be displayed make the field “hdLinkTo” computed when created with the formula:

id := @DbLookUp ( … );

@GetDocField ( ID; “Link” )

where the dblookup must get the source document id ( as you have already done ).

With this you are doing the following:

  1. in the first step you add a field with a link to the source document inside the source document!, it sounds strange but it is very helpful

  2. You copy that field to the document where you want to have the link, with this you have automatically a doc link to the source document!

With this solution you don’t have to use any script to get the doc link.

The problem of the script is that it will be executed each time you open the document unless you add extra checkings saving the link id and checking if exists in a field before creating it…

I suggest you to use the alternative solution with just two formulas a computed fields and forget the script.

Subject: RE: Help with DocLink Code

Thanks for the reply. This is actually what I originally planned to do (in order to aviod having to learn Lotus Script), but unfortunately I do not have access to make changes to the other database. I will try to figure out how to make it work in the query open/post open area you mentioned. Thanks again for all the help, I greatly appreciate it!

Subject: RE: Help with DocLink Code

Ok, using that method I get a “object variable not set” error when I open the doc.

For reference, here is my code:

Sub Postopen(Source As Notesuidocument)

Dim session As New NotesSession

Dim db As NotesDatabase

Dim currDoc As NotesDocument

Dim doc As NotesDocument

Dim rtitem As NotesRichTextItem

Dim linkTo

Dim HPTDLink As NotesItem



Set db = session.CurrentDatabase

Set currDoc = session.DocumentContext

linkTo = currDoc.GetItemValue ( "linkTo") (0)

Set doc = db.GetDocumentByUnid ( Cstr ( linkTo ) )

If ( currDoc.hasItem ( "HPTDLink" ) ) Then

	Set rtitem = currDoc.GetFirstItem ( "HPTDLink" )

Else

	Set rtItem = New NotesRichTextItem ( currDoc, "HPTDLink" )

End If

Call rtitem.AppendDocLink ( doc, db.Title)

Call currDoc.Save ( False, False )

End Sub