How can I remove one element in a field without removing all values and replacing them?
I want to remove a element from a multi-valued field (specifically, the third element).
myfield(0)-"red"I want to remove element "white" from the field. How can this be done in script without removing all of the values and replacing them?
myfield(1)="blue"
myfield(2)="white"
myfield(3)="green"
Either use Evaluate (inefficient, and the formula can be complex) or write LotusScript code to remove the unwanted item from the array, then assign the field from the array.
As with any other task that's more complex than you might like, you can make it easier on yourself by modularizing the code into reusable bits. For example, you could use the following routine:
Sub RemoveElementFromField (doc As NotesDocument, Byval fieldname As String, Byval index As Integer) Dim value As Variant, i As Integer value = doc.GetItemValue(fieldname) If index > 0 And index <= Ubound(value) Then If Ubound(value) = 0 Then doc. ReplaceItemValue fieldname, "" Else Redim Preserve value(0 To Ubound(value) - 1) For i = index To Ubound(value)-1 value(i) = value(i+1) Next doc.ReplaceItemValue fieldname, value End If End If End SubOnce you have this, you can remove the value you wanted from the array using a statement such as:
RemoveValueFromField doc, "Colors", 2If you wanted to be more general about it, you could write a routine to manipulate arrays:
Function ArrayRemove(arr As Variant, Byval ind As Integer) As Variant ' Returns the array with the element at index 'ind' taken out.then (alternately) you could reassign t he field using the statement:
...
doc.Colors = ArrayRemove(myfield, 2)Also, you could write a function to scan the array for the value you wanted to remove, for cases where you don't know the index:
Function SearchAndDestroy(arr As Variant, value As Variant, Byval compMethod As Integer) Dim pos pos = ArrayGetIndex(arr, value, compMethod) If Isnull(pos) Then SearchAndDestroy = arr Else SearchAndDestroy = ArrayRemove(arr, pos) End If End Function