How to increment count inside a for each row

Hey Guys

I have tricky question. But to ask it, I have to explain my process. So sorry for the long message. But basically I have a process made that reads a excel and outputs a datatable. I then use a for each row to cycle through all the rows in the data table and carry out tasks based on names in the spreadsheet. Inside the for each row, I use workflows to navigate to the different applications. So when I get the main process workflow I want to increment A counter each time it hits this workflow. But because it’s in a for each row. The count is going through once and not increamenting as expected. As I want to use this count to limit this process for only 10 rows after it’s read and meeting some criteria. Any idea how to fix this count? It’s basically counting to ten like 111111111111 ten times inside of 123456…

Any advise would be very helpful?

1 Like

@bobby
There are several options to implement it. however when you read the docu to the for each row Activity there is a builtin functionalty called Index and a counter is incremented for each loop

Output

  • Index - A zero-based index that specifies what element of the current collection is being iterated, stored in an Int32 variable.
2 Likes

So the sequence be like this

FOR EACH ROW activity

IF condition like this
counter < 10
If true it will go to then part where we can keep the MAIN WORKFLOW
or
It goes to else part where we can use BREAK ACTIVITY so that the loop will stop once after reaching 10 records

Till then those 10 records will be processed one by one inside the loop

Cheers @bobby

1 Like

I am not sure what you mean. Also what other options. Can you please give me some more suggestions? It’s good to learn a variety.

Thanks

Hi @Palaniyappan

The problem is that I want it to increment the count if the rows are not completed. So there is 20 rows. 10 are completed. 10 are reaming so when the robot runs. I want it to ignore the first 10 using an if condition which I have written and then after that. Increment the count for ones that completed. So the problem is that the counter is not updating properly in the next workflow because of the for each row.

1 Like

We can do that with a variable like use a int_count variable of type int32 defined in the variable panel with default value as 0

So in first FOR EACH ROW LOOP use a if condition like this
int_count < 10
If true it will go to then part where we can keep the MAIN WORKFLOW
or
It goes to else part where we can use BREAK ACTIVITY so that the loop will stop once after reaching 10 records
—and next to this IF CONDITION a assign activity like this
int_count = int_count +1

This loop will process only till 10 records and those 10 count will be stored in a int32 variable

Now we go two options like either we can ignore the processed records before iterating and while iterating

So before iterating we can use a assign activity and delete those processed records with this expression
dt = dt.AsEnumerable().Skip(int_count).CopyToDatatable()

Where dt is the variable of type datatable

Or if we don’t want to delete the records and just to start from the left records to be processed
Then in FOR EACH ROW loop pass the variable dt as input
—inside the loop use a IF condition like this
dt.Row.IndexOf(row) > int_count
If true it will go to THEN part where we can keep the sequence of activities we want

Cheers @bobby

1 Like

@bobby
some members prefer to define a counter by their own, regardless if its already available by built in functionality index (as referenced above)
sometimes people like to shift to do-while loops and implement the counter by their own

in your case also another option could be as you want to limit the rows to 10 that in step before you do filter, limit the rows e.g. with filter, LINQ statement, Take Method …

However as more you are sharing your requirements as more an individual solution can be found with you. Let us know your open questions

1 Like

were we able to process this @bobby

1 Like

Yes buddy, sorry I forgot to wirte back in excitment. Thanks so much for your help. :slight_smile:

1 Like

Cheers
@bobby

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