Problems with arrays in LS, please help

Hi,at the moment I’m trying to code an agent that will delete all “$UpdatedBy” Fields from an database by copying all documents to a new version, removing the field, adding back referrers and saving the new document.

My code have some issues with setting the “$REF” fields into the right order, it seems both arrays in the first “for-loop” don’t get the UNIDs of the main documents so the later following “Arraygetindex” in the second “for-loop” can’t find anything to make a reference to. The Error I get in this line is “Type Mismatch”.

I hope you are able to understand what I like to achieve. Thanks for any help in advance.

Greets,

Marco

Code starts here:

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim newdoc As NotesDocument

Dim collection As NotesDocumentCollection

Dim j As Integer

Dim i As Integer

Dim s

Dim item As NotesItem

Dim docUNIDs (1 To 1000) As String

Dim newdocUNIDs (1 To 1000) As String

Dim valueArray

Dim ref As NotesItem



Set db = session.CurrentDatabase

Set collection = db.AllDocuments



For j=1 To collection.Count

    

    Set doc = collection.GetNthDocument(j)

    If (Not doc.IsResponse) Then

        docUNIDs(j) = doc.UniversalID

        Set newdoc = New NotesDocument( db )

        Call doc.CopyAllItems( newdoc, True )

        newdocUNIDs(j) = newdoc.UniversalID

        Set item = newdoc.GetFirstItem("$UpdatedBy")

        If (item Is Nothing) Then

            Call newdoc.Save(True,False)

            Call doc.RemovePermanently( True )

        Else

            Call item.Remove

            Call newdoc.Save (True, False)

            Call doc.RemovePermanently( True )

        End If

    End If

Next



For i=1 To collection.Count

    

    Set doc = collection.GetNthDocument(i)

    If (doc.IsResponse) Then

        Set newdoc = New NotesDocument( db )

        Call doc.CopyAllItems( newdoc, True)

        Set ref = doc.GetFirstItem("$REF")

        valueArray = ref.Values

        s = Arraygetindex( docUNIDs, valueArray )

        Set ref = New NotesItem( newdoc, "$REF", newdocUNIDs(s) )

        Set item = newdoc.GetFirstItem("$UpdatedBy")

        If (item Is Nothing) Then

            Call newdoc.Save(True,False)

            Call doc.RemovePermanently( True )

        Else

            Call item.Remove

            Call newdoc.Save (True, False)

            Call doc.RemovePermanently( True )

        End If

    End If

    

Next

End Sub

Subject: Problems with arrays in LS, please help.

Thanks for your suggestions, I incorporated the first one like the code in this posting.

The second suggestion seems to be much easier I thought about this first but for one thing I wasn’t sure. Good reminder I’ll try this too.

If anyone can make some suggestions or notices about my code please add your comments.

Greets,

Marco

Code so far (not working):

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim newdoc As NotesDocument

Dim collection As NotesDocumentCollection

Dim responses As NotesDocumentCollection

Dim j As Integer

Dim i As Integer

Dim s

Dim item As NotesItem

Dim docUNIDs (1 To 1000) As String

Dim newdocUNIDs (1 To 1000) As String

Dim valueArray

Dim ref As NotesItem



Set db = session.CurrentDatabase

Set collection = db.AllDocuments



For j=1 To collection.Count

    

    Set doc = collection.GetNthDocument(j)

    If (Not doc.IsResponse) Then

        docUNIDs(j) = doc.UniversalID

        Set newdoc = New NotesDocument( db )

        Call doc.CopyAllItems( newdoc, True )

        newdocUNIDs(j) = newdoc.UniversalID

        Set item = newdoc.GetFirstItem("$UpdatedBy")

        If (item Is Nothing) Then

            Call newdoc.Save(True,False)

            Call doc.RemovePermanently( True )

        Else

            Call item.Remove

            Call newdoc.Save (True, False)

            Call doc.RemovePermanently( True )

        End If

    End If

Next



Set responses = db.AllDocuments



For i=1 To responses.Count

    

    Set doc = collection.GetNthDocument(i)

    If (doc.IsResponse) Then

        Set newdoc = New NotesDocument( db )

        Call doc.CopyAllItems( newdoc, True)

        'Set ref = doc.GetFirstItem("$REF")

        'valueArray = ref.Values

        s = Arraygetindex( docUNIDs, doc.ParentDocumentUNID)

        'Set ref = New NotesItem( newdoc, "$REF", newdocUNIDs(s) )

        Call newdoc.MakeResponse( db.GetDocumentByID(newdocUNIDs(s)) )

        Set item = newdoc.GetFirstItem("$UpdatedBy")

        If (item Is Nothing) Then

            Call newdoc.Save(True,False)

            Call doc.RemovePermanently( True )

        Else

            Call item.Remove

            Call newdoc.Save (True, False)

            Call doc.RemovePermanently( True )

        End If

        

    End If

    

Next

End Sub

Subject: Problems with arrays in LS, please help.

Would it be far better to set the advanced database options? Just set it like below (see last two properties on the last property tab of the db) and the updated fields will shrink to one entry, which they would with your approach as well, then touch every document and save it.

Subject: RE: Problems with arrays in LS, please help.

I tried your suggestion with this code and the two options set to “1” but still the “$UpdatedBy” Fields don’t change better reverse to just one entry.I used this code:

Sub Initialize

Dim session As New NotesSession

Dim db As NotesDatabase

Dim doc As NotesDocument

Dim collection As NotesDocumentCollection

Dim i As Integer

Dim item As NotesItem



Set db = session.CurrentDatabase

Set collection = db.AllDocuments



For i=1 To collection.Count

	Set doc = collection.GetNthDocument( i )

	Set item = doc.ReplaceItemValue( "Cthulhu", "Fhtagn" )

	Call doc.Save( True, False)

	Call doc.RemoveItem( "Cthulhu" )

	Call doc.Save( True, False)

Next

End Sub

Subject: RE: Problems with arrays in LS, please help.

How about this instead?

	Set item = doc.RemoveItem( "$UpdatedBy" )

	Set item = doc.RemoveItem( "$Revisions" )

	Call doc.Save( True, False)

My recollections was that you could remove those fields and then they would be recreated with just the latest entry, but that may have been through the C API and not through LotusScript.

Subject: Problems with arrays in LS, please help.

heay Marco

you can get & set values from reserved fields in the following way

refValue = doc.~$Ref(0)

doc.~$Ref = refValue

Trying doing this way… and still you have a problem pls create a dummy db and send it to me I shall fix it up for u :slight_smile:

Cheers

KV

karpagavinayagamkp@in.ibm.com

Subject: Problems with arrays in LS, please help.

Marco

Forgot to add the following line in my previous post.

I am giving you a generic function which will update the $updatedBy field from the source to target. Go thru this function it should help you.

Function changeFieldValue(sourceDoc As notesdocument,targetDoc As NotesDocument)

Dim arrUpdatedBy()

Redim Preserve arrUpdatedBy(Ubound(sourceDoc.~$UpdatedBy))

For i = Lbound(arrUpdatedBy) To Ubound(arrupdatedBy)

	arrUpdatedBy(i) = sourceDoc.~$UpdatedBy(i)		

Next

'now put the arrUpdatedBy array to the $updatedBy field in your target document

targetDoc.~$UpdatedBy = arrUpdatedBy

Call targetDoc.Save(True,True)

End Function

Subject: Problems with arrays in LS, please help.

Marco

Forgot to add the following line in my previous post.

I am giving you a generic function which will update the $updatedBy field from the source to target. Go thru this function it should help you.

Function changeFieldValue(sourceDoc As notesdocument,targetDoc As NotesDocument)

Dim arrUpdatedBy()

Redim Preserve arrUpdatedBy(Ubound(sourceDoc.~$UpdatedBy))

For i = Lbound(arrUpdatedBy) To Ubound(arrupdatedBy)

	arrUpdatedBy(i) = sourceDoc.~$UpdatedBy(i)		

Next

'now put the arrUpdatedBy array to the $updatedBy field in your target document

targetDoc.~$UpdatedBy = arrUpdatedBy

Call targetDoc.Save(True,True)

End Function

Subject: Problems with arrays in LS, please help.

I haven’t studied your code in depth, but I would suggest that rather than manipulating the $REF field directly (dangerous IMHO) you consider using

notesDocument.ParentDocumentUNID

and

notesDocument.makeResponse(newParentDocument)

HTH - Rufus.