Deleting a record in a subtable while loop the subtable records

I was reviewing some logic block actions and noticed that there was a pattern in it that could be alarming:

In the case, it is looping through the records within a subtable (EmailsSub). Inside the loop, based on some boolean condition, it would delete the current record from the subtable. What would be the effect of doing this?

This should work perfectly fine. Whether the subtable is a sub main or sub sub, the list of records is in a set order. Looping through that set of records and deleting some of them will not modify the index of each item in the “list” or cause the looping to end abruptly or continue for too long, if that’s what you’re hinting at: this is the encouraged way to delete records in a subtable within logic (conditional optional, of course).

If your subtable has 5 records, looping and deleting might look something like this:

  1. Run logic for record 1
  2. Run logic for record 2
  3. Run logic for record 3, then delete record 3
  4. Run logic for record 4
  5. Run logic for record 5, then delete record 5

A second loop would find only three records, records 1, 2, and 4.
There should be no issues with re-indexing mid-loop in such a way that deleting record 3 would then loop to the fourth record in the subtable, which is now record 5, thereby ‘skipping’ record 4 - that behavior will not occur.

3 Likes

Excellent! Thanks for the info.

1 Like