Manage Learn to apply best practices and optimize your operations.

A Reusable Agent To Change Field Values On Documents (R5)

I was creating a new database in R5 recently and came across a problem I often
I had several categorized documents that I wanted to change the "CategoryName"
field of. Now, in the past, what
I have done is to write an agent to change the value of the specific field I
wanted to change.
I've been through this before, however, and I knew that as soon as I made that
I would find another field I wanted to change as well.
Therefore, I'd be right back at square one, writing another agent to change a
different specific field's value.
Well, I knew there had to be a better way, and after a lot of coding I've found
one. My goal was this: create a single agent, that could be used in multiple
views, that would allow a user to change the values of fields on multiple
documents they'd selected in a view. I'm happy to say that I have achieved my
goal, and that is what I'm here to share with you today.

The following code is designed to work in Notes/Domino R5 and will not work
as-is in Notes/Domino R4.x.
It's also designed to work in a view containing similar documents, either
created with the same form and/or containing the same fields.

The possibilities for customizing this agent are many. You can modify it so
that you further narrow down the fields and even types of fields that you allow
the user to change. You can make it an agent that worms its way through all of
the documents in a database changing any fields that have a specific value that
you specify. Or you can use it as is and save yourself from having to
constantly create simple agents that change specific fields.
Option Public
Option Explicit
%INCLUDE "lsconst.lss"

Dim currentDB As NotesDatabase
Dim selectedDoc As NotesDocument
Dim formName As String
Dim fieldNames() As String
Dim newValue As String
Dim continue As String
Dim fieldChoice As Variant
Dim fieldChoiceList As Variant

Sub Initialize
'--Dim the local variables
Dim session As New NotesSession
Dim workspace As New NotesUIWorkspace
Dim selectedDocs As NotesDocumentCollection
Dim notesItem As NotesItem
'--Set the variables
Set currentDB = session.CurrentDatabase
Set selectedDocs = currentDB.UnprocessedDocuments
'--Return an error and exit the sub if no document is selected
If ( selectedDocs.Count = 0 ) Then
Msgbox "Please select a document.", MB_OK, "Error:"
Exit Sub
End If
'--Get the name of the form of the first selected document
Set selectedDoc = selectedDocs.GetFirstDocument
formName = selectedDoc.Form(0)
'--Call the CreateFieldChoiceList sub which will return a list of all the
fields on the form for the user to choose from
Call CreateFieldChoiceList
'--Ask the user which field they would like to change
fieldChoice = workspace.Prompt ( PROMPT_OKCANCELLIST, "Field Name:", "Please
choose the name of the field you would like to change.", fieldChoiceList(0),
fieldChoiceList )
'--Get the current value of the selected field from the first document
'--and present it to the reader as the default choice.
Set notesItem = selectedDoc.GetFirstItem ( fieldChoice )
newValue = Inputbox ( "What is the new value for this field?", "New Value:",
notesItem.Text )
'--Call the ConfirmChange sub to confirm they want to make this change
Call ConfirmChange
'--Exit this sub if the user chooses not to make the change
If ( continue = "No" ) Then
Exit Sub
End If
'--Replace the value on the selected documents
Do Until ( selectedDoc Is Nothing )
Set notesItem = selectedDoc.GetFirstItem ( fieldChoice )
notesItem.Values = newValue
Call selectedDoc.Save ( True, True )
Set selectedDoc = selectedDocs.GetNextDocument ( selectedDoc )
End Sub

Sub CreateFieldChoiceList
'--Dim the local variables
Dim form As NotesForm
Dim i As Integer
i = 0
'--Get the form that the first selected document uses
Set form = currentDB.GetForm ( formName )
'--Build an array of the field names from the selected document
Forall item In selectedDoc.Items
Redim Preserve fieldNames(i)
'--If the field name begins with a "$", then skip it
If ( Left ( item.Name, 1 ) <> "$" ) _
And ( item.Type <> RICHTEXT ) _
And ( item.Name <> "UNID" ) _
And ( item.Name <> "FORM" ) Then
fieldNames(i) = item.Name
i = i + 1
End If
End Forall
'--Sort the fields by name before you present them as choices to the user
fieldChoiceList= SortArray ( fieldNames )
End Sub

Sub ConfirmChange
'--Dim the local variables
Dim confirm As Integer
'--Ask the user if they are sure they want to make the change
confirm = Msgbox ( "Are you sure you want to change the " & Cstr ( fieldChoice
) & _
" field to """ & newValue & """ for all selected documents?", _
MB_OKCANCEL, "Confirm Change:" )
'--Set the value of the continue variable to either "Yes" or "No"
If ( confirm = IDOK ) Then
continue = "Yes"
continue = "No"
Exit Sub
End If
End Sub

Function SortArray ( inarray As Variant ) As Variant
'--This SortArray function can be put into a script library along with the
'--Swap sub and called from there
Dim i%, j%, max%
Dim array As Variant
array = inarray
max% = Ubound(array)
For i% = 0 To max% - 1
For j% = i% + 1 To max%
If array(i%) > array(j%) Then
Swap array(i%), array(j%)
End If
SortArray = array
End Function

Sub Swap ( v1 As Variant, v2 As Variant )
'--This Swap sub can be placed in a script library along
'--with the SortArray function and called from there
Dim v As Variant<

Dig Deeper on Domino Resources - Part 2

  • Favorite iSeries cheat sheets

    Here you'll find a collection of valuable cheat sheets gathered from across the iSeries/ community. These cheat ...