Agent

I have an agent in a database that I want to run and send emails to me. I have posted the code. It is not sending me the emails. What am I missing?

Sub Initialize

Dim s As New NotesSession

Dim db As NotesDatabase

Dim dc As NotesDocumentCollection

Dim view As NotesView

Dim doc As NotesDocument

Dim ndoc As NotesDocument

Dim rtitem As NotesRichTextItem

Dim visits As Integer

Set db = s.CurrentDatabase

Set view = db.GetView("all")

Set dc = view.GetAllDocumentsByKey(0, True)

firstonecreated = False

If dc.Count > 0 Then

	For i = 1 To dc.Count

		Set doc = dc.GetNextDocument(i)

		If doc.Client(0) <> "" And doc.Location(0) = "Off-site" Then

			If Not firstonecreated Then

				Set ndoc = db.CreateDocument

				Set rtitem = New NotesRichTextItem(ndoc, "Body")

				ndoc.Form = "Memo"

				ndoc.SendTo = "Laura L. Ravlin"

				ndoc.Subject = "Today's Client Visits"

				Call rtitem.AppendText("The following visits are scheduled today...Please check Account Review Items:")

				Call rtitem.AddNewLine(2)

				Call rtitem.AppendText("__________________________________")

				Call rtitem.AddNewLine(2)

				firstonecreated = True

				

			End If

			Call rtitem.AppendDocLink(doc, "Click here for details of the visit")

			Call rtitem.AddTab(1)

			Call rtitem.AppendText(doc.employee(0))

			Call rtitem.AddTab(1)

			Call rtitem.AppendText(doc.client(0))

			Call rtitem.AddNewLine(2)

			

		End If

	Next

	If firstonecreated Then

		Call ndoc.Send(False)

	End If

End If

End Sub

Subject: Agent

There are several things that look like they would prevent this agent from running properly.

  1. Set dc = view.GetAllDocumentsByKey(0, True)

Is that correct? You want to exactly match zero to the first sorted column in the “all” view? If not, then you would get no documents back, causing the program to bypass the create and send of the email.

  1. Set doc = dc.GetNextDocument(i)

That’s not the proper syntax and if you want to process all the documents in the document collection, that’s not the way to do it. One method is this:

Set doc = dc.GetFirstDocument

Do Until doc is Nothing

.

.

.

Set doc = dc.GetNextDocument(doc)

Loop

  1. It appears you’re trying to create a “newsletter”. Maybe looking into using the NotesNewletter class might make things a little easier.

Hope that helps.

Subject: Try:

Set view = db.GetView("all")	firstonecreated = False

Set doc = view.GetFirstDocument

While Not (doc Is Nothing)

	If doc.Client(0) <> "" And doc.Location(0) = "Off-site" Then

		If Not firstonecreated Then

			Set ndoc = db.CreateDocument

			Set rtitem = New NotesRichTextItem(ndoc, "Body")

			ndoc.Form = "Memo"

			ndoc.SendTo = "Laura L. Ravlin"

			ndoc.Subject = "Today's Client Visits"

			Call rtitem.AppendText("The following visits are scheduled today...Please check Account Review Items:")

			Call rtitem.AddNewLine(2)

			Call rtitem.AppendText("__________________________________")

			Call rtitem.AddNewLine(2)

			firstonecreated = True

		End If

		Call rtitem.AppendDocLink(doc, "Click here for details of the visit")

		Call rtitem.AddTab(1)

		Call rtitem.AppendText(doc.employee(0))

		Call rtitem.AddTab(1)

		Call rtitem.AppendText(doc.client(0))

		Call rtitem.AddNewLine(2)

			

	End If

	Set doc = view.GetNextDocumetn(doc)

Wend

If firstonecreated Then

	Call ndoc.Send(False)

End If

Subject: RE: Try:

Thank you this worked, but I realized I forgot a criteria. I need to send only those visits that are “today”. I added this code…

If doc.Client(0) <> “” And doc.Location(0) = “Off-site” And doc.Date(0) = Today() Then

but now it is not working when I know there are documents for today (3/16/05)…why?

Subject: RE: Try:

I would be willing to bet that doc.Date(0) is a date-time value (a opposed to date-only) internally. Today returns a date with a time of midnight (no time elapsed in the current day), so unless your documents also contain a midnight (to the exact second) value, they can’t ever be equal to Today. Try converting both values to Long before comparing them:

If doc.Client(0) <> “” And doc.Location(0) = “Off-site” And Clng(doc.Date(0)) = Clng(Today)

Subject: RE: Try:

That didn’t work…I get a Type Mismatch error…

Subject: RE: Try:

A date-time variant will convert cleanly to a long, truncated at the decimal point. Is there a valid date-time value available in the field in all documents? (An empty string will not convert to long.) Try assigning the value to a Variant first, then testing for type.

Subject: Agent

Concerning Set doc = dc.GetNextDocument(i).

GetNextDocument takes a document as a parameter.

GetNthDocument takes an integer as a parameter.

Subject: Agent

You need to use dc.getforstdoc and dc.getnextdoc see below. (please note I’ve not actually tested the code below).

Sub Initialize

Dim s As New NotesSession

Dim db As NotesDatabase

Dim dc As NotesDocumentCollection

Dim view As NotesView

Dim doc As NotesDocument

Dim ndoc As NotesDocument

Dim rtitem As NotesRichTextItem

Dim visits As Integer

Set db = s.CurrentDatabase

Set view = db.GetView("all")

Set dc = view.GetAllDocumentsByKey(0, True)

firstonecreated = False

If dc.Count > 0 Then

’ For i = 1 To dc.Count

	Set doc = dc.Getfirstdocument

	Do Until doc Is Nothing

		If doc.Client(0) <> "" And doc.Location(0) = "Off-site" Then

			If Not firstonecreated Then

				Set ndoc = db.CreateDocument

				Set rtitem = New NotesRichTextItem(ndoc, "Body")

				ndoc.Form = "Memo"

				ndoc.SendTo = "Laura L. Ravlin"

				ndoc.Subject = "Today's Client Visits"

				Call rtitem.AppendText("The following visits are scheduled today...Please check Account Review Items:")

				Call rtitem.AddNewLine(2)

				Call rtitem.AppendText("__________________________________")

				Call rtitem.AddNewLine(2)

				firstonecreated = True

				

			End If

			Call rtitem.AppendDocLink(doc, "Click here for details of the visit")

			Call rtitem.AddTab(1)

			Call rtitem.AppendText(doc.employee(0))

			Call rtitem.AddTab(1)

			Call rtitem.AppendText(doc.client(0))

			Call rtitem.AddNewLine(2)

			

		End If

NextDoc:

		Set doc = dc.GetNextDocument(doc)

	Loop		

’ Next

	If firstonecreated Then

		Call ndoc.Send(False)

	End If

End If

End Sub

Subject: Agent

There are working examples of agents that do this in the Sandbox as well as in the Agent FAQ.