I got problem with loop in Ls

Hi guys, I am creating a button, by which it detect a number of selected document in view, and then compose a new document on each of the selected document.

The problem is, when I select 3 documents in the view and run the code, instead of generating one new document on each selected document, it generate 3 same document on the first selected document. Below is the code, hope you guys can help me out… I suspect the problem is with the GetNextDocument.

Dim session As New NotesSession

Dim collection As NotesDocumentCollection

Dim doc As  NotesDocument





Dim workspace As New NotesUIWorkspace

Dim db As NotesDatabase

Dim Uidoc As NotesUIDocument





Set db= session.CurrentDatabase

Set collection = db.UnprocessedDocuments

Set doc = collection.GetFirstDocument





While Not(doc Is Nothing)

	

	

	Set Uidoc =  workspace.ComposeDocument( "", "", "InvoiceForm" )

	Call Uidoc.Save

	Call Uidoc.Close

	

	Set doc = collection.GetNextDocument(doc)

	

Wend

Subject: don’t use composedocument

If you’re using back end classes, and you don’t need the user to see the new document (which you don’t by the looks of things as you close it stragith away), just create the new document in the back end as well

Dim session As New NotesSession

Dim collection As NotesDocumentCollection

Dim doc As NotesDocument

Dim workspace As New NotesUIWorkspace

Dim db As NotesDatabase

Set db= session.CurrentDatabase

Set collection = db.UnprocessedDocuments

Set doc = collection.GetFirstDocument

dim newdoc as notesdocument

While Not(doc Is Nothing)

set newdoc = db.createdocument

newdoc.form = “InvoiceForm”

call newdoc.computewithform(false, false)

call newdoc.save(true, true)

Set doc = collection.GetNextDocument(doc)

Wend

This will just compute the default values for the new document, if you’re using composedocument for inheritance you will have to do that work manually (get the values of doc and write to the correct fields in newdoc)

Subject: thx for the solution

Thanks for the proposed solution Sir. Besides, I had discovered a different approach. It has combination of both LS and formula language.

For the button


@Command([ToolsRunMacro];“Scount”);

@Command([ToolsRunMacro];“Composer”);

@Command([FileCloseWindow]);


Below is the code for agents

Scount (Agent with target as selected doc)

======

Sub Initialize

Dim s As New NotesSession

Dim db As NotesDatabase

Dim coll As NotesDocumentCollection

Dim count As Integer

Dim doc As NotesDocument

Dim w As New NotesUIWorkspace

Dim uidoc As NotesUIDocument





Set db = s.CurrentDatabase

Set coll = db.UnprocessedDocuments







Set uidoc = w.EditDocument

Call uidoc.FieldSetText("count",Cstr(coll.count))

Call uidoc.Save

Call uidoc.Close

End Sub

Composer (Agent with target as None)

====================================

@For(n :=1; n<=@ToNumber(@GetField(“count”)) ; n:=n+1 ;

@Command([Compose]; “InvoiceForm”);

@Command([FileSave]);

@Command([FileCloseWindow]);

@Command([NavNextSelected])

)


I know this method is long, but I personally more prefer formula language. Hence I use LS to count the number of selected doc, and setfield into a hiddenfield.

Formula will do all the rest, it will getfield from the hidden field, and set the number of loop should run.