Thursday, 5 April 2018

LotusScript Agent Send Email to Multiple Receipients Per Recipient Including a Doc Link Based on a Search Formula

As I mentioned on Twitter yesterday I created a LotusScript Agent for sending emails to multiple receipients (per receipient) including found documents and a doclink based on a search formula. In response to the Tweet I received the request to publish the Agent. Below a short description and the sample code of the LotusScript Agent.
In the LotusScript Agent first all documents are collected that meet the conditions in the search formula. In the next step in the script the various recipients are put into an array. Next all duplicates are removed from the Array using the ArrayUnique function. To obtain an optimal and correct Array, a FullTrim is also performed on the Array. Given an array, FullTrim eliminates "empty" entries and eliminates duplicate, trailing and leading whitespace within entries; and given a string, eliminates duplicate, trailing and leading whitespace in the string. As the next step the email is compiled. For my own specific requirements I have built in a comparison between two date fields . Based on the outcome of the number of days, a document including a doc link is included in the email or not (based on the documents found in the search formula). In the last step the found receipients in the array are per reciepient placed in the SendTo field including the found documents and a doc link based on the outcome of the comparison of the two date fields. Finally the emails per reciepient are send.

Sub NAME (db As NotesDatabase, doc As NotesDocument)
' error handler
On Error GoTo errorHandler

' init
Dim s As New NotesSession
Dim dc As NotesDocumentCollection
Dim searchFormula As String
Dim md As MailData
Dim i As Integer
Dim x As Integer
Dim maildoc As NotesDocument
Dim addresses As NotesName
Dim rtitem As NotesRichTextItem
Dim days As Variant
Dim ds As Variant
Dim d1 As NotesDateTime
Dim d2 As NotesDateTime
Dim numdays As Double
Dim resultArr As Variant
Dim day1 As NotesDateTime
Dim day2 As NotesDateTime
Dim noofdays As Double
Dim ownerresultArray As Variant
ReDim addressto(0) As String

Set s = New NotesSession
Set db = s.CurrentDatabase
Set maildoc = New NotesDocument(db)

' search support calls impact high
searchFormula = {(Form = "Form" & Field = "Value" & (Field2 = "Value" | Field 3 = "Value"))}
Set dc = db.Search(searchFormula, Nothing, 0)

Set doc = dc.GetFirstDocument
While Not(doc Is Nothing)
ReDim Preserve addressto(i)
Set d1 = New NotesDateTime(Today())
Set d2 = New NotesDateTime(doc.Lastmodified)
ds = d2.TimeDifference(d1)
numdays= Abs(ds/(86400))
If numdays > 0 And numdays < 8 Then
addressto(i) = (doc.Receipient(0))
i = i + 1
End If
Set doc = dc.GetNextDocument(doc)

resultArr = ArrayUnique(addressto,0)
ownerresultArray = FullTrim(resultArr)

x = 0
ForAll v In ownerresultArray
'- prepare mail
maildoc.Form = "Memo"
maildoc.CopyTo = "Name"
maildoc.Subject = "Subject text"
Set rtitem = New NotesRichTextItem(maildoc, "Body")
Call rtitem.AppendText("First Text Line Body Email Message")
Call rtitem.AddNewLine(1)
Call rtitem.AddNewLine(1)

' email
Set doc = dc.GetFirstDocument
Set rtitem = New NotesRichTextItem(maildoc, "Body")
While Not(doc Is Nothing)
If doc.Field(0) = resultArr(x) Then
Set day1 = New NotesDateTime(Today())
Set day2 = New NotesDateTime(doc.Lastmodified)
days = day2.TimeDifference(day1)
noofdays= Abs(days/(86400))
If noofdays > 0 And noofdays < 8 Then
'- Append descriptive text, link and new line
Call rtitem.AppendText(doc.Field(0) & "  " & doc.Field(0) & " " & doc.Field(0) &" ")
Call rtitem.AppendDocLink(doc, "Link")
Call rtitem.AddNewLine(1)
End If

'- Set recipient
Set addresses = New NotesName(resultArr(x))
maildoc.SendTo = addresses.abbreviated
End If
Set doc = dc.GetNextDocument(doc)

'- send mail
Call maildoc.Send( True )
x = x + 1

End ForAll

Call AddToStackTrace()
End Sub

I hope that those who have requested to publish the Agent on my blog can use the Agent for their own applications or at least have received a handle for their own development. In my next blog posts more LotusScript Functions and Agents that I have implemented during the past period in various Notes applications.

1 comment: