so far we have the ability to edit contacts in our application
in this lesson we will change the Contact Details screen
to allow creating new contacts. Let me just quickly go over
what we want to do in this lesson. The first thing that we want to change
here in the contact detail
is actually the title
of the page because when we want to create a new contact
we cannot show here the name on the contact, it doesn't
exist yet so
we want to show here a different title depending on
if we are creating are editing a contact
the same is to say that when when my contact
ContactId is empty
and the ContactId, remember that it's
this input parameter on the page so when
this input parameter is empty and we'll see how
we're going to test for this when this is
empty in that case, True
I want to show here on the screen
'New Contact' and when it's not
empty so when we pass a value for instance when this ContactId = 1
or two, and so on
I want to show here the name of the contact
and it will be for instance Joana Rivera. Okay so
that's the first change will we're going to do here on the screen is to change
the title
next we will need to check how we're going to
fetch the information from the database in the Preparation Action
remember that we have an Aggregate
that gets this ID, goes into the database
to find the contact there
so that it then shows here in the Form
the information about this contact we will need to
to check if we need to do anything here in preparation
for this to still work. And the final thing will need to do
is actually to look into the Save Action
because when we are editing a contact so in this
case when we are showing Joana Rivera,
actually going to send with the UpdateContact
we're going to send
the changes in Joana Rivera, so for instance the change in the name are you on the
phone number
we're going to send those changes back to the database to stored
when we are creating a contact we can't
use the UpdateContact Action. We will need to use
the Create Contact Action
for that matter. Ok so
this is generally what we're going to do let me just clean up here the drawing
so that we can get these things done. Okay so
let's start by setting the title of the page and to set the title according to
that condition that we saw here the ContactId being
we're going to use this tool here, this is
the UI If. The UI If allows you to show or hide
parts of the user interface based on a condition
that will evaluate to True or False so let's
add the If here to the screen
and you'll see that the If has a True part
which is the top part of the If and it has a False part
what you also notice is that the
If tool has a condition and this is where we're going to write down
that condition that we had here so the
ContacId, our input parameter needs to be empty
since the ContactId, is a Contact Identifier
what we're going to do here is actually to match this ContactId
to test if this value is equal
to NullIdentifier(). And this NullIdentifier is a built-in function
that returns an empty value for an identifier
so this is a special value for you to compare with the
input parameter to test if the input parameter
is Null or is empty so what we said is that
when our ContactId is empty
then we are going to show here in the True part
we're going to show 'New Contact', when it's not
empty we want to show the name of the contact so we already have here this
expression
with the name of the contact, I'm just going to drag and drop it
inside the False part of the If
and that's it for the title so we're done here
now let's go into the Preparation and
remember that we have here
we have an Aggregate that filters
the contacts using the ContactId
what will happen is when this ContactId
is Null, when this contact is empty
then we will have no contacts returned from the database
because let me just show you here the ID
actually has a value in this case Joana is
ID number one we don't have in the database
any contact with an empty identifier because this is a primary key attribute
these attribute needs to be filled in so this means that
when we tried to get a contact with an ID
equals to Null then from the database we will get
no contacts and this means that on the Form here on the screen
we will have an empty form for us
to create a new contact so that's why we don't need to change
anything here in the Preparation in this Aggregate, we only need now to
take care of the Save Action so that we can
create or update the contact
that we have on the screen. So let's go into the Save Action
here and
basically what I'm going to use
the counterpart for the logic
of the UI If which is this If tool here
and let me just rearrange here the nodes
and when this is, let me just put
here the condition as you expect it also has a condition
when our ContacId
is Null, so the same condition we added to the screen
when this is Null then we want to do something here which is
actually to create the contact, when it is
False, then we want to update it
and you will notice that here in the Data tab
on the Contact Entity we actually have
a CreateContact Entity Action
which are going to use in the situation here
and just like the Update action, the Create Action
receives a record as a source parameter
and this will be the record of the form
okay so when our contact is Null
then we will create the contact when it's not Null
then we are going to update it. Now this is one way of
doing this logic but actually there is a simpler way of doing this because
if you'll notice here in the Contact Entity
we have CreateOrUpdate Contact Entity Action
and basically this action let me just get some space here in the flow
these action that I'm going to ad to the flow now
does all this logic that we have here
for you in one shot. So basically what you need to provide to this
action like you did for the Update and Create
Contact actions you need to provide the Source Record:
which we will get it from the Form
that one, and what it does is
when the contact doesn't exist in the database
then it will create the contact it, will insert that contact in a database
when the contact already exists in the database
then it will update that contact. Okay so we can remove all this logic here
right-click and delete these nodes
this is a simpler way of doing all that logic
one final thing we need to do to be able to create contacts, we need to go back
into the Contacts screen and
add a new link here to create a contact so
'New Contact'. And we're going to
link this text to the contact detail
and now we need to fill-in the input parameter and probably
you already guessed that here
we are going to actually passe the NullIdentifier() value
this is the value that we are testing
in the logic that we changed in the Contact Details so
when this ContacId has this value this screen will behave
as creating a new contact, the title will be
'New Contact' and the CreateOrUpdate action will
actually insert that contact in the database
so we're done here. Let's publish and create a contact
okay so here we have let's try to create a new contact for
our phone book and let's call in
John Doe and put here a phone number
and let's save it and let's see
and there we have it - John Doe. The last
contact in our list. That's it.