ForEachOf Duplicates Assignment Calls

Hello,

If I am iterating over a collection of elements (say List) and have a counter I wish to use that is scoped to the foreach, the system will add 2 on each iteration instead of just 1.

I have attached an example workflow that demonstrates this. The value for InternalScopedCounter (the variable scoped to the foreach) will either start at 1 and increase by 2 on each iteration or it will start at 4 and increase by one for each iteration. ExternalScopedCounter (the variable scoped to the root sequence) will behave as expected.

TestCollectionIteration.json (9.8 KB)

I don’t have OpenRPA at hand right now, but if you look at the source you can see it calls IncIndex for each row, and without being able to debug it right now, i think that is increasing all variables created on activity level, if it finds a “total” variable.I think that is a bug, it think it was supposed to call GetIndexVariable and increment that, if found.

Do you have a sequence inside the foreach ?
Normally you would have a sequence inside the ActivityAction, and therefore also have all user created variables inside the sequence scope

Hi Allan,

Yes, there is a sequence inside of the ForEachOf, but I can’t scope the instance counter to that inner because it will re-initialize the variable on each iteration. Scoping it to the ForEach action seems to have this issue while scoping it to the root sequence does not.

It doesn’t look like ForEachOf offers its own instance counter in the variables pane. If it did, I could use that instead of creating my own and maintaining that.

Hmm, it has been forever since i touched this code.
but just did a quick test, i see only “foreach data row” add’s the “Index” and “Total” variable on the activity scope. If i test those in foreach data row it seem to work, but if i re-create them manually in either foreach data row", “foreach” “do” and “loop” it either add’s to the value or reset them all to 0 … what a horrible thing i made there.

I have created a new buiild of OpenRPA that fixes that code. 1) it gets the right variable ( index or total and don’t care if only one is there ) 2) it actually does the update correctly ( i hope )

Now, back to you point.
Yes, you are right, the variables inside a sequence are reset doing each loop, since the scope is inside the body of the activity.
So you can either declare the index variable outside the looping activity and increment it yourself doing each loop, or you can update openrpa to 1.4.57.4 or never for anyone reading this in the future and use my “hijacking” of Index and Total and make sure scope is the activity, not the sequence inside the activity

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