660
Views
5
Comments
How to loop a list and filter the same list and update the records from filtered list
Application Type
Traditional Web
Service Studio Version
11.53.7 (Build 60881)

Hi All,

Currently I am trying to use a for each loop to iterate/loop a list, then I need to filter a list from the original list and update the records from filtered list back to original list record.

E.g. Main List has 100 records from different categories value like cat1, cat2, cat3.... then if it iterates the main list, say the List.Current.Category value is cat1 I will need to filter a list for cat1 to update all cat1's records in the main list. So after updating the cat1's records in the main list, when the second iteration of the main list happen, it will skip those records belong to cat1.

I was getting the iteration error like cannot filter while iterating a list/Duplicate is not a valid operation inside a StartIteration/EndIteration block.

**All the lists are not retrieved from any aggregate, they are uploaded lists.

[My bad for not able to attach the oml. This is not a part of my personal project].

2026-02-26 06-29-24
Rahul
 
MVP

HI,

this opreation will not happen on same list .you need to Create a another local variable with same list type inside action and use this list inside loop with orignal list.


Hope this will sovle you problem.

Regards

Rahul


2020-09-10 09-11-19
J u n i o r

@Rahul Sahu Hi, I have duplicated the list but it got the error raised in the first post also. Could you perhaps provide any more info regarding your suggestion?

2021-06-02 20-50-04
Márcio Carvalho

It's almost the same thing as this

https://www.outsystems.com/forums/discussion/78933/how-to-avoid-the-list-was-redefined-while-being-iterated-error/

But be careful that if you try to loop a list inside of a list is going to give you the same error.

So what you need to do is to create an aux list to use to loop because you cannot filter a list from a parent list that is being looped. The only thing you can do use is update using assign on the original list that is being looped or you can create aux variables that you will use to save data where at the end you save to your output.

In this case, I would:

  • create an aux variable, 
  • assign the values of the list you are filtering from the main list to the aux 
  • do the same condition you want to do on the list filter, but in this case, a simple condition using the if widget 
  • update the record that I am currently(looping) using the list index to find the index (that way you just need to use the listinfexof.position) to update the value like this listname[listindexof.position].value.

Let me know if you don't understand or if I said something wrong.

Thanks! :)

2020-09-10 09-11-19
J u n i o r

@Márcio Carvalho Hi, sorry for the similar title. Could you perhaps explain more on the flow using that aux variable? Because the lists mentioned in my case are actually not retrieved from aggregate.

2020-09-10 09-11-19
J u n i o r

Hi @Márcio Carvalho , 

Attached the proposed workflow of my question:

Community GuidelinesBe kind and respectful, give credit to the original source of content, and search for duplicates before posting.