You are viewing limited content. For full access, please sign in.

Question

Question

Problem using SDK to clear template field list

asked on April 3, 2019 Show version history

I have an SDK script I'm using to try to clear and repopulate a template field list.  When I use the line For i As Integer = 0 To fieldList.Count - 1 I get an Index out of Range error.

If there are 7 values in the template field list and I use For i As Integer = 0 To 3 the script removes every other value.

Here's the script:

Try

        'Get a reference to the field that contains the list...

        Dim myField As FieldInfo = Field.GetInfo("Send To Packet", Me.RASession)

        'Instantiate a copy of the list itself...

        Dim fieldList As List(Of String) = myField.GetItemList

   'Step through the list and remove all items...

        For i As Integer = 0 To fieldList.Count - 1  **This gives an Index out of Range error

        ‘For i As Integer = 0 To 3  ** This removes every other value

           fieldList.RemoveAt(i)

       Next


'add items in ListOfPacketFolders to the template list

'for each x as string in FolderList

'    fieldList.Add(x)

'next

        'Persist the changes...

        myField.SetItemList(fieldList)

        myField.Save()

        'Cleanup...

        myField = Nothing

        fieldList = Nothing

    Catch ex As Exception

        'Pass any error messages back to workflow...

        WorkflowApi.TrackError(ex.Message)

    End Try

Thanks!

0 0

Answer

SELECTED ANSWER
replied on April 3, 2019

Janice,

The issue is that the list itself is changing for each iteration.  There are several ways to iterate through a list and delete each item;

You can iterate though the list in reverse order; For i As Integer = fieldList.Count -1 To 0 Step -1 and do the RemoveAt(i)

Or, more easily, you can just clear the list with the Clear method; fieldList.Clear

My question would be why you are loading the list up to just delete the contents?  Couldn't you just instantiate a new List(Of Object) and add the folder contents to it, then set the item list property with the new list?

 

0 0
replied on April 3, 2019

I was using an example from here to modify the list but your suggestion is much better.  I will try just creating a new list and replacing the old one.

 

Thanks!

0 0
replied on April 3, 2019

That worked perfectly.  Thanks!

0 0

Replies

You are not allowed to reply in this post.
You are not allowed to follow up in this post.

Sign in to reply to this post.