lotus-noteslotusscript

Agent for UnprocessedDocuments After New Mail Arrives


I wish to modify some documents when they arrive in my mail-in application. (I need to remove the $REF field which then allows dragging and dropping in to folders, but that's not the point.)

I have my agent set to After New Mail Arrives and to select unprocessed documents. Designer Help for Unprocessed documents states...

enter image description here

With the agent properties as ....

enter image description here

But the agent selects any document in the Inbox which has been modified or edited rather than just the new arriving email. There is also a delay of up to a minute before the agent runs.

Sub Initialize
Dim session As New NotesSession
Dim db As NotesDatabase
Dim collection As NotesDocumentCollection
Dim doc As NotesDocument    
Set db = session.CurrentDatabase
Set collection = db.UnprocessedDocuments
Set doc = collection.GetFirstDocument()
While Not(doc Is Nothing)
    If doc.Hasitem("$REF") Then
        doc.Subject= "($Ref) " & doc.Subject(0) 'Only testing here will remove item if exists
    Else
        doc.Subject = "(No Ref) " & doc.Subject(0)
    End If

    
    Call doc.save(True,False)
'   Call session.UpdateProcessedDoc( doc ) 'This doesn't appear to make any difference.
    Set doc = collection.GetNextDocument(doc)
Wend

End Sub

Any help in running this type of script on only new mail and faster than at present, as users may edit the doc before the agent has run, would be appreciated. Thanks


Solution

  • You might want to use trigger "Before new mail arrives" instead of "After new mail has arrived".

    "Before new mail arrives" gets executed for every single new email immediately.

    You get access to email document with

    Dim session as New NotesSession
    Dim doc as NotesDocument
    Set doc = session.DocumentContext
    

    You can find a good comparison of both triggers here.