Deleting within a loop


Author
Message
Thomas Holste
Thomas Holste
StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)
Group: StrataFrame Users
Posts: 144, Visits: 618
Hi there,

wether I am deleting records within a do .. Loop while bo.movenext Loop or within a

For each enumerable construct, not all records which fulfill a condition to be deleted, are deleted.

As I read some other posts, I already checked that there are no filters or sorts active.

What might be the reason for this? Or what should I do instead?

Thanks in advance

Thomas
Larry Caylor
Larry Caylor
StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)StrataFrame VIP (1.2K reputation)
Group: Awaiting Activation
Posts: 592, Visits: 3.7K
Try looping backwards. The following assumes you are only going to mark the records
as deleted and you are going to do a save when you're done to do the actual delete/s.

If Not businessObject.IsEmpty Then
     businessObject.MoveLast()
    
Do
       If <Some logic to determine you should delete the current row> Then
        
businessObject.DeleteCurrentRow(True)
       
End If
    
Loop While businessObject.MovePrevious
     businessObject.save()

End If




        



Thomas Holste
Thomas Holste
StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)
Group: StrataFrame Users
Posts: 144, Visits: 618
Hi Larry,

thanks for your help. It seems to work and I will test it a little bit more. But I still wonder why the other way does not work.

Best regards

Thomas
StrataFrame Team
S
StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)StrataFrame Developer (4.2K reputation)
Group: StrataFrame Developers
Posts: 3K, Visits: 2.5K
Larry's solution will work.

The reason that cycling forward will not work is that the rows are removed from the business object when they are deleted.  When you cycle forward through the records and move the CurrentRowIndex forward, it will skip over items like this example:

First Step:

Row1 <- CurrentRowIndex = 0 <- This one is deleted
Row2
Row3
Row4
Row5 <- Count = 5

Second step:

Row2 
Row3 <- CurrentRowIndex = 1 <- This one is deleted
Row4
Row5 <- Count = 4

So, on each step, the current row index is advanced, but it skips an item, because all of the items are shifted up at the same time, because you just deleted one of them from the list.

There's basically 3 options:
  1. Larry's solution of working backwards.  The current row index shifts the same direction as the items being deleted.
  2. Set ShowDeletedRows = True on the business object before you loop through them.  This is probably the easiest because you don't have to change your loop logic.  All of the records are still visible in the BO after they are deleted, but are flagged as deleted for the save.  This is only logical if you're marking the records as deleted to do a bo.Save() at the end.
  3. Loop while Count > 0 like this:
While bo.Count > 0 '-- or Not bo.IsEmpty
    bo.DeleteCurrentRow() '-- Each time you delete a row, the rows will shift up so the next row to delete is always CurrentRowIndex = 0
End While
Thomas Holste
Thomas Holste
StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)StrataFrame User (360 reputation)
Group: StrataFrame Users
Posts: 144, Visits: 618
Hi Ben,

thank you für making that clear.

Best regards

Thomas
GO

Merge Selected

Merge into selected topic...



Merge into merge target...



Merge into a specific topic ID...




Similar Topics

Reading This Topic

Login

Explore
Messages
Mentions
Search