Tweak to LS Agent to act on all docs in view

Hi all. Need a tweak on my LS agent that is hopefully obvious to some. I have a scheduled Simple Action agent for documents in a select view that performs 3 separate actions. One of these actions is a LS agent that composes an email harvesting the addressee and custom body text. This LS agent alone works fine when run on selected doc from Action menu. It also works when part of the scheduled Simple Action agent, BUT it'll email only the first doc in the view (while the rest of the actions perform on all the docs in the view. My LS code below, and feel like UnprocessedDocuments and/or GetFirstDocument is the culprit? Don't think I'm not looking to loop anything here, as the Simple Action agent is acting on each doc. But the LS email acts only one one.

The ultimate goal is to send an email whenever a new document is created in the database, customized SendTo and Body from document contents. Can accomplish 90% of this w/o LS, but Formula does not work with "Send on Behalf" agent property, and the Send Mail Message action does not allow for Formula text for body (though it does for To, cc:, bcc: and Subject - not Body). Thanks for any insight.

Sub Initialize

Dim Session As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim dc As NotesDocumentCollection

Set db = session.CurrentDatabase
Set dc = db.UnprocessedDocuments
Set doc = dc.GetFirstDocument

Dim memo As NotesDocument
Set memo = New NotesDocument( db )

Dim bodytext As Variant
bodytext = doc.GetItemValue( "body_field" )
Call memo.ReplaceItemValue( "Body", bodytext )

Call memo.ReplaceItemValue( "Subject", _
"plain text Subject" )
Call memo.ReplaceItemValue( "Form", "Memo" )

Dim EmailTo As Variant
EmailTo = doc.GetItemValue( "Email_Address" )
Call memo.Send( False, _
EmailTo )

End Sub

You DO need to loop yourself (not like simple actions or formulas agents)

' add this to your declaration

dim nextdoc as notesdocument

Then:

'//the loop

....

Set doc = dc.GetFirstDocument

while not doc is nothing

Set nextdoc = dc.getNextDocument(doc)

.... your code with dim memo.....

set doc = nextdoc

wend

....

Thank you Jerome. This makes sense now that I better understand what's happening with the "parent" agent, which triggers this to run. So the loop is necessary and works as expected. Very grateful.

You’re welcome Eric!

The code will work only on first document as you havenot take handle of next document at all.

It must be done in a loop.

Check if below code works.

********

Sub Initialize

Dim Session As New NotesSession
Dim db As NotesDatabase
Dim doc As NotesDocument
Dim dc As NotesDocumentCollection

Dim memo As NotesDocument

Dim EmailTo As Variant

Set db = session.CurrentDatabase
Set dc = db.UnprocessedDocuments
Set doc = dc.GetFirstDocument

While Not(doc Is Nothing)


Set memo = New NotesDocument( db )

Dim bodytext As Variant
bodytext = doc.GetItemValue( "body_field" )
Call memo.ReplaceItemValue( "Body", bodytext )

Call memo.ReplaceItemValue( "Subject", "plain text Subject" )
Call memo.ReplaceItemValue( "Form", "Memo" )


EmailTo = doc.GetItemValue( "Email_Address" )
Call memo.Send( False, EmailTo )

Set doc = dc.GetNextDocument(doc)
Wend

End Sub

********

I am not near laptop with Notes Setup as of now to give complete working code.

So, test the same.

However, you can refer to information present in below URL to understand how "While" loop can be used to go through all the documents. There are examples explaining the same about how to use "Unprocessed documents" method.

Examples: UnprocessedDocuments property

You have also mentioned that -->

The ultimate goal is to send an email whenever a new document is created in the database, customized SendTo and Body from document contents.

In that case, please refer to below URL --->

UnprocessedDocuments

Check for section below section in above URL.

What documents are returned?

Note -->

1. If you run the above code using a button present on a view, then it again might run only on one document depending if only one document is highlighted in view. You have to select all documents before clicking the button.

If you are running the above modified code using some agent via ACTION MENU, then then it again might run only on one document. Reason being from Designer side, you might have selected to run the agent on only selected documents.

You can check the same by checking the value of TARGET field in BASICS tab for the agent.

Either you select all documents manually and then run the agent or set the value of TARGET to "ALL new and modified documents" or "All documents in database" (depending upon your requirement).

2. If you want this agent to run on specific view, then instead of using "unprocesseddocuments" , run the code on specific view. Else your code will check for all unprocessed documents present in a database.

You can skip this suggestion if you are sure that you have correct ways of running the agent from UI (maybe) so that action on only documents of your choice will happen.

If not, then modify your code accordingly.

Refer to example provided in below URL -->

Examples: GetFirstDocument method

GetView

Thank you Amit. Yes, the loop is necessary, I understand that now. And I appreciate the shared resources & explanations.