34
Views
8
Comments
Solved
Remove multiple records of list inside a for each
Application Type
Reactive

I am iterating a local list, I added the CurrentRowNumber of the items I want to delete to a temporary list. After the for each loop is complete I did another for each on the temporary list and use the ListRemove to delete all the intended rows.

However it works for some records while other don't , I can't find a pattern (is not deleting by order), the error is "Index out of bonds. Index 12 for bounds [0,10]". 

The other only solution that I saw was make a copy of the list , delete in the copy and then set the copy as the original but I think it's a really bad practice. 




Rank: #128
Solution

Hi Beatriz,

You'd have to first order that list of rownumbers descending, I guess, and then go through it to make sure you delete from back to front in your original local list.

What you do now, is probably in ascending order, but after the first remove, all positions go one down, so all your following delete will be on a record one after the one you really want to delete, and so on, eventually trying to delete a record that was for example 18 out of 20 initially, but as you already deleted 3 items, there are now only 17 items left, giving you that out of bounds.

Good luck,

Dorine

Hi Beatriz,


Could you put the code or example of the list you want?

You may want to use the listfilter to filter the items you want to delete and then iterate this list for deletion.

Rank: #295

Hi Beatriz,

It would be great if you can share the sample oml for the thing going wrong. 

Thanks 

Rank: #258

Removing records from a list you are looping over is a problem in most programming languages. It's far easier and safer to do the inverse and make a new list with the records you want to keep.

Hi Beatriz Ferreira,

As per my understanding in first loop you are fetching indexes which you want to delete in second loop.

So it will not work because if you deleted first record in second loop it will reset the indexes.


Solution: Do you have any identifier in list if yes then you should identify these identifiers instead of indexes in first loop and you can delete records using these identifiers in second list.


Regards

Shashikant Shukla


mvp_badge
MVP
Rank: #2

Hi Beatriz,

The index of an element is determined by the number of elements that come before: the first element has index 0 (since 0 elements come before), the second element has index 1, etc. However, this is a dynamic number: if you remove an element before another element, all elements after the one removed have now an index that's one lower than before, as one less element comes before it (it's deleted).

Of course, you could compensate for this, e.g. by subtracting the CurrentRowNumber of the second list from the index in the Current of that list (this works because every time you delete an element, all further elements are decreased by one more, so if you're about to remove the third element, the CurrentRowNumber is 2, so if, say, the original index was 11 than it's now at 9 since two elements are deleted).

However, I'd advise you to do it differently: instead of removing elements, you could add the elements that you want to keep to a second list, so that you create a new list with all elements to be kept. When done, you can then assign that list to the original list. Of course, this is only a good strategy if the list you have is fairly small, and/or the size of each element is fairly small, and/or the number of items to keep isn't that large. If you have a very large list, with large elements (e.g. various Entity records) and you need only remove a couple of items, what I proposed above is probably better.

Rank: #5768

Thank you all for the answers, I think all the answers that you gave can solve my problem , I used the solution provided by @Dorine Boudry because it was the simplest and works!