25
Views
6
Comments
Solved
List was redefined while being iterated
Question
Application Type
Service

Hi Team,
Stuck with a very complicated issue .In the OnAfterFetch of the Aggregate, I am looping through the result list and updating fields assigning it to another list which is also bound to the UI .And when i click on sorting twice it gives me error like "List was redefined while being iterated" as on now i have used a loader in the onafterfetch action but can we have a permanent and proper solution to this .

Note - Data on the Ui is from aggregrate GetJm which is coming from external database

Assign 1- JId = GetJM.List.Current.jobmonitoring.id
             2 - CStId = GetJM.List.Current.jm.stid

In both the assignment inside the loop I am assigning 
GetJM.List.Current.LastUpdatedBy = GetRAforUpdatedByUsername.List.Current.User.Name
Record List of Loop = GetJM.List
Feel free to ask any questions. 
Solution with screenshot or oml will be highly appreciated

2026-01-28 16-57-48
Mihai Melencu
Champion
Solution

Hi @Siddharth Vyas ,

The error occurs because you are trying to refresh the same list that is being iterated in a For Each loop. While a list is under iteration, its source cannot be modified or redefined.

Additionally, it is not a good practice to use aggregates directly in For Each loops.

Instead of refreshing the aggregate, you can use a List Filter action and apply the required condition.

Hope this clarifies the issue.

2022-06-23 06-21-50
Siddharth Vyas

Let me try this solution.

2022-06-23 06-21-50
Siddharth Vyas

When i implemented this its working as I am not able to see "List was redefined while being iterated " but in  my last updated by column it is updating name of only 1 user for all the  records. Eg if my 1st entry is updated by User1 and Second entry by User2 .In last updated by column it shows User1 for all the records

2026-01-28 16-57-48
Mihai Melencu
Champion

It’s not possible to pinpoint the exact issue with the information provided, but it’s likely that you’re always returning the first record of the list instead of iterating through it. I recommend debugging the logic to confirm this. If you can share your OML, I can also take a closer look. 

2025-03-19 06-26-09
Mayur Budukale
Champion
Solution

Hi Siddharth, 

The issue looks like your are trying to modifying and refreshing the same Aggregate list (GetJM.List) while looping over it in OnAfterFetch, and the UI (sorting) re-triggers the Aggregate at the same time.
This redefines the list mid-iteration, causing the runtime error “List was redefined while being iterated.” 

Here's how you could solve this. 

  • Create a new local list JM_EnrichedList and bind the UI to it.

  • In OnAfterFetch, do:  JM_RawList = GetJM.List.

  • Clear JM_EnrichedList.

  • Loop For Each JM_RawList.

  • Copy Local = JM_RawList.Current.

  • If Local.LastUpdatedBy <> "" then call GetRAforUpdatedByUsername.

  • Set Local.LastUpdatedBy = RA.List.Current.User.Name.

  • ListAppend(JM_EnrichedList, Local).

  • End For.

Never write to GetJM.List.Current or refresh inside the loop. Hope this helps

2026-01-28 16-57-48
Mihai Melencu
Champion
Solution

Hi @Siddharth Vyas ,

The error occurs because you are trying to refresh the same list that is being iterated in a For Each loop. While a list is under iteration, its source cannot be modified or redefined.

Additionally, it is not a good practice to use aggregates directly in For Each loops.

Instead of refreshing the aggregate, you can use a List Filter action and apply the required condition.

Hope this clarifies the issue.

2022-06-23 06-21-50
Siddharth Vyas

Let me try this solution.

2022-06-23 06-21-50
Siddharth Vyas

When i implemented this its working as I am not able to see "List was redefined while being iterated " but in  my last updated by column it is updating name of only 1 user for all the  records. Eg if my 1st entry is updated by User1 and Second entry by User2 .In last updated by column it shows User1 for all the records

2026-01-28 16-57-48
Mihai Melencu
Champion

It’s not possible to pinpoint the exact issue with the information provided, but it’s likely that you’re always returning the first record of the list instead of iterating through it. I recommend debugging the logic to confirm this. If you can share your OML, I can also take a closer look. 

2025-03-19 06-26-09
Mayur Budukale
Champion
Solution

Hi Siddharth, 

The issue looks like your are trying to modifying and refreshing the same Aggregate list (GetJM.List) while looping over it in OnAfterFetch, and the UI (sorting) re-triggers the Aggregate at the same time.
This redefines the list mid-iteration, causing the runtime error “List was redefined while being iterated.” 

Here's how you could solve this. 

  • Create a new local list JM_EnrichedList and bind the UI to it.

  • In OnAfterFetch, do:  JM_RawList = GetJM.List.

  • Clear JM_EnrichedList.

  • Loop For Each JM_RawList.

  • Copy Local = JM_RawList.Current.

  • If Local.LastUpdatedBy <> "" then call GetRAforUpdatedByUsername.

  • Set Local.LastUpdatedBy = RA.List.Current.User.Name.

  • ListAppend(JM_EnrichedList, Local).

  • End For.

Never write to GetJM.List.Current or refresh inside the loop. Hope this helps

2024-10-05 13-30-20
Huy Hoang The

Hi @Siddharth Vyas ,

The problem occurs because you are reassigning the value of the list that is being looped. 

1. Within the for loop, you can not refresh the aggregate. You can use a list filter to replace it. 

2. U can use the List_temp have a structure similar to your list for the loop action


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