Nested for each Loop on single list

I have a for each loop which is iterated to see if a list item is marked for delete operation. When a marked list item found it is deleted.

Now, I would like to add validation to see if atleast one item is marked if not to throw a validation message.

For this, I have added two for each loops on the same Table list which in runtime giving me an exception "Detected a recursive iteration over the same list."

Please find attached my .oml file; in here DeleteSelected is the screen action that has the issue.

Please help.

Hello Siva Kumar Bommi,

If you are already iterating over list A (ContactTable.List in this case), you cannot then again iterate over list A until the first for each is complete.

If you want to show a message if none are selected, or not show one if they selected one, you have a few options.  One option would be to have a HasSelections boolean variable that is false before the loop.  If an option is checked in the loop, assign HasSelections to True.  Then after the for each is complete, check the value of HasSelections to show a message or not.

Does that help?

Hello Craig, 

Sure, I think that helps, but i was trying to see if there is any way to allow early exit of the for each loop (before processing all the records once the boolean variable is set to true.

Please suggest. 

Thank you.

Hi 

you can not iterate ContactTable.List a second time within the for each. 

Duplicate the list and iterate by the duplicated list instead. 

Br

Domingues, that will avoid the error but not solve what Siva is trying to do.

Siva Kumar Bommi wrote:

Hello Craig, 

Sure, I think that helps, but i was trying to see if there is any way to allow early exit of the for each loop (before processing all the records once the boolean variable is set to true.

Please suggest. 

Thank you.

No, OutSystems does not have the equivalent of a break.  This is normally fine though as if you are just iterating over records from a list, there should not be so many that completing the iterations impacts performance.  Are you seeing a case that differs?


Hi Siva,

Your approach has different problems.

Besides the ones already pointed to you, the message asking for confirmation will not be triggered until the end of the action. This is not how a request to the server from the browser works. 

So, if you want to check if something is selected and in this case ask for CONFIRMATION, you can do things differently.

1. Without confirmation after the action is called and assuming only one item can be deleted, you can iterate a single time, like this:

If you want to check if something is selected and in this case ask for confirmation, in case you can have a single item selected, I would use a different approach.
Options:
a) Use a page local variable (boolean) to store if there is something selected or not. Use the OnChange of the checkbox or radio button to update it accordingly. Every time you set it to true, refresh the button to delete to contain a confirmation message.

b) Call an action to check and case you find something to delete, execute a small javascript to click on a hidden link in the page that has the confirmation message that calls the action to delete the items.

Probably there are other ways of doing the same thing.

Cheers

Craig St.Jean wrote:

Siva Kumar Bommi wrote:

Hello Craig, 

Sure, I think that helps, but i was trying to see if there is any way to allow early exit of the for each loop (before processing all the records once the boolean variable is set to true.

Please suggest. 

Thank you.

No, OutSystems does not have the equivalent of a break.  This is normally fine though as if you are just iterating over records from a list, there should not be so many that completing the iterations impacts performance.  Are you seeing a case that differs?


Not right now Craig, as I just learning outsystems and this my sample application. And thank you for your suggestion, I implemented and it worked. Just wanted to show you my code.

Craig St.Jean wrote:

Siva Kumar Bommi wrote:

Hello Craig, 

Sure, I think that helps, but i was trying to see if there is any way to allow early exit of the for each loop (before processing all the records once the boolean variable is set to true.

Please suggest. 

Thank you.

No, OutSystems does not have the equivalent of a break.  This is normally fine though as if you are just iterating over records from a list, there should not be so many that completing the iterations impacts performance.  Are you seeing a case that differs?


That is not also entirely true. You can break the cycle as well.

This pattern works.

Br

Domingues wrote:

That is not also entirely true. You can break the cycle as well.

That is equivalent to a traditional return, not a break.  Break would imply that you are still continuing with the flow of the action.  That said, you are correct you can end a for each with an End.

@Siva,

Notice that your code has a Feedback Message asking for confirmation that the user will never see (potentially) and will have no effect at all, as it will be returned only at the end of the execution and it is just something that is displayed in the screen, not asking for user confirmation.

Cheers.

Eduardo Jauch wrote:

@Siva,

Notice that your code has a Feedback Message asking for confirmation that the user will never see (potentially) and will have no effect at all, as it will be returned only at the end of the execution and it is just something that is displayed in the screen, not asking for user confirmation.

Cheers.

Hi Eduardo, 

I got it, i have been testing my app for sometime and i get that message on few times but not always. Please suggest me an alternative. Thank you.


Hi,

You can get the message sometimes, but it will not allow you to do anything like "confirm", as it is just a feedback message, not a confirmation dialog window.

I gave you two suggestions before on how to work if you want confirmation before deleting :)

Cheers.