Hi Everyone,
Can we exit from a!forEach() loop?
I have a use case , where I need to iterate through the list of users and once the condition met ( checking for the user's organization and weight category), I have to stop iterating through the rest of the items. I need order to achieve this, I have to exit from the loop.
How can I achieve this ?
Apologies for not clarifying the use case earlier.
Use case is given below.
Use Case: There is a list of Boxers. Each boxers belongs to particular organization and weight category. I want to select 2 boxers for the match randomly. 2 boxers must belong to same weight category but different organization.
To achieve this I was trying to loop through list of boxers and fetch 2 boxers belonging to same weight category but different organization. Once I get 2 different boxers, then I wanted to exit the loop, because I got 2 boxers for the match and don't want to loop further. Note: Weight category and organization are not known initially. That also needs to be picked randomly.
Please do let me know if this is still not clear.
Discussion posts and replies are publicly visible
Hi beenav4597,
I don't think that you can exit the for loop on the specific condition.
What can you do that save the null value when the condition is false and save the value on the true condition and outside the loop you can use the reject function for getting the desire output.
a!localVariables( local!test : { 1,2,3,4 }, reject( isnull(_), a!forEach( items: local!test, expression: if( fv!item = 2, fv!item, null() ) ) ) )
You can use wherecontain() function if it fulfills the condition.
https://docs.appian.com/suite/help/22.3/fnc_array_wherecontains.html#:~:text=When%20to%20use%20wherecontains(),field%20in%20that%20CDT%20array.
a!localVariables( local!test : { 1,2,3,4 }, local!index : wherecontains( 2, local!test ), if( a!isNullOrEmpty( local!index ), "", property( local!test, local!index ) ) )
Hopefully it will help.
Thanks harshjan for the reply. Does that mean I'll have to iterate through all the items in the list even if the condition has met early?
Yes, if you are using for loop, then it will automatically iterate itself.If you can elaborate your use case, then we can find an alternative for that.