Help with Script Agent to Create New Documents

Hello,

I need help with an agent that I am currently working on. I developed the agent using Script(which I am not experienced with) and I’m not sure how to proceed at this point. The purpose of the agent is the following:

It will be triggered manually to run on selected documents in a view that contains student records/names. It should first display a picklist with a list of courses(from another view) and based on the course(s) selected, create a new form ‘Student Record for CEU Course’ for each student selected and populate the ‘Course Num’ field with those courses and finally save and close each document.

The Script that I have written I got from reading the help and utilizing bits and pieces of sample code so I’m not sure if it was done correctly but it does work partially. It will display the picklist of courses but after I select the courses, nothing else happens…any clues as to what I can change or add to the existing code??? Any help appreciated. Here is the script I have so far:

Sub Initialize

On Error Goto ErrorHandler

REM get the current database from the session

Dim ses As New NotesSession

Dim db As NotesDatabase

Set db = ses.CurrentDatabase

REM get the unprocessed documents

Dim col As NotesDocumentCollection

Set col = db.UnprocessedDocuments

REM process the unprocessed documents

Dim docToProcess As NotesDocument

For docCount=1 To col.Count

REM get the document in the collection to process

Set docToProcess = col.GetNthDocument(docCount)

Print "Processing document "+Cstr(docCount) + " of "+Cstr(col.Count)

Dim newDoc As New NotesDocument(db)

Dim workspace As New NotesUIWorkspace

Dim picklist As Variant

With newDoc

.Form = “Student Record for CEU Course”

picklist = workspace.PickListStrings(PICKLIST_CUSTOM, True, “dcoirm1ln/DC/USEPA/US”, _

“QS\OversightTest.nsf”, “Training Events\by Code”, “Select a training event”, _

“Please select a course this student attended”,2)

Call newdoc.FieldSetText(“Course_Num”, picklist)

.ComputeWithForm True,False

.Save True, True

End With

REM update the doc we just processed so we do not process it again

Call ses.UpdateProcessedDoc(docToProcess)

Next

Exit Sub

ErrorHandler:

Print “Error :: " +Error$+” "+Cstr(Err) + " "+Cstr(Erl)

Exit Sub

End Sub0

End Sub

Thanks!

Subject: Sure try this instead…

Sub Initialize On Error Goto ErrorHandler

REM get the current database from the session

Dim ses As New NotesSession

Dim db As NotesDatabase

Dim workspace As New NotesUIWorkspace

Dim picklist As Variant

Dim newDoc As NotesDocument

Dim docToProcess As NotesDocument

Dim col As NotesDocumentCollection



Set db = ses.CurrentDatabase

REM get the unprocessed documents

Set col = db.UnprocessedDocuments

REM process the unprocessed documents

For docCount=1 To col.Count

REM get the document in the collection to process

	Set docToProcess = col.GetNthDocument(docCount)

	

	Print "Processing document "+Cstr(docCount) + " of "+Cstr(col.Count)

	picklist = workspace.PickListStrings(PICKLIST_CUSTOM, True, "dcoirm1ln/DC/USEPA/US", _

	"QS\OversightTest.nsf", "Training Events\by Code", "Select a training event", _

	"Please select a course this student attended",2)

	If Not Isempty(picklist) Then

		Set newDoc = db.CreateDocument

		With newDoc

			.Form = "Student Record for CEU Course"

			Call .ReplaceItemValue("Course_Num", picklist)

'ammend the following line with the proper field name for the student name

			.StudentName = docToProcess.StudentName

			.ComputeWithForm True,False

			.Save True, True

		End With

	End If

REM update the doc we just processed so we do not process it again

’ no need for this only used for agents that run on uprocessed documents

’ Call ses.UpdateProcessedDoc(docToProcess)

Next

Exit Sub

ErrorHandler:

Print "Error :: " +Error$+" "+Cstr(Err) + " "+Cstr(Erl)

Exit Sub

End Sub

Subject: RE: Sure try this instead…

I was able to get much farther this time, the actual course documents for each student selected were created but I’m currently running into a problem:

If I selected 3 student records to be updated from the view, the picklist prompts me 3 times to select the courses…whereas I would like to select the courses the first time and it applies automatically to the remaining number of students selected as well. Any ideas??

Subject: RE: Sure try this instead…

Just move the “picklist=” line from inside the For loop to before the For loop.

Subject: RE: Sure try this instead…

That’s it! Thank you so much for your help.

Kenya