< Back

Document Subject: Send email when a date is reached
Hint Short Cut: Add this to your code & documentation to help you find this page.
http://#MailOnDateAgent or http://A555F9/nn.nsf/ByAlias/MailOnDateAgent

LotusScript to send Mail when a date is reached.




 

'This should be in an agent that is scheduled.

 

     On Error Goto ErrHdlr
   
    Dim s As New notessession
    Dim db As notesdatabase
    Set db = s.currentdatabase
   
    'set up debug/error memo
    Dim debugmemo As New notesdocument(db)
    debugmemo.Form="Memo"
    debugmemo.sendto = s.effectiveusername 'signer of agent
    Dim debugrt As New notesrichtextitem(debugmemo, "Body")
    Call debugrt.appendtext ( db.server & "   " & db.filepath )
    Call debugrt.addnewline(1)
    Call debugrt.appendtext ( "Agent: " & s.currentagent.name & "    " & Today)
    Call debugrt.addnewline(1)
   
    'The search formula
    Set SearchDate = New NotesDateTime("01/01/1990")
   
    ' This will find all documents with Form=Contract with status not set to erminated and with the         Date field TerminateDate a week away
    'Selection = "Form = ""Contract"" & Status <> ""Terminated"" & TerminateDate =" @adjust(@today;0;0;7;0;0;0)"
   
   
    'This will find all documents with Form=Contract with status not set to erminated and with the Date field TerminateDate set to today
    Selection = "Form = ""Contract"" & Status <> ""Terminated"" & TerminateDate = @today"
   
   
    Dim col As notesdocumentcollection
    Set Col = DB.Search(Selection, SearchDate, 0)
   
    Dim doc As notesdocument
   
    Dim mdoc As notesdocument
    Dim rt As notesrichtextitem
   
    Set doc = col.getfirstdocument()
    Do Until doc Is Nothing
         
         'Add some debug
         Call debugrt.appendtext( doc.Owner(0) & "   " )
         Call debugrt.appenddoclink( doc, "Link")
         Call debugrt.addnewline(1)
         
         'Create the email
         Set mdoc = New notesdocument(db)
         Set rt = New notesrichtextitem(mdoc, "Body" )
         mdoc.sendto=doc.Owner(0)
         mdoc.subject= "Terminated document " & doc.subject(0)          
         Call rt.appendtext("Click on link to see the terminated document.")
         Call rt.addnewline(1)
         Call rt.appenddoclink( doc, "Link")
         Call rt.addnewline(1)
         Call rt.appendtext("( Server: " & db.server & " File path: " & db.filepath & " Doc UNID: " & doc.universalid & " )" )
         
         
         'This bit updated the status field to stop the email going again
         doc.status="Terminated"
         Call doc.save(True,True)
         
         'Send the email off
         Call mdoc.send(False)
         
         Set doc = col.getnextdocument(doc)
    Loop
   
    'send off the debug memo
    Call debugrt.appendtext( "Agent finished: " & Today )
    Call debugmemo.send(False)
    End
   
ErrHdlr:
   
    'Add some debug
    Call debugrt.appendtext( "Error: " & Error$ & "   Err: " & Err & "   Erl:" & Erl )
   
   
    If Not(doc Is Nothing) Then
         Call debugrt.appendtext( "Possible error document: ")
         Call debugrt.appenddoclink( doc, "Link")
         Call debugrt.addnewline(1)
    End If
   
    Call debugmemo.send(False)
    End