Making open document a response to a new document

As part of a migration to a new database structure I have created a button in a source document (thisSource) to perform the following actions: - create a new order document (targetOrder)

- populate new order from fields in the open source document (thisSource) 

- make the source document (thisSource) a response to a new order document (targetOrder)

- change form used for thisSource to new form ("fResponse")

Currently, I am trying to identify the targetOrder by finding the last record in a view, but is there a better way? Can I capture the uniqueID of the newly created targetOrder and use that instead?

Also, would it be better to have the button in a view and use a selected document as a source instead?

Another refinement might be to prompt the user whether they want to make the source document a response to an existing order document or to create a new order document, as I’ve tried to do here.

You’ll see from this code that I’m a LotusScript novice. This is my non-working code - I’ve commented what I’m trying to achieve at each point - hopefully this might explain where I’m going wrong!

Your suggestions very very welcome!

Subject: making open document a response to a new document

Here’s the code I need to fix…

Sub Click(Source As Button)

   Dim workspace As New NotesUIWorkspace

 Dim session As New NotesSession

 Dim db As NotesDatabase

 Dim view As NotesView

 Dim doc As NotesDocument

'not sure if this correct - want to use current open document as

“thisSource”

Dim thisSource As NotesDocument

'use “targetOrder” as the name for the newly created order document

Dim targetOrder As NotesDocument

'not sure why/if I need this

Dim form As NotesForm

Dim uidoc As NotesUIDocument

'not sure why/if I need this

Dim item As NotesItem

Set db = session.CurrentDatabase

'view “vOrdersMigrate” ordered list of order documents created using form

“fNSO”

Set view = db.GetView( "vOrdersMigrate" )

'set target as last order document in list

Set targetOrder = view.GetLastDocument

'source document as current open document - this seems to be a problem

Set thisSource = workspace.CurrentDocument

'create new order Set uidoc = workspace.ComposeDocument(“”,“”,“fNSO”)

Set doc = uidoc.Document

Call doc.Save( True, True )

'should I close new order document to move on?

'Call doc.Close(True)

'Populate fields in Order from NCI

'for example, copy value from field Subject in thisSource document to

Subject in targetOrder

Set item = targetOrder.ReplaceItemValue( "Subject", thisSource.Subject 

)

'or is this the correct syntax?

targetOrder.Subject = thisSource.Subject   

'Save and close Order - return focus to thisSource?

Call targetOrder.Save( True, True )

'make thisSource a response to the newly created order document - now the

last Order in the view “vOrdersMigrate”

Call thisSource.MakeResponse(targetOrder)

'change the form used for thisSource to new form (“fResponse”)

thisSource.Form = "fResponse"

'or is this the correct syntax?

Set form = db.GetForm("fResponse")

'save changes

Call thisSource.Save( True, True )

End Sub