Friday, 30 September 2016

Quick IBM Notes Tip: Administrative LotusScript Agent to Update Field Values on Selected Documents in View


Lately I focus particularly on LotusScript, Java and NodeJS and no longer on XPages. XPages has as it looks in all the recent IBM Roadmaps not a real future anymore. More on this subject in my next blog post.
In my daily work I frequently encounter request to customize field values in selected documents. One of the most frequently used administrative LotusScript Agents I use fot this purpose is an Agent from Breaking Par Consulting, Inc. to update Field Values in selected documents in a Notes View.
At the moment the agent is executed the person running the Agent will be prompted for the Field Name, the (New) Field Value and a Field Type. All the selected documents in the Notes View will be updated. As shown in the third image it is also possible to delete a Field with this Agent. The Agent Properties I use for this Agent are Trigger - Action Menu Selection and Target - All Selected Documents.


Sub Initialize
   Dim ws As New NotesUIWorkspace
   Dim session As New NotesSession
   Dim prompt As String
   Dim fieldName As String
   Dim fieldValue As String
   Dim dataTypes As Variant
   Dim thisDataType As String
   Dim fieldValues As Variant
   Dim newFieldValues As Variant
   Dim db As NotesDatabase
   Dim coll As NotesDocumentCollection
   Dim i As Integer
   Dim doc As NotesDocument
   Dim item As NotesItem

prompt = "Please enter the name of the field to be updated"
   fieldName = ws.Prompt(3, "Enter Field Name", prompt, "")
   If fieldName = "" Then Exit Sub
   If Instr(fieldName, " ") <> 0 Then
      prompt = "Error! Field Names can't have spaces!"
      Msgbox prompt, 16, "Error"
      Exit Sub
   End If

prompt = "Please enter the new value. For multiple values, separate with a colon."
   fieldValue = ws.Prompt(3, "Enter Field Value", prompt, "")

Redim dataTypes(5) As String
   dataTypes(0) = "Text"
   dataTypes(1) = "Number"
   dataTypes(2) = "Date"
   dataTypes(3) = "Readers"
   dataTypes(4) = "Authors"
   dataTypes(5) = "DELETE THIS FIELD"
   prompt = "Choose the data type of the value(s)"
   thisDataType = ws.Prompt(4, "Choose Data Type", prompt, dataTypes(0), dataTypes)
   If thisDataType = "" Then Exit Sub

Set db = session.CurrentDatabase
   Set coll = db.UnprocessedDocuments
   fieldValues = Evaluate({@Explode("} & fieldValue & {"; ":")})
   Select Case thisDataType
   Case dataTypes(0) : Redim newFieldValues(Ubound(fieldValues)) As String
   Case dataTypes(1) : Redim newFieldValues(Ubound(fieldValues)) As Double
   Case dataTypes(2) : Redim newFieldValues(Ubound(fieldValues)) As Variant
   Case dataTypes(3) : Redim newFieldValues(Ubound(fieldValues)) As String
   Case dataTypes(4) : Redim newFieldValues(Ubound(fieldValues)) As String
   End Select
For i = Lbound(fieldValues) To Ubound(fieldValues)
      Select Case thisDataType
      Case dataTypes(0) : newFieldValues(i) = Trim(fieldValues(i))
      Case dataTypes(1) : newFieldValues(i) = Val(fieldValues(i))
      Case dataTypes(2) : newFieldValues(i) = Cdat(fieldValues(i))
      Case dataTypes(3) : newFieldValues(i) = Trim(fieldValues(i))
      Case dataTypes(4) : newFieldValues(i) = Trim(fieldValues(i))
      End Select
   Next
Set doc = coll.GetFirstDocument
   While Not doc Is Nothing
      If thisDataType = "DELETE THIS FIELD" Then
         If doc.HasItem(fieldName) Then Call doc.RemoveItem(fieldName)
      Else
         Call doc.ReplaceItemValue(fieldName, newFieldValues)
         If thisDataType = dataTypes(3) Or thisDataType = dataTypes(4) Then
            Set item = doc.GetFirstItem(fieldName)
            If thisDataType = dataTypes(3) Then item.IsReaders = True
            If thisDataType = dataTypes(4) Then item.IsAuthors = True
         End If
      End If
      Call doc.Save(True, False)
      Set doc = coll.GetNextDocument(doc)
   Wend
End Sub

As in almost all situations, there are of course other possibilities to update Field Values in all (selected) documents in a Notes View. Below a basic eample I use for updating Field Values in all documents in a specific Notes View.

Sub Initialize  

Dim s As New NotesSession 
Dim db As NotesDatabase 
Dim doc As NotesDocument 
Dim view As NotesView 

Set db = s.CurrentDatabase 
Set view=db.GetView("VIEWNAME")
Set doc = view.GetFirstDocument 
view.AutoUpdate = False

While Not (doc Is Nothing)  
If doc.FIELDNAME(0) = "CURRENTVALUE" Then 
doc.FIELDNAME = "NEWVALUE" 
End If 
Call doc.Save(True, False) 
Set doc=view.GetNextDocument(doc)  
Wend 
End Sub

1 comment:

  1. "XPages has as it looks in all the recent IBM Roadmaps not a real future anymore. "
    I thought that you were a cheer leader for Notes / Domino and the "exciting times ahead" and you have been focusing on Node.js this whole time ;-)

    ReplyDelete