Structure the Application Logic
Execute a Method over a List of Records
This lesson is part of the Developer Online Class for OutSystems 9 course.

in this lesson we will see how to iterate through a list
to execute logic on each element we are going to implement a delete
order action that will delete all the items associated with the order
and then the order itself so let's go ahead here into the logic tab
and let's create yet another user action
this will be the RemoveOrder
user action and again
we will need to have information in this action
of which Order we want to remove so we'll
have an input parameter that in this case will be
an Order Identifier the first thing we want to do
in this action is actually to fetch
the order items from the database so let's go ahead here into the Data tab
and drag-and-drop the OrderItem to create the aggregate
let's filter these order items
with the OrderId so
add a filter here the OrderId attribute
of the OrderItem must match the input parameter of the
action there you have it so for this order we will have
Let me just show here the hidden attributes
for this order which is number one
we will have these products with these quantities and this price
okay that's done let's go back here to the action
now that we have we have fetched the order items we want to iterate
through this list the return the result
of the aggregate we want to iterate through each one
of these order items and remove them from the order
and to do this we're going to use this tool here
which is the For Each this allows you to iterate over
each element in a list and in this case let me drop it here in the flow
the list that we are going to use is
the result the list property of
the aggregate okay
so now we need to for
each element we need to remove the
order item so we were going to reuse the action we created in previous lesson
and this means that we need to connect the for each with this
action and you'll notice that
this connector here says cycle so this means that
while we have elements in our list
this actions is going to be executed and after this action is
executed we'll go back into the for each
and this creates this loop here
that iterates over each OrderItem that we get
from the aggregate okay this
action here needs an input parameter and the input parameter is actually
the order item that we will get
from the aggregate this is the list that we are iterating
okay so this is the OrderItem that we want this current property here
will point will point to
the element that's currently being iterated
in that list so this is the element that we want to
use here for this action and
and that's done for deleting the order items after we delete the order items
we can now safely let's go here we can now safely
delete the order
and we need to specify the identifier of the order
that we will get from the input parameter of the action
and that's it okay so now we want to use this
action in our screens and we will use the action here in the OrderDetail
and for that we will add
new button here to our form and we will call it
Delete and let's double click to create this screen action for this button
and here in the screen action we're going to use
the RemoveOrder
user action and we will need to pass the OrderId
so this is the input parameter of this screen
and after we delete the Order we redirect the user back
to the Orders screen so here
now let me just rename here the screen action because we already had
a delete to remove the order item from the order
and this will be the DeleteOrder
and let me just do one other thing which is
indicate that this button is only visible
when the order actually exist so when the
OrderId is different
from NullIdentifier()
okay and one other thing that I want to do here is
there's another property here for the button which is
the Confirmation Message when this properties set
the user is prompt to confirm this action
when the button is pressed so we want to make sure that the user
knows that he's deleting an order and we will have here
a confirmation message for that
"Are you sure you want to you delete this order"
and this text will be used to prompt the user
for confirmation of this action
and were done and
all we have to do now is test this let me just add here a breakpoint
let me add a breakpoint here in the screen action so that we can
test this using the debugger
so let's start the debugger you can do it here
and let's publish our application
okay and so let's go into the order screen
and let's open this last order from Tom
and we have a order item here
and now we're going to delete this order
there you have the confirmation message "Are you sure you want to delete this order"
I'm sure and now we're stopped here
in the screen action let's step over
and let's step into this
RemoveOrder we're passing the OrderId which is
number five as we've seen in the screen so let's step
into so that be have
the debugger going through our user action
now we will fetch the order items
from the database let's see the list of order items
there we have we have one order item to remove which is the Mac Book as you've
seen in the screen
now we will cycle through this list
and we are going to remove the order item we can step into here
and the order item that we receive here is this one
we're going to fetch the product
we're going to update the quantity in stocks so we're going to have one product in
there update the product delete the order item
and we're done and we will continue the cycle but
since our lists since our list only has one
element this means that we will no longer enter here
the cycle we will continue to delete the order so
next delete the order and
we're done the user action and finally we're going to
redirect the user back to the orders screen so let's just continue here
and there you have it the order was deleted and if you go to the products
the stock for the Mac Book Air is updated
and we have now one item in stock
and that's it