Structure the Application Logic
Implement Logic in a Method
This lesson is part of the Developer Online Class for OutSystems 9 course.
LEARN MORE

Transcript
in this lesson we will see how to make our application logic reusable
by removing it from the screen logic and placing it
inside the reusable action we will implement the reusable action to update
the quantity in stock
whenever a product is deleted from an order so
here in the OrderDetail
remember that we have this delete delete action here
to remove the product from an order and this
delete action is implemented in screen action
and what we want to do is to
have a reusable action that actually
deletes the order item and updates the quantity in stock
for that product in the OutSystems Platform the logic is implemented using actions
you can execute business logic in the preparation of the screen
or in screen actions and another way to execute logic
is using user actions and you you will create these
here in the logic tab under this folder
Actions so let's create a user action
and let's call it RemoveOrderItem
a user action allows you to implement business logic that can be
reused
throughout your application so you will be able to use this user action
in every screen you have in your application in every screen action of
that screen and so on
okay since user actions have their own scope
you need to send data through parameters to be able to access it
inside the user action and in this case to remove the order item we will
need to receive here
as an input parameter the OrderItem
that we want to remove now notice that
the data type of this element is actually
the entity OrderItem so far
we only have been creating simple data types like booleans, integers and
identifiers
but it is possible to create variables to store
a whole record in this case an OrderItem
and this OrderItem will have
this definition that we have here so it will have
all these attributes that we can use inside this action to compose the logic
so let's let's do this logic here
to remove the order item what we want to do the first thing we want to do is to
update
the quantity in stock for the product so let me
find here the product entity and create an aggregate
to fetch the products and
in this case we will won't we want to filter
we want to filter this aggregate with
the identifier of the product that we have
in the order item so this attribute here
and we want this identifier to match
the Product Id
okay so
we have the product let's go back here into the logic
now that we have fetched the product from the database we can
change the quantity in stock attribute of that product so we will use an assign
node here
to update that value
and we want to say that the
quantity in stock is equal to
let's set here the expression is
equal to the quantity in stock that we have there at the time
plus the quantity that we have
in the other item that we are going to remove that we are going to delete
so this quantity here so we set
the quantity in stock now we can update the product
let's go here into the Data tab and
update product
this will be the product that we fetched
with the aggregate and now
that we have updated the product we can now safely remove
the order item so here we will use
the DeleteOrderItem
and we need to specify the identifier
and we will get the identifier again from the input parameter it's the Id
attribute of the order item that we received as
parameter
okay our action is is pretty much done
now we want to reuse reuse this user action
in our screens so back here in the OrderDetail
we have the Delete link which goes into...
binds to this screen action to the Delete screen action
and here we have the DeleteOrderItem and I want to replace this, so let
remove I want to replace this DeleteOrderItem
with the action that I have just created so
use here the RemoveOrderItem
now we'll need to specify what OrderItem
it is that I want to remove and this we will get from the Table Records
with the current Property
the OrderItem entity that we have there
This is the OrderItem that you want to remove
okay it seems that we'er done let's publish and
test our application
okay let's select one of the orders that we have in our application
let's say in this one for instance number four
and let's remove
let's remove this OrderItem but before I do this
before I do this let me just let me just go here
and open the products link in a new tab
and let's look into the Logitech
so the quantity in stock is 0
for the Logitech headset and we're going to remove
3 elements we're going to remove three
items of this product from this order so this means that
our quantity in stock will be increased by three elements
let's let's go ahead and do this
there the item was removed from
the order let's go here back into the screen
and let's refresh the list
and there you have it now we have 3 items
of the Logitech headset product and that's it