For each fails on one process but executes on another process

Dear everyone

I am struggling to understand why my For Each is failing on one of my processes and yet it works on another process. Can someone locate the error?

This is the process that fails…

And this is the process that runs correctly…

Both processes uses a For Each loop where they loop through a dictionary. The process that fails does not even reach the Log Message, so I suspect it must either be the Log Message or the actual For Each loop, however, if this were the case then why does it not fail on the other process?

Ps. The url variable is a string (“WEBSITE”+item.Key()+“?”+String.Join(“&”, qparams))
Pss. The error I am getting is

For Each: Specified cast is not valid.

Hello, I just edited my post to include the error message, take a look at it :slight_smile:

Hi @nmjvk,

Did you check the properties panel for each activity, would you please share the screenshot to understand better what is the issue with For Each activity?

kind Regards

Yes, sure. Here, take a look. Left side is the one failing right side is the one who is correct

They seem identical to me.

@nmjvk As you are looping a dictionary, keys must be iterated and then we get the values for the respective keys. we cannot iterate the values of a dictionary directly without key.

Why can I do it on one process but not on the other? I mean, I can also change the log message to be item.key() and the same error will pop up.

Dictionary is a combination of key and value.
Value need to be stored in a key. Without the key name, we cannot get the value.
try with item.key().tostring

Hmmm… I do not think that is the specific case here. Doing item.Value() or item.Key() work completely correct on my first process, however, when I do it on my other process it fails.

@nmjvk can you show me “MATCHED_DUNS” variable type in failure case

Hi @nmjvk

Verify that the data types used in the dictionary and the variables used within the For Each loop are compatible

Hope it helps!!

Sure, here it is!

I am using an assign activity to add to the dictionary inside a For Each Row in Data Table

MATCHED_DUNS(CurrentRow(“KEY”)) = CurrentRow(“VALUE”).ToString


Hope this helps :slight_smile:

They should be compatible. I get no “compatibility” error. Moreover, in the first process it runs like it should

Can you check your type agument is same as this one

Yes, I see. I have the following…

I guess I would have to change this to System.Object, System.String? However, doing this gives me errors on my vairables inside the for each loop, is this supposed to happen? It says that I am not able to use the operand “+” now. Could this perhaps be because that UiPath does not like using “+” when dealing with objects?



Can you remove the for each and Re give again and pass the dict var in for each

Hope it helps!!

@nmjvk Yes, Your Key Type is Object and Value type is String. Then whenever you use Key then you will convert it to string by putting item(key).ToString

1 Like

Hello again Raja, your solution works great! If I could just ask you for one thing, could you write a summary of what you have to to do in a step by step guide so I can mark it as a solution? Right now your answers are spread throughout this post. :slight_smile:

It was type argument issue in For Each Activity.
I got your Variable type and made same variable in my own Studio.
Then I used Latest for Each Activity and gave that variable it.

Note: In latest for each it picks arugment type automatically according to your Variable.

I Took that argument type and sent you.
Your issue was in key type. It was object but you have selected as string.

1 Like

@nmjvk Thanks for kind words and support. Happy Automation.

1 Like

This topic was automatically closed 3 days after the last reply. New replies are no longer allowed.