0
votes

I am trying to use the "ReplaceItemValue" in lotus script for a multi-value field, with the following code:

(Functionality is when the field 'Region' has the value "SL-BAO" or "SL-S&T" it should replace them with "SL-S&A". Other values should be kept as such)

Sub Initialize
    Dim session As New NotesSession
    Dim uiws As New NotesUIWorkspace

    Dim doc As NotesDocument

    Dim doccoll As NotesDocumentCollection
    Dim i,j As Integer
    Dim itm1,itm2 As NotesItem

    Set uiView = uiws.Currentview
    Set docColl = uiView.Documents
    Set doc = docColl.Getfirstdocument()
    Dim tmpval() As String
    j=1
    While Not doc Is Nothing
        For i=0 To UBound(doc.Region)
            ReDim Preserve tmpval(i+1) 
            If (doc.Region(i) ="SL - BAO") Or (doc.Region(i) ="SL - S&T") Then
                tmpval(i)="SL - S&A"
            Else
                tmpval(i)=doc.Region(i)
            End If
        Next

        Call doc.ReplaceItemValue("Region",FullTrim(tmpval))
        Call doc.Save(True,False)       
            Set doc=doccoll.GetNextDocument(doc)
            Wend
End Sub

Unfortunately, the code doesn't properly replaces the values. When the field has "SL-S&T" alone, after running the script, it returns with two values.. Similar scenario for all cases.. the code keeps returning garbage values.

Kindly help... Thanks in advance..

2
Rather than writing tmpval(i)="SL - S&A" try using ArrayAppend to add new elements to the end of your array.Naveen
I would suggest using "Do Until Doc is Nothing" instead, it is easier to read and maintain in the future. Also, I recommend never to use extended notation for fields like you do, use the GetItemValue() method instead.Karl-Henry Martinsson

2 Answers

1
votes

The problem in your code is this line:

ReDim Preserve tmpval(i+1)

This will never clean "garbage" values from a former document.

Example:

First Document has Values "SL - BAO" : "Something".

just before getting the next document in the line Set doc=doccoll.GetNextDocument(doc) the tmpval has the values tmpval(0) = "SL - S&A", tmpval(1) = "Something".

Now you "redim" the array, but use the preserve flag. So after the redim, both values are still in tmpval. If your second doc only has one value (SecondDocVal), then tmpval will look like this:

tmpval(0) = "SecondDocVal", tmpval(1) = "Something"

And this value will be written back to your document.

You could prohibit this by changing the beginning of your script like this:

instead of using:

   For i=0 To UBound(doc.Region)
        ReDim Preserve tmpval(i+1) 

write:

   ReDim tmpval(Ubound(doc.Region)
   For i=0 To UBound(doc.Region)

That way, there is no "preserve".

Nevertheless: This is only an explanation for why your code does not work.

A better solution would be to use "Replace"- function as stated in the second example in the answer of nempoBu4.

1
votes

Your code doesn't properly replaces the values because of errors as stated in the answer of Torsten Link.

Here is some solutions for your issue:
1: You can put doc.Region values to tmpval, replace values in tmpval and put it back to doc.Region.

'Your code

Dim tmpval As Variant

'Your code

tmpval = doc.Region

For i = 0 To Ubound(tmpval)
    If (tmpval(i) = "SL - BAO") Or (tmpval(i) = "SL - S&T") Then
        tmpval(i) = "SL - S&A"
    End If
Next

doc.Region = tmpval

'Your code

2: You can use Replace function:

'Your code

Dim findArray(1) As String
Dim replacementArray (1) As String

findArray(0)="SL - BAO"
findArray(1)="SL - S&T"

replacementArray(0)="SL - S&A"
replacementArray(1)="SL - S&A"  

'Your code

doc.Region = Replace(doc.Region, findArray, replacementArray)

'Your code

3: You can use @Replace @-function and Evaluate statement:

'Your code
doc.Region = Evaluate({@Replace(Region; "SL - BAO" : "SL - S&T"; "SL - S&A" : "SL - S&A")}, doc)
'Your code

As suggested by Karl-Henry Martinsson if you are an inexperienced developer, then try to avoid the use of Evaluate() all the time.