Open the document if found else create a new one

Hi,

I have a button in a form. What i intended it to do is this…

Find a document with UNID (act a key and is stored in the current document) and created using FormA in View A. If this document is found then Open it, else Compose a new document using FormA and inherit the UNID of the parent document.

I could do this in LotusScript by utilising the GetDocumentByKey, where if the search succeed the document is opened, else compose a new document. The problem is, this Script doesn’t work in Browser. I figure i can rewrite the program in Notes command e.g @Command([OpenView]; “My View Name”; UNID);

@Command([OpenDocument]; “1”). But this doesn’t seem to work as you can’t use UNID as key in the browser. I’ve run out of alternatives. Can anyone suggest ?

Cheers and thanks a lot for your help

VT

Subject: Open the document if found else create a new one

Change your form button to ([ToolsRunMacro]; “AgentName”) and put your LotusScript into the new agent (the Runtime Target of the agent must be None).

Your agent ends with:

         Print "[" + returnURL + "]"

where returnURL points to the doc that was found, or the newly created one.

Subject: RE: Open the document if found else create a new one

Hi Peter,

I sort of understand what you wrote earlier, but unfortunately I did not fully understand the ‘Print’ part. What is the syntax for this. Below is my code. Hope someone out there could give me ideas to solve it…

Sub Initialize

Set ss = New NotesSession

Set db = ss.CurrentDatabase



Dim view As notesview

Dim doc, DERdoc As notesdocument

Set doc = ss.DocumentContext

If doc Is Nothing Then Exit Sub



Dim ws As New NotesUIWorkspace

Dim uidoc As NotesUIDocument

'**********************************************

Set view = db.getview("DERByID")

Set DERdoc = view.GetDocumentByKey(doc.DocUniqueID(0),False)



If DERdoc Is Nothing Then 'no document is found, create a new one

	'I am not sure of what code to use here. Do i use the Print Command here. 

	'ComposeDocument works for notes client, but nothing happens in the browser.

	Set uidoc = ws.ComposeDocument( "", "", "DER")

Else 'DER created before, just open/launch it.

	'Also what code do i use here.

End If

End Sub

Help is badly needed.

Thanks a million.

Subject: RE: Open the document if found else create a new one

Hi Victor,

You’re partially on the right track.

However, you can’t use any of the front-end classes on the web, like you can’t on scheduled agents.

ie no NotesUIWorkspace nor NotesUIDocument or any of their methods(like ComposeDocument). These are only for the Notes Client.

Your code is fine up to ‘create a new one’.

In this case you could use:

If DERdoc is nothing then

Set DERdoc = db.CreateDocument

DERdoc.Form = “”

and set the rest of the DERdoc fields

Call DERdoc.Save( true, false )

End If

The Print statement is then used to redirect the browser to the DERdoc. The “[” and “]” tell Domino to treat the enclosed url as passthrough HTML:

myurl = “/” + dbpath + “/0/” + DERdoc.UniversalID + “?OpenDocument”

Print “[” + myurl + “]”

or your could use “?EditDocument” if your web user has author access to DERdoc. The “/0/” means don’t care about the view.

Have fun!

PS - Unrelated Tip: Always use ‘Option Declare’ in your agent options. This forces you to declare all variables and avoid hours of debugging when you make a small typo.

Subject: RE: Open the document if found else create a new one

Hi Peter,

Cheers for the reply. I fully undestood what you mean. What I was really looking for is tobe able to launch the document upon finding it, not just create and save it. Of course this is not a problem in browser but it won’t work in browser. Do you think I can achieve (i.e launcing found doc. in browser by coding my programme in script) ? I used the script because i need to GetDocumentByKey as I could think of a better way (JS/formula)of finding a document given a UNID. Cheers again Peter.

Subject: RE: Open the document if found else create a new one

Maybe this will help, if you see the whole thing from the parts in our various postings:

Sub Initialize

Dim ss as New NotesSession

Dim db as NotesDatabase

Dim view As NotesView

Dim doc, DERdoc As NotesDocument

Dim myurl as String

On Error GoTo ProcessError

Set db = ss.CurrentDatabase

Set doc = ss.DocumentContext

If doc Is Nothing Then Exit Sub

'assuming the context doc on which the action runs this agent contains a field named ‘DocUniqueID’ & view ‘DERByID’ is sorted by DERdoc.UniversalID

Set view = db.GetView(“DERByID”)

Set DERdoc = view.GetDocumentByKey(doc.DocUniqueID(0),True)

If DERdoc is nothing then

'DERdoc does not exist, so create it

Set DERdoc = db.CreateDocument

DERdoc.Form = “”

Call DERdoc.Save( true, false )

End If

'Now redirect the browser to DERdoc in read mode

myurl = “/” + dbpath + “/0/” + DERdoc.UniversalID + “?OpenDocument”

Print “[” + myurl + “]”

Exit Sub

ProcessError:

Exit Sub

End Sub

The new doc is created only if the existing one is not found. It is the Print statement that opens the document in the browser. It might help if you try it and see what happens for, first a doc that does exist, then one that doesn’t. dbpath needs to be the path within the your data directory- eg testing/mydb.nsf (with forward slashes).

Hope this helps you to find your solution.

Regards,

Peter.

Subject: RE: Open the document if found else create a new one

I got the creating the document part working. The problem i see now it seems that the Print doesn’t do it’s job of launcing the webpage. Just to test it’s functionality, I did this in the Agent…

Sub Initialize

Print “[http://www.yahoo.com]”

End Sub

I’m expecting it to launch the yahoo homepage but nothing happend when i tested it in the browser. Am i missing something here. The agent is triggered via @Command([ToolsRunMacro];“(Find DER)”).

Cheers