I am new to Outsystems and having confusion about when we need to use for each loop and when to ad-hoc ?
What is main differnce between For Each and ad-hoc loop ?
ForEach need a list to loop through each item in the list.
ad-hoc loop just like a normal for each, which you could specific how many times it should loop and do the action.
Sample 1:
Order Item Attributes : Id, OrderId, ProductId, Quantity
You have a list fetch from the aggregate, let's say an Order Item List which belong to an order, and you want to know the total sum of this order, you could use ForEach to loop through each Order Item, and do the calculation which sum the Product.Price * Quantity.
Sample 2:
You have a number given which is N=10
And you need to know the sum from 1 to N, then you need to use the ad-hoc loop by using If(), and calculate the sum, result = result + N.
Hi Bilal,
The main difference is, In for each loop you are traversing items of List. It will go through each item one by one.
But In ad hock loop you will run loop using If condition. Loop terminates when Condition fails.
Hope this help.
Hi Muhammed,
In addition to what has already been said, if you use a For Each loop with the output of an Aggregate, and you do not use the Aggregate as output, or in a ListAppendAll etc., the Aggregate will use a database cursor, which means that each iteration a record is fetched. This is an optimization that speeds things up, but also means that when debugging you can only see the Current record.
Another difference: when looping over a list, you cannot modify that list itself (though you can modify the records the list holds), so no ListAppend, ListDelete etc. That is because a For Each is done (if it's not a database cursor like I described above) with an iterator, and you cannot modify a list that's being iterated.
Also, ad-hoc loops need some index variable, and if you are looping over a list you need to index all uses of that list, which is error prone and gets laborious quickly. So my advice is to always use a For Each, unless you really want to do something that makes it impossible (like deleting records from the list you're looping over, or inserting records into it).
EDIT: Just thought of another difference: you can only exit a For Each when all records have been looped over, or when you exit the entire action. It's not possible to "break" out of a For Each. So if you need to stop processing as soon as you reach a certain record, you may also consider using an ad-hoc loop (the alternative is setting some "skip" boolean that you test each iteration to see if further processing is needed, but this will still iterate over the entire list, which may be slow if it's a long list).
Hi Muhammed Bilal, For Each:
Ad-hoc Loop:
For Each Example:
Ad-hoc Loop Example:
This way, For Each is easy and automatic. Ad-hoc is for when you want more control.
Summary:
Hi Cemal,
You replied to a post that's 1,5 years old. Also, your answer has already been given in the other replies. Please check the date of the original question and any answers when replying to a post, thanks!