Agent to Update Field Values Needing a Replace String Operation

Author: Tripp W Black

Created: 06/03/2002 at 05:07 PM

 

Category:
Notes Developer Tips
Agents, LotusScript

The follwoing code changes the Notes Domain in three fields of a person document. This code can be improved/updated to include looping for the fields.

Sub Initialize
' this agent will change the domain information on a person's hierarchical name to "/NewDomain/US"
Dim s As New NotesSession
Dim db As NotesDatabase
Dim agent As NotesAgent
Dim col As NotesDocumentCollection
Dim doc As NotesDocument
Dim item As NotesItem
Dim stringtoreplace As String
Dim stringtoreplacechars As Integer
Dim replacementstring As String

Dim newvalues() As String ' Array List of values of an item and its replacements
Dim newvalue As String ' current value being transferred or replaced

Dim count As Integer

' _________________________________

stringtoreplace = "OldDomain/"
replacementstring = "NewDomain/"

' _________________________________

stringtoreplacechars = Len(stringtoreplace) -1
Set db=s.CurrentDatabase
Set agent = s.CurrentAgent
Set col = db.UnprocessedDocuments
Set doc = col.GetFirstDocument
While Not (doc Is Nothing)
' get items out of doc - FullName, DisplayName and Owner (FullName is multivalue)
Set item = doc.GetFirstItem("FullName")
count=0
Forall v In item.Values
Messagebox( v )
' update item --> check and see look for and replace search string
newvalue=v
Do While Instr(newvalue, stringtoreplace) > 0
' scan for replacement
newvalue = Left$(newvalue, Instr(newvalue,stringtoreplace)-1) + replacementstring + Right$(newvalue,Len(newvalue)-(Instr(newvalue, stringtoreplace) + stringtoreplacechars))
Loop
' redim array for new size
Redim Preserve newvalues(count)
newvalues(count) = newvalue
count = count+1
End Forall
' replace old string value(s) with new string value(s)
Set item = doc.ReplaceItemValue( item.Name, newvalues)

' reset variables for next field
count=0
Redim newvalues(0)

' do DisplayName field
Set item = doc.GetFirstItem("DisplayName")
Forall v In item.Values
Messagebox( v )
' update item --> check and see look for and replace search string
newvalue=v
Do While Instr(newvalue, stringtoreplace) > 0
' scan for replacement
newvalue = Left$(newvalue, Instr(newvalue,stringtoreplace)-1) + replacementstring + Right$(newvalue,Len(newvalue)-(Instr(newvalue, stringtoreplace) + stringtoreplacechars))
Loop
' redim array for new size
Redim Preserve newvalues(count)
newvalues(count) = newvalue
count = count+1
End Forall
' replace old string value(s) with new string value(s)
Set item = doc.ReplaceItemValue( item.Name, newvalues)

' reset variables for next field
count=0
Redim newvalues(0)

' do Owner field
Set item = doc.GetFirstItem("Owner")
Forall v In item.Values
Messagebox( v )
' update item --> check and see look for and replace search string
newvalue=v
Do While Instr(newvalue, stringtoreplace) > 0
' scan for replacement
newvalue = Left$(newvalue, Instr(newvalue,stringtoreplace)-1) + replacementstring + Right$(newvalue,Len(newvalue)-(Instr(newvalue, stringtoreplace) + stringtoreplacechars))
Loop
' redim array for new size
Redim Preserve newvalues(count)
newvalues(count) = newvalue
count = count+1
End Forall

' save the changes
Call doc.Save(True, False)

' cycle to next doc
Set doc = col.GetNextDocument(doc)
Wend

End Sub

previous page