331
Views
2
Comments
Outsystems logic flow - For Each + If
Question
Service Studio Version
11.53.32 (Build 61761)

Hello,

I am currently developing an application that uses both the for each loop and if statement in a logic flow. 

The app works in this way: 

1) I have a form that allows me to choose between a few templates: 

2) Each of these templates have their own unique information (PITask entity):

3) The save button in the form in part 1 has this logic flow:

4) The purpose of the For Each loop + If else nodes is to check the PITasksList shown in part 2. So the for loop goes through every single data point in part 2 and check if the PI template ID selected in the form in part 1 matches the PI template ID found in the PITask.List. 

5) So for example, if I select PI-001 in the form and I click the save button, it runs this logic flow and check PITask.List for all data that has PI-001, then it create 4 different rows of data (because there are 4 data with PI-001).


So my question is, this logic flow works only for PI-001, however it does not work for the other different templates (PI-002 etc...). So I am wondering what is causing this issue/am i missing something. Thank you and sorry if my question isnt clear.

2021-09-06 15-09-53
Dorine Boudry
 
MVP

Hi @Low Ding En ,

there is sooo much wrong here, it is hard to start helping.

First of all, if your purpose is to create a number Job orders, depending on the PI template selected, none of that should be happening in your client logic.  What you should have is a server action, and inside that, execute an aggregate on your PI table with as filter the PI Template.  That aggregate would yield your 4 records you are interested in.  Then you can have a ForEach in your server without the need for an If, and just doe the CreateOrUpdateJobOrder for all 4 of them.

Second, if you ever can not use an aggregate to reduce a list based on a filter condition, use a ListFilter and then iterate on the result of that.

Third, the reason this is only working for PI001, is that in your loop, when there is no match, the False branch of the IF just exits the loop, instead of going to the next record to evaluate by pointing back to the ForEach element.  

Fourthn it is an odd location for the Form Valid check, not sure what that is doing there inside that loop, but it means that you validate in the middle of doing stuff, and you risk your logic stopping in the middle, with part of the creates done, and part not.

Dorine

UserImage.jpg
Low Nico

Hello Dorine,

Thank you so much for your suggestions and taking the time to help me with this. Looks like I still have alot to learn (like when to use server action over client action). But I have a clear direction on how to tackle this issue now. Thank you!!

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