Structure the Application Logic
Return Values from 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 set up a user action
so that it returns data we are actually going to extend the logic we built in
the previous lesson
to make sure that when we add products to an order
we decrease those products from the stock so
here again in the logic tab we will add
a new user action and this will be
the AddOrderItem and again this
AddOrderItem will receive an input parameter which will be
an order item and
actually let me just go back here into our screen
the logic that we want to implement when we
add a product remember that we navigate here into this
OrderItemDetail we
select the product and the quantity and then here in the Save
action we
get the product calculate the total price here
bind the OrderId to the OrderId attribute of the OrderItem
and then create or update the order item so actually
this is part of the logic this is part of the logic that I want to
add to my user action so let me
let me cut this logic from this
screen action will get back to to this screen action to implement
the rest on the logic and let's paste this logic
inside this AddOrderItem screen
okay
let me just reconnect here these nodes in the flow
and rearrange these
to look better and I have here a couple of errors
let me just guide here
my implementation by looking at the errors that we have
and fixing those so here the first one
we are using
the OrderItemForm so this is because we were using this in a
screen action
and we are using this in the filter condition
of our aggregate so here we will need to fix this
to use the input parameter of our
action so here we want the ProductId
so this is basically this
attribute here okay done let's see the next error
double click here and
and again in the aggregate I have the calculated attribute that uses again
the quantity
from that... from the form that we have in the screen and in this case
we want to use again the input parameter
so let me just select this and replace with the quantity from the input
parameter
and we're done here and there's another error
and this one here is
is in this logic in the assign we were actually
assigning the OrderId to
our OrderItem and remember that on the screen
the OrderId is an input parameter for the OrderDetail screen
so... the OrderItemDetail screen so
we're actually going to receive here for this action
also the OrderId there
And this fixes that error and we still have
here a couple of errors let's see what those are okay so
now we need to change here the assignment we're no longer assigning this
to the form on a screen we're assigning to the input parameter that we receive so the
OrderId in this case and the total price
in this other case there and finally
we need to fix here the information that we're sending to the
to the database it's no longer the form its
the order item that we receive
as input parameter okay so this logic is
fixed now what we want to do is to
update the stock of the product so
let me just get some space here we already fetched the product from the
database
and now we can do a
logic very similar to the one we did in the previous lesson basically
we want to assign to this product that
that we fetched from the database to this product we want to
assign
the quantity in stock and we want to say that the quantity in stock
is equal to what we have there right now so
again the quantity in stock and we want to
remove from the stock so I'll use the minus here
the quantity that we receive
in our OrderItem so this is the quantity of that product that we are
buying
this is the quantity that we want to remove from the stock
okay. so we update the
record we fetched from the database and now we need to update the database so
we will need to use here the UpdateProduct
entity action
there and as a source
we will pass the products that we fetched from the database
done and now
our user action is pretty much done now we need to use this user action back
in the OrderItemDetail screen so here
in the Save action we want to use
the AddOrderItem
and we will need to fill in here this parameters so the OrderItem
will be the record in the form
here and the OrderId
we receive as input parameter of the screen so
here the input parameter OrderId
okay that's it we're done let's publishing and test
okay let's go first into the products
list and let's see that we have for instance here
our 'USB cable' so let's let's our 'USB cable'
to one of our orders let's say this order number five
and add the product
we'er going to select the 'USB cable' and let's
add one 'USB cable' save it
and the product is added to the order let's go back into the product screen
and well oops
it seems that we are allowing that
our stock becomes negative and it is workinh actually it is working we
are the decrementing
the stock of the product but
we don't want to to allow the stock to be negative
so let's go back to our application and let's let's fix this
and to fix this we're actually going to change our user action
and we need to provide
a result for this action
and and we will do this by creating
and output parameter for the OrderItem
that will indicate that we don't have
enough stock
and the output parameters are parameters that can be used after the action is executed
inside the action we can assign
a value to the output parameters as we assigned to
the values of... to the output of the aggregate for instance
and then this value can be used
here in the screen action after we execute
the AddOrderItem action and
in this case let me just fix here the data type
for the parameter because I want this to be a boolean
a boolean output parameter and here
we'll need to change the logic so that
instead of doing all this instead of allowing a product to be added
to an order item and leaving a negative stock
so instead of doing all this I want to
return from this action that there is not enough stock so
let me first check here we will use an If for that
let's check if we have enough stock
to add the product to the order and we will do this by looking into
the quantity that we want to buy needs to be
less or equal than
the quantity of the product we fetched from the database
actually the quantity in stock okay so
when this is true when the quantity that we want to buy is less or equal than
the quantity in stock
then we'll go ahead let me just swap here the connectors
so on the true side we will go ahead and update the stock can create the order
item
when this is not true then I want to
change the output of my action
and say that there is not enough stock this means that
the value of this boolean attribute the boolean
output parameter will be true indicating that
there is not enough stock and we want to end
the action at this point
okay our action is complete now in the web screen where we use the action
here in the save screen action of the
OrderItemDetail after executing the action we need to
test the output to determine if this was successful or not
and this means that if
there was not enough stock
so if this returns true
in that case we want to invalidate the form
and you already seen in the previous lessons that to invalidate the form
we will invalidate the input
by setting the Valid property and the ValidationMessage property
and we will do this for the quantity input that we have here
in the form so here
let's set the Valid property to false
and the ValidationMessage to something like
"There is not enough stock of that product"
done and let's
end the execution with an End node
and we're done let's publish
okay so let's go back to our order
let's remove the 'USB cable' this will
increase the product stock
so now let's just confirm that the 'USB cable' stock is
zero again let's try to add the 'USB cable'
to our order so add product
let's select the 'USB cable'
and we want one of these
save and there you have it there is not enough stock
of that product