API
@-Formulas
JavaScript
LotusScript
Reg Exp
Web Design
Notes Client
XPages
 
Update Field On Selected Documents
One of our administration agents that we almost always include in applications is this one. It updates a field on the selected documents. You'll be prompted for a field name, the field value, and then a field type. All the selected documents will be updated. The agent is pretty straightforward. It handles removing of a field, too (that's one of the options).

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

The only drawback to this code is that your values cannot have a colon (:) in them, because that is assumed to be a separator. What we do in that situation is debug the script and just change the value before it's stored on the documents. If you want to be ambitious, you could change the second prompt to add "text multi value", "number multi value", etc. to the list. Then, instead of prompting for one value, you continually prompt until a null value (empty string) is entered if one of the "multi value" options was chosen. That way you could put a colon into individual values.