Update agent

The following code is a sample update agent, which was created using LotusScript. The agent performs some tasks based on the UA_ACTION value (such as, Updates, Inserts), and sends e-mail with updated values in the e-mail’s body.

Sub Initialize
   Dim Session As New NotesSession
   Dim maildoc As NotesDocument
   Dim doc As NotesDocument
   Dim orig_doc As NotesDocument
   Dim db As NotesDatabase
   Dim paramDocId As String

   Dim agent As NotesAgent
   Dim sendTo As String
   Dim subject As String
   Dim str1 As String
   Dim suffixMsg As String

   'field variables specific to business logic
   Dim ticketNo As Variant
   Dim users As Variant
   Dim otherusers As Variant
   Dim priority As Variant
   Dim supporter As Variant
   Dim department As Variant
   Dim category As Variant
   Dim type1 As Variant
   Dim itemvalue As Variant
   Dim solution As Variant

   Dim requestValues As Variant

   Dim actionValue As Variant
   Dim ccToLength As Integer

   Dim orig_noteid As Variant
   Dim orig_formname As Variant
   On Error Goto errorhandler

   The agent has the skeleton code ready to send mail
   ‘TODO :
   ' 1. Fetch values from document
   ' 2. Based on values fetched send mail
   Print "Entered agent"
   Set db = Session.CurrentDatabase
   Set agent = Session.CurrentAgent
   paramDocId = agent.ParameterDocID
   Print "Before doc " + paramDocId
   Set doc = db.GetDocumentByID(paramDocId)
   Print "after doc"
   'gather field values to mail
   'doc.UserName is equivalent of doc.GetItemValue("UserName')
   ticketNo = doc.TicketNo
   users = doc.UserName
priority = doc.Priority
   supporter = doc.Supporter
   department = doc.Department
   category = doc.Category
   type1 = doc.Type
   itemvalue = doc.Item
   solution = doc.Solution
   actionValue = doc.UA_ACTION
   requestValues = doc.RequestValues
   Print "Printing the request values.. "
   Print Cstr(requestValues(0))
   sendTo = Cstr(users(0))
   Print "Before Substring"
   str1 = Mid$(Cstr(actionValue(0)), 4)

      If(Strcompare(str1, "Save") = 0) Then
         suffixMsg = "Saved"
      Elseif(Strcompare(str1 , "CloseTicket") = 0) Then
         suffixMsg = "Closed"
      Elseif(Strcompare(str1 ,  "CreateTicket")) Then
         suffixMsg = "Created"
      Elseif(Strcompare(str1 , "Accept") =0) Then
         suffixMsg = "Accepted"
      Elseif(Strcompare(str1 , "Reject") =0) Then
         suffixMsg = "Rejected"
      Else
         suffixMsg ="Default"
      End If
      If(str1 <> "Cancel") Then
         Print "Inside If	"
         orig_noteid = doc.orig_noteid
         orig_formname = doc.orig_formname
         Set orig_doc = db.GetDocumentByID(Cstr(orig_noteid(0)))
         If(orig_doc Is Nothing) Then
            Print "Original doc not found for id - " + Cstr(orig_noteid(0))
         End If
         If(Cstr(orig_noteid(0)) = "") Then
            Print "Original note id is empty : CREATEDOC"
            'create new document
            'place to do validation and creating a new document
            Set orig_doc = db.CreateDocument
            Call orig_doc.AppendItemValue("Form", orig_formname(0))
            Call orig_doc.AppendItemValue("TicketNo", ticketNo(0))
            Call orig_doc.AppendItemValue("UserName",users(0))
            Call orig_doc.AppendItemValue("Priority", priority(0))
            Call orig_doc.AppendItemValue("Supporter", supporter(0))
            Call orig_doc.AppendItemValue("Department", department(0))
            Call orig_doc.AppendItemValue("Category", category(0))
            Call orig_doc.AppendItemValue("Type", type1(0))
            Call orig_doc.AppendItemValue("Item",itemvalue(0))
            Call orig_doc.AppendItemValue("Solution", solution(0))
            Call orig_doc.AppendItemValue("UA_ACTION",actionValue(0))
            Call orig_doc.Save(False, True)
         Else
            Print "Original note id non empty : UPDATEDOC			"
            'update existing document
            'place to do validations
            'Dim richtext As New NotesRichTextItem(orig_doc,"Body")
            Dim item As NotesItem

            Set item = orig_doc.ReplaceItemValue("TicketNo", ticketNo(0))
            Set item = orig_doc.ReplaceItemValue("UserName",users(0))
            Set item = orig_doc.ReplaceItemValue("Priority", priority(0))
            Set item = orig_doc.ReplaceItemValue("Supporter", supporter(0))
            Set item = orig_doc.ReplaceItemValue("Department", department(0))
            Set item = orig_doc.ReplaceItemValue("Category", category(0))
            Set item = orig_doc.ReplaceItemValue("Type", type1(0))
            Set item = orig_doc.ReplaceItemValue("Item",itemvalue(0))
            Set item = orig_doc.ReplaceItemValue("Solution", solution(0))
            Set item = orig_doc.ReplaceItemValue("UA_ACTION",actionValue(0))
            Call orig_doc.Save(False, True)
            Print "Replaced values"
         End If
         Set maildoc = New NotesDocument( db )
         'prepare to send mail
         Dim body As  NotesRichTextItem
         Dim lintTo As NotesDocument
         Print "about to send mail to " + sendTo
         maildoc.Form = "Memo		"
         maildoc.SendTo = sendTo
         'maildoc.CC = supporter(0)
         subject = "Your ServiceDesk Ticket#  " + Cstr(ticketNo(0)) + 
            " has been  "  + suffixMsg
         maildoc.Subject = subject
         Print "preparing body		"
         Set body  = maildoc.CreateRichTextItem("Body")

         Print "created body"
         body.appendText("Helpdesk values are, ")
         body.AddNewline(1)
         body.appendText("Ticket No = " + ticketNo(0))
         body.AddNewline(1)
         body.appendText("UserName = " + users(0))
         body.AddNewline(1)
         body.appendText("Priority = " + priority(0))
         body.AddNewline(1)
         body.appendText("Supporter = " + supporter(0))
         body.AddNewline(1)
         body.appendText("Department = " + department(0))
         body.AddNewline(1)
         body.appendText("Category = " + category(0))
         body.AddNewline(1)
         body.appendText("Type = " + type1(0))
         body.AddNewline(1)
         body.appendText("Item = " + itemvalue(0))
         body.AddNewline(1)
         body.appendText("Solution = " + solution(0))
         body.AddNewline(1)
         body.appendText("Action taken = " + actionValue(0))
         body.AddNewline(2)
         'If(Cstr(orig_noteid(0)) <> "") Then
         Call body.AppendDocLink(orig_doc, "Double click to open it")
         End If
         'maildoc.Body = body
         Call maildoc.Save(False, True)
         Print "about to send						"
         Call maildoc.Send( False )
         Print "mail sent	"
         xml="<Record><Field label=""Index"" position=""1"">Status</Field>
         xml = xml +"</Record><Record>"
         xml = xml +"<Field label=""Index"" position=""1"">Record updated
            successfully</Field>		"
         xml = xml +"</Record>"
         'Call doc.AppendItemValue("form", "xmldoc")
         Call doc.AppendItemValue("domino_status", "ok")
         Call doc.AppendItemValue("domino_output", xml)

         Exit Sub ' forcibly exiting
      End If

   errorhandler :
      Set doc = db.GetDocumentByID(paramDocId)
      xml = "Msg from postprocessor: Completed with errors	"
      'Call doc.AppendItemValue("status_desc", xml)

      Call doc.AppendItemValue("domino_status", xml)
      Call doc.AppendItemValue("domino_output", xml)

      Call doc.Save(False, False)
      ' Display the values on entry to the 
         ' error-handling routine.
      Print "Error: " Error(), "   Err:" Err(), "   Erl:" Erl()
      Print "End.."+	paramDocId
      Resume Next
End Sub