Reverse Iteration
Question

Can I do reverse iteration in Outsystems like in for-loop?

for eg:

for(i=10; i>=1; i--)

{

...

}

Need to iterate the list from the end.

Hi,

Indeed you can do that. Since there is no "while" widget in OutSystems, you can do an iteration by using If. With each iteration, you can check the current value of i, and assign a new value to i.

Please check the attached oml.

ReverseIteration.oml

mvp_badge
MVP

Really like to understand why this reply is down voted. It is good answer and should be marked as solution, not being downvoted.

mvp_badge
MVP

I'm not the downvoter, and while the answer is definitely correct, I think it would be interesting if Rifaz could share why he needs to start from the end of the List - it's possible that sorting through the List before iterating through it would be sufficient, discounting for performance.

While there are cases where the standard For logic isn't applicable, they're rare and the If-based loop logic leaves the code a little harder to read when compared with the more standard For loops. I like to keep them as last resorts and only use them if I can't structure my logic in any other way.

Rifaz, would you mind sharing a little more of the logic you're building?

mvp_badge
MVP

Agree, a solution (potentially less performant but easier to maintain) could be to sort the list first, so that the For Each tool could be used instead of using the If construction.

mvp_badge
MVP

I’m not the downvoter either, but the solution presented does not address the For-Each requirement of the OP (although it does answer the example given): the .List.Current record won’t be changed for every iteration when using an If, so you would need to resort to indexing the list explicitly which wasn’t mentioned at all and has an impact on performance as it won’t make use of the database’s cursor.

I’d agree a more readable (and less error prone) alternative would be to simply sort the list in reverse order at the database level (only if it is the result of an aggregate or SQL) or reverse the list using ListSort with the opposite direction if possible… but for that you need the list to be sorted to begin with.

Thank you for your reply.

Yes Sorted and iterated It is working. But I just wanted to know if there is a reverse iteration is available in Outsystems without sorting.

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