WHILE inside FOR EACH activity

I have to save invoices in a program from an excel file. Invoices can have 1 or more items. If there are multiple items, I have several excel rows with the same invoice number.
I have made an activity FOR EACH to read each invoice and in the body I need a loop. As long as the invoice number is the same, the items must be recorded within the same invoice.
I can’t get the while loop to work properly.
Maybe someone knows how to fix it. Thank you!!

image

@ROSARIO_JIMENEZ_MARTINEZ
Welcome to the forum

We would suggest to group the data as described here (NON-Linq / LINQ Approach)

Then loop over the group and process its members as needed

1 Like

@ROSARIO_JIMENEZ_MARTINEZ ,

Welcome to UiPath Community!

Follow the below steps:

  1. Declare a variable to hold invoice value
  2. Use FOR EACH check if Invoice variable empty or not. If empty, assign current iterating invoice to Invoice variable. If not empty, Check if Invoice variable is same as current iteration invoice. If same record as same invoice else assign current iterating invoice to Invoice variable

Hope this will help.

Thanks,
Ashok :slight_smile:

1 Like

There’s no need for a while loop. You just need a variable that keeps track of the previous invoice number and then compare it with the current invoice number in the For Each Row loop.

If-condition: CurrentRow("Num_Invoice").ToString.Equals(previousInvoice)

2 Likes

Thank you very much, you are right, this is the best way to solve my problem. Just one thing, the first row compares with the table header. Is there any way to recognize that it is the first row?

@ROSARIO_JIMENEZ_MARTINEZ while reading the excel with Read Range checkbox the property AddHeaders this will add first row as header and while iterating it will not iterate. Only data rows will be iterated.

1 Like

Perfect, thank you!!! :wink:

1 Like

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