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

Question

Question

VB.NET Script Error (Unable to cast object type "System.String" to type "System.Collections.Generic.List'1[System Object]")

asked on July 12, 2018

Hello!

This is pretty frustrating, because I've used this before and it's worked perfectly, but for some reason is not working now, when I've got a deadline in a few hours.

It's super simple stuff, I've got a multi-value token (named Type) collecting a string in Workflow, and I'm trying to pass that token to a VB script (to create a CSV file).

The actual code I'm using for this is:

Dim Type As Collections.Generic.List(Of Object) = GetTokenValue("Type")

 

This will run until it hits this line, and then throws an error:

Unable to cast object of type "System.String" to type "System.Collections.Generic.List'1[System Object]"

 

Normally, I'd make sure my variable is declared as a list before assigning it to avoid casting, but with the GetTokenValue function, I'm pretty limited. That said, I've used this exact line of code in the past, and have not had issues. Can anyone think of something I could try to get around this?

Thanks!

0 0

Answer

SELECTED ANSWER
replied on July 13, 2018

I would not have expected that behavior but I can duplicate it as well.  If a multivalue token only has one value is is passed as a String not a List(Of Object).  Strangely though if the multivalue token has no values it gets passed as an empty  List(Of Object)!

There might be a more elegant solution but you could always do a type check at the beginning of the routine;

            Dim passedToken As Object = Me.GetTokenValue("type")
            Dim typeList As List(Of Object) = New List(Of Object)

            If (TypeOf passedToken Is String) Then
               typeList.Add(passedToken)
            Else
               typeList = passedToken
            End If

 

2 0

Replies

replied on July 12, 2018

Peter,

This works for me;

            Dim type As List(Of Object) = Me.GetTokenValue("type")
            Me.SetTokenValue("ObjectCount", type.Count)

The token 'ObjectCount' returns the count of values in the multivalue token 'type' without any error. 

As a suggestion though I would use a more descriptive name for the list of object, perhaps something like 'typeList' instead of 'type'  to avoid any confusion with the Type class...

0 0
replied on July 12, 2018

It might be too basic but are you sure the token 'type' is set up as a multivalue token?  Also, perhaps disabling the SDK Script activity and adding a Track Tokens activity to look at the value of the 'type' token that the script is using to see if the values look appropriate?

0 0
replied on July 12, 2018

Hi Cliff!

 

Thanks for the reply. I've given your change a try, and unfortunately still yield the same issue. 

I had not considered that there might be a Type class in VB already (new to the language), so I've taken your recommendation and changed it to typeList instead.

Feel free to lob basic checks, it's always appreciated. I did make sure the token was set to multi-value, and I'm running the script from inside the VB editor so I can make sure the values entered are legitimate. 

0 0
replied on July 12, 2018

I didn't think to run the script from the editor, in my test I published the workflow  and ran it to check for errors! 

When I run the script above from the editor it fails with the same error you report.  The help system indicates that you can enter a multivalue token in the editor by separating the values with a semicolon but that doesn't appear to work.

Does the script fail when you publish and run the workflow?

0 0
replied on July 12, 2018

By running the script as part of the workflow, it sends a warning message to workflow, reporting that error. It doesn't run any of the code after that error (which is all of it, because this is the second assignment).

0 0
replied on July 13, 2018

I put together a simple test workflow that retrieves a multivalue token and iterates through it reporting back each object type and value in the list object.

        Protected Overrides Sub Execute()
            'Write your code here. The BoundEntryInfo property will access the entry, RASession will get the Repository Access session
            Try
                ' Get MultiValue Token
                Dim lstTypes as List(of Object) = GetTokenValue("Type")
                ' Make sure the object is not null
                If lstTypes IsNot Nothing Then
                    ' Make sure there 1 or more items in object
                    If lstTypes.Count > 0 Then
                        ' Process each item in object
                        For iIndex As Integer = 0 To (lstTypes.Count - 1)
                            Dim sType As String = Nothing
                            ' Retrieve object type at index
                            Dim objType As Object = lstTypes(iIndex)
                            ' Cast object to string
                            sType = objType.ToString()
                            ' Report object index, type, and value to Workflow Messages
                            WorkflowApi.TrackInformation(iIndex.ToString() & ": " & objType.GetType().ToString() & " : " & sType)
                        Next
                    End If
                End If
            Catch ex As Exception
                ' Report error to Workflow Messages
                WorkflowApi.TrackError(ex.Message)
            End Try
        End Sub

Resulting messages when run:

0 0
replied on July 13, 2018 Show version history

I changed up my workflow and set it to read a multivalue field from a document.  I got the same error you see if the field only has 1 value.

Here is how I modified the code to handle MultiValue field with just a single value

        Protected Overrides Sub Execute()
            'Write your code here. The BoundEntryInfo property will access the entry, RASession will get the Repository Access session
            Try
                ' Get MultiValue Token
                Dim objTypes as Object = GetTokenValue("Type")
                ' Make sure the object is not null
                If objTypes IsNot Nothing Then
                    Dim sObjectType As String = objTypes.GetType().ToString()
                    WorkflowApi.TrackInformation(sObjectType)
                    Select Case objTypes.GetType().ToString()
                        Case "System.Collections.Generic.List`1[System.Object]"
                            Dim lstTypes as List(of Object) = GetTokenValue("Type")
                            ' Make sure there 1 or more items in object
                            If lstTypes.Count > 0 Then
                                ' Process each item in object
                                For iIndex As Integer = 0 To (lstTypes.Count - 1)
                                    Dim sType As String = Nothing
                                    ' Retrieve object type at index
                                    Dim objType As Object = lstTypes(iIndex)
                                    ' Cast object to string
                                    sType = objType.ToString()
                                    ' Report object index, type, and value to Workflow Messages
                                    WorkflowApi.TrackInformation(iIndex.ToString() & ": " & objType.GetType().ToString() & " : " & sType)
                                Next
                            End If
                        Case "System.String"
                            ' Handle String types
                            Dim sTypes as String = GetTokenValue("Type")
                            WorkflowApi.TrackInformation("0: " & sTypes.GetType().ToString() & " : " & sTypes)
                        Case Else
                            ' Handle Any other types
                            Dim oTypes as Object = GetTokenValue("Type")
                            WorkflowApi.TrackInformation("0: " & oTypes.GetType().ToString() & " : " & oTypes.ToString())
                    End Select
                End If
            Catch ex As Exception
                ' Report error to Workflow Messages
                WorkflowApi.TrackError(ex.Message)
            End Try
        End Sub

 

2 0
SELECTED ANSWER
replied on July 13, 2018

I would not have expected that behavior but I can duplicate it as well.  If a multivalue token only has one value is is passed as a String not a List(Of Object).  Strangely though if the multivalue token has no values it gets passed as an empty  List(Of Object)!

There might be a more elegant solution but you could always do a type check at the beginning of the routine;

            Dim passedToken As Object = Me.GetTokenValue("type")
            Dim typeList As List(Of Object) = New List(Of Object)

            If (TypeOf passedToken Is String) Then
               typeList.Add(passedToken)
            Else
               typeList = passedToken
            End If

 

2 0
replied on July 13, 2018

Cliff

nice, simple, clean solution

0 0
replied on July 13, 2018

Hey guys, thanks for the assist!

 

That is some odd behavior, but checking for String vs. List of objects did the trick! Too bad I have 30 variables I need to check this with... but no matter! I have a solution!

 

Thanks again

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

Sign in to reply to this post.