Variable Values get lost in For Each loop

Hi All

I’m encountering the following.

I’m looping over an array of strings, to find certain values within the strings.

In each iteration of the for each loop
I’m passing that string/line into an invoke activity
with an if inside and in that if an assign to an OUT variable.

Example the data I need is on line 5, 7 and 9
when the data from line 5 is extracted and the loop goes to line 6 I lose the value of the variable
same of 7 and 9.

The scope of the variable is set on the highest level. I’ve stepped through the entire array manually and it happens always when exiting the loop.

Can anybody assist?

Many thanks!
Kind Regards , Benjamin.

I’ve removed the invoke and placed the if’s in the for each but the issue reoccurs.


I’ve restarted UIPath and the issue happens only with the invokes without it functions normally

Hi @Benjamin_Degryse ,

Would you be so kind as to provide us with an example, or the sample data itself?
I don’t think its necessary to use an Invoke Activity to perform this, I can think of something once I see the data.

Kind Regards,
Ashwin A.K

You’re using the same variable every iteration of the loop and overwriting the value.

Hi Paul

I’ve stepped with debug through the entire document, the value only gets put into the variable when the if condition is true, so no that is not the case.

The values are lost when a new iteration of the for each starts.

Many thanks.

Hi Ashwin

That is correct that it is not necessary to use an invoke activity for this. I’ve moved the logic from in the invoke activity into the for each and it works as it should.

But I would still like to know how or why the values are lost with an invoke activity.

Many thanks!

Hi @Benjamin_Degryse ,

Without having a Visual on the Workflow Developed, It would be difficult for us to Provide you with the Exact reason, But if you are using an Invoke Code Activity, then it might be due to the way the arguments are passed i.e direction of the Arguments, In/Out.

If you could provide us with more details we could confirm the case.

Then you have the variables scoped to the For/Each’s internal sequence. You said you scoped them to the highest level, but are you sure you don’t have duplicates?

Hi SupermanPunch

I’m using an invoke activity not an invoke code.

I pass in a file as string
I split that string into an array of strings
then loop over that array of strings
i pass each line into an activity
which checks if some value occurs in that string
if so it assign a substring to an out argument
which gets auto assigned to a variable in the import arguments.
if not nothing happens and the loop continues

Here below some screen caps

i’ve commented the invoke activities as i move the logic into the for each.

Many thanks!

Hi @Benjamin_Degryse, check the scope of the datatable and any duplicate variable created.


Hi Paul

I’ve double checked no duplicates in the variable names and indeed scoped to the highest level.
Without an invoke activity it all works, with it does not.

Many thanks!

Hi Arivu

No datatable but just strings
no duplictes
and variables are scoped to the highest level.

Many thanks!

HI @Benjamin_Degryse ,
For all the lines you are calling invoke method and assign to the same variable. for 5th line we are getting output but for 6th record not getting the value since condition not satisfied and returns empty value and assigning to same variable so previous value will be lost.


Hi Arivu

I understand what you mean! and that could be the case!

Many thanks for your insight!

Then you’re doing something wrong with the arguments of the Invoke Code. But you don’t need Invoke Code. Just use an Assign.

Hi Paul

I am not using invoke code.
I was using invoke activity.

Please check the reply of Arivu.
It’s because with an invoke activity, if no assign happens the activity returns an empty value
which gets assigned to the variable ands thus the value gets lost.

The solution is not using an invoke activity but just assign.

Many thanks!

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