• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 227
  • Last Modified:

Need help with a agent

I have a agent that runs through each New or Modified doucument in my inbox.  The agent is designed to create and send a email from some data in the body of the email and then it marks the document read.  The agent runs great.  The problem I am having is if a document contains wrong information and the shortname can not be found in the address book.  The agent stops and the rest of the documents are not processed.  
Example:  

If I have 5 unread documents in my inbox
The agent runs
The first document has a correct shortname
A email is sent and that document is marked read
The agent then goes to the second document
If the shortname is invalid
The agent stops there and the rest of the documents are not processed.

It would be nice if the agent would skip the bad document and finish the rest of the list.  I will attach the agent code
Sub Initialize
	Dim s As New NotesSession
	Dim db As NotesDatabase
	Dim c As NotesDocumentCollection
	Dim cur As NotesDocument
	
	Set db = s.CurrentDatabase
	Set c = db.UnprocessedDocuments
	
	Set cur = c.GetFirstDocument
	Do Until cur Is Nothing
		If Not cur.HasItem( "flagProcessed" ) Then                'if it doesn't have flag - process it
			Call SendMemoFromTag( cur, "UserName", Chr(10) )
			Call cur.ReplaceItemValue( "flagProcessed", "1" )      'flag the processed e-mail
			Call cur.Save( True, False, True )                                'you must save the change in the doc
		End If
		Set cur = c.GetNextDocument( cur )
	Loop
End Sub
 
 
 
 
Sub SendMemoFromTag(memo As NotesDocument, Byval tagStart As String, Byval tagEnd As String)
	
	Dim sess As New NotesSession
	Dim parts As Variant
	Dim body As String, sendto As String
	sess.ConvertMime = True
	body = memo.GetItemValue("Body")(0)
	If (Instr(1, body, tagStart, 5) = 0) Then Exit Sub
	parts = Split(body, tagStart)
	If (Instr(1, parts(1), tagEnd, 5) = 0) Then sendto = Trim(parts(1)) Else sendto = Trim(Strleft(parts(1), tagEnd))
	
	Dim memo2 As NotesDocument
	Set memo2 = memo.ParentDatabase.CreateDocument
	
	line2 = "Requester Name: " & sendto & Chr(10)
	line3 = "Your job has been completed by the Prototype Technology Center." & Chr(10) & Chr(10)
	line4 = "Please do not respond to this email.  This email is for notification purposes only" & Chr(10)
	line5 = memo.Body & Chr(10)
	
	Memo2.ReplyTo = "Please Do Not Reply"
	memo2.Form = "memo"
	memo2.Body = line5 +  line3 + line4 
	memo2.SendTo = sendto
	memo2.Subject = memo.Subject
	Call memo2.Send(False)
	
End Sub

Open in new window

0
Jaziar
Asked:
Jaziar
  • 4
  • 4
1 Solution
 
Bill-HansonCommented:
Sure, just add an error trap to SendMemoFromTag.  The example below simply ignores all errors, but you could add code to the CATCH block to handle the error.  For example, you could log that the message was not sent.
Sub SendMemoFromTag(memo As NotesDocument, Byval tagStart As String, Byval tagEnd As String)
        On Error Goto CATCH
        Dim sess As New NotesSession
        Dim parts As Variant
        Dim body As String, sendto As String
        sess.ConvertMime = True
        body = memo.GetItemValue("Body")(0)
        If (Instr(1, body, tagStart, 5) = 0) Then Exit Sub
        parts = Split(body, tagStart)
        If (Instr(1, parts(1), tagEnd, 5) = 0) Then sendto = Trim(parts(1)) Else sendto = Trim(Strleft(parts(1), tagEnd))
        
        Dim memo2 As NotesDocument
        Set memo2 = memo.ParentDatabase.CreateDocument
        
        line2 = "Requester Name: " & sendto & Chr(10)
        line3 = "Your job has been completed by the Prototype Technology Center." & Chr(10) & Chr(10)
        line4 = "Please do not respond to this email.  This email is for notification purposes only" & Chr(10)
        line5 = memo.Body & Chr(10)
        
        Memo2.ReplyTo = "Please Do Not Reply"
        memo2.Form = "memo"
        memo2.Body = line5 +  line3 + line4 
        memo2.SendTo = sendto
        memo2.Subject = memo.Subject
        Call memo2.Send(False)
        Exit Sub
CATCH:
        Exit Sub
End Sub

Open in new window

0
 
JaziarAuthor Commented:
Made the change waiting to see if it works - thanks for the quick repsonse
0
 
JaziarAuthor Commented:
One quick question - all the emails the agent sends out, has me as the sender.  I am guessing it because I wrote the agent.  Is there a way to have the sender as a generic person, something like Tool Shop?
I think your Catch is working, but none of the documents have been wrong so far.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
SysExpertCommented:
You either need to set up a User ID for this and have it sign the agent or use the workaround of sending via SMTP via the Server mail box directly, messy but works.


I hope this helps !
0
 
Bill-HansonCommented:
>> "Is there a way to have the sender as a generic person, something like Tool Shop?"

Yes.

SysExpert is right, that you can sign the agent using the id that you want to use for the sender, but if you are using Notes R6 or later, all you have to do is select the user in the Agent's 'Run on behalf of' property (see screen shot below).

Also, make sure that you set the 'From' and 'Principal' fields to the sender's name.

        Memo2.ReplyTo = "Please Do Not Reply"
        memo2.From = "Some User/Organization"
        memo2.Principal = "Some User/Organization"
        memo2.Form = "memo"
        memo2.Body = line5 +  line3 + line4
        memo2.SendTo = sendto
        memo2.Subject = memo.Subject
AgentRunOnBehalfOf.gif
0
 
Bill-HansonCommented:
I just ran a quick test to see how hard it is to spoof the reply address.  Here's what I tried:

Sub Initialize
      Dim sess As New NotesSession
      Dim db As NotesDatabase
      Dim memo As NotesDocument
      Set db = sess.CurrentDatabase
      Set memo = db.CreateDocument
      memo.Form = "memo"
      memo.From = "Do Not Reply"
      memo.Principal = "Do Not Reply"
      memo.ReplyTo = "Do Not Reply"
      memo.Subject = "ReplyTo Test"
      memo.Body = "Please do not reply"
      Call memo.Send(False, Split("My Name/Org,myaccount@gmail.com", ","))
End Sub

Notice that one copy goes to my Notes account and another goes to my gmail account.  Also, the agent was signed by myself with no value in the agent's 'Run on behalf of' property.

Results:

In the Notes client, the sender is "Do Not Reply", but it also shows "Sent by: My Name/Org".  When I reply, the reply attempts to send to "Do Not Reply".  So, the only way to truly spoof the Notes client is to sign the agent with another id or use the agent's 'Run on behalf of' property.

In gmail, the spoof is complete.  There is no mention of my name, only "Do Not Reply".

So, in a nutshell, to spoof the Notes client, you need to use another id, but for external mail, setting From, Principal, and ReplyTo is enough.
0
 
JaziarAuthor Commented:
Sorry about the delay in repsonse.  I am soory that I kind of got off task with the run by question.  When I used the ERROR / CATCH method, I can only guess that it worked.  Right now when new documents come in to my inbox they are red(usread) and once the agent runs they turn black(read).  So now when the agent stops becuase of a invalid shortname it will be red(unread), but it stops the rest of the documents from being runned by the agent.  I know where the problem is becuase of the red(unread) document.  When using the code above, all the documents are black(read), so I do not know which ones where valid and which ones where not.  What I am really needing is when I open the in-box the ones still red(unread) are the prtoblems and the ones black(read) are good and I know they have ran.

I really need to get this working!!
0
 
Bill-HansonCommented:
No problem.  All you need to do is to change the SendMemoFromTag sub in to a function, then check the return value before updating the flagProcessed field.

Try this code:
Sub Initialize
        Dim s As New NotesSession
        Dim db As NotesDatabase
        Dim c As NotesDocumentCollection
        Dim cur As NotesDocument
        
        Set db = s.CurrentDatabase
        Set c = db.UnprocessedDocuments
        
        Set cur = c.GetFirstDocument
        Do Until cur Is Nothing
                If Not cur.HasItem( "flagProcessed" ) Then                'if it doesn't have flag - process it
                        If (SendMemoFromTag( cur, "UserName", Chr(10) )) then
                                Call cur.ReplaceItemValue( "flagProcessed", "1" )      'flag the processed e-mail
                                Call cur.Save( True, False, True )                                'you must save the change in the doc
                        End If
                End If
                Set cur = c.GetNextDocument( cur )
        Loop
End Sub
 
Function SendMemoFromTag(memo As NotesDocument, Byval tagStart As String, Byval tagEnd As String) As Boolean
        On Error Goto CATCH
        Dim sess As New NotesSession
        Dim parts As Variant
        Dim body As String, sendto As String
        sess.ConvertMime = True
        body = memo.GetItemValue("Body")(0)
        If (Instr(1, body, tagStart, 5) = 0) Then Exit Function
        parts = Split(body, tagStart)
        If (Instr(1, parts(1), tagEnd, 5) = 0) Then sendto = Trim(parts(1)) Else sendto = Trim(Strleft(parts(1), tagEnd))
        
        Dim memo2 As NotesDocument
        Set memo2 = memo.ParentDatabase.CreateDocument
        
        line2 = "Requester Name: " & sendto & Chr(10)
        line3 = "Your job has been completed by the Prototype Technology Center." & Chr(10) & Chr(10)
        line4 = "Please do not respond to this email.  This email is for notification purposes only" & Chr(10)
        line5 = memo.Body & Chr(10)
        
        Memo2.ReplyTo = "Please Do Not Reply"
        memo2.Form = "memo"
        memo2.Body = line5 +  line3 + line4 
        memo2.SendTo = sendto
        memo2.Subject = memo.Subject
        Call memo2.Send(False)
        SendMemoFromTag = True
        Exit Function
CATCH:
        SendMemoFromTag = False
        Exit Function
End Function

Open in new window

0
 
JaziarAuthor Commented:
That Works Great!!!  Thank you so much
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 4
  • 4
Tackle projects and never again get stuck behind a technical roadblock.
Join Now