How count a number of iterations in a for each

how count a number of iterations in a for each, i try activity assign counter = counter + 1 but the output is 1 always

Hi,
Do you have a screenshot of what you’re working with?

counter=counter+1 should be inside the For Each at the start ideally. And, it should be of Integer type.

Put counter = 0 outside for each
and
counter = counter + 1
inside for each

5 Likes

thanks

And how to get the last counter result,if i need the data some where else like excel or in a message box etc?

hi @Gary_K,

Tried the counter in the same way, but whenever a new iteration is started the counter value is forcefully being set to 0, not able to get the count correctly.

now you can use the index is more easier than before

1 Like

If it is getting set to 0, it’s probably in the wrong scope. Check your variable pane and look at your variable’s scope column to make sure it is in a scope “outside” of the For each. The variable gets declared at the beginning of the scope and will reset each time it gets to that scope.

Also, go with @luchovelez suggestion. In the newer versions of Studio, the For each has an index output.

4 Likes

Yes as u have suggested in the newer version we are having the index of but the value is not giving, give text position.
For this purpose, I have used the LookUpTable which gives me the correct position but the problem is that it works fine when the string is supplied directly and when that is being replaced with string variable is not able to identify the item in the list.

How would one use the For Each index output?

Hi @cambiukpytou

A few common uses for the index output:

  1. You want to pull from another array that shares the same order. Let’s say you are renaming the columns in a datatable from an array. For each col In dt1.Columns.Cast(Of DataColumn()), then use Assign: col.ColumnName = arrHeaders(index)
  2. You want to keep track of position in loop so you can continue where it left off. For each row In dt1.AsEnumerable.Skip(index)
  3. Or just to Log the item number

There are probably many approaches to use the index output.

I hope these are some useful ideas for you.

Regards.

1 Like

Thanks it worked for me i am a newbie in UI path . Thanks for the post. Thanks Clay

Hello Everyone

I’m not sure what’s wrong with my code but the counter is not adding. :frowning: could someone help me please? ThanksBook1.xlsx (8.5 KB) Counter.xaml (7.5 KB)

Hello Clayton,
I am facing a similar issue of incrementing the counter.I am having a process in REFramework.
I am declaring a counter variable as value 2 and writing back to excel and then incrementing by1 but when it again comes to process new transaction it is reseting back to 2 and not 3.
This is what i have done.Can you please help

Hi @amitkk_84 Your scope is set to “Sequence1” for the variable, which means it will get declared at the start of that sequence. If you don’t want it to be declared again, then you need to set the scope to a more outer scope. If you only want it to declare once, then set it to the outermost scope (also known as making it global).

I hope this helps.

Regards.

Here Problem is eventhough i have declared in the body section , for the 1st iteration it will work but again for the second iteration i gets reset.
For e.g. In the screenshot even though i declare anywhere in the workflow itgets reset to 2
Also i have given default value as 2.

Since the scope is “Create_SO_VA01”, it will be reset to 2 at the start of the sequence. To make it global to your entire project/framework, you need to change it to an argument with an IN/OUT direction, so it will maintain the value throughout the project. Then, you can declare it as global in your Main .xaml and pass it in as an IN/OUT argument.

Hi Clayton,
I have tried using the argument variables but still i m facing the same issue.
Please find the below attachment what i have done.Variables issue.docx (1.2 MB)

I’m seeing that you have Out_C in the Main.xaml set to an OUT argument; you should use a variable, not an argument, here, unless you want to manually change this value from within Orchestrator. Since you get this from the GetData.xaml, I am assuming you don’t want to do that.

The reason it’s resetting back to its initial value of “2” is that I believe the “Assign: in_C1=in_C1+1” is inside the Create_SO .xaml, therefore the value after you add 1 never gets out of that workflow. You need to set it as an IN/OUT direction. Or, wherever you are doing counter+1 it should be an IN/OUT argument, so the value can be passed “out” of the workflow and back “in” with the changed value.

Also, if you are using transactional process using the Framework, where it goes to the “Get Transaction” state, this means that your projects exits out of the Process.xaml and goes to the Get Transaction state, then re-enters the Process.xaml. This means that the counter either needs to be changed/added inside the Main.xaml or changed to an IN/OUT direction for the Process.xaml if it is changed/added inside the Process.xaml.

Also, keep in mind that the REFramework uses the TransactionNumber variable/argument to control the count of the transactions. So you can see how that is used if it helps.

Let me know if you are still confused.

Regards.

Hello Clayton,
I am still a bit confused. Sorry for bothering too much.I changed the value of in_C1 to in/out argument but still it is resetting to 2.