logo

NJP

Learn MORE JavaScript on the ServiceNow Platform: Lesson 13 - GlideForm (g_form)

Import · Dec 12, 2023 · video

Okay, Welcome back to learn more JavaScript
on the ServiceNow platform. Behind the scenes, I had to record that
line like eight times. I just kept messing it up. Anyways,
if you are tuning in from the last lesson, we just got out of GlideAjax and now
we're going to be talking about GlideForm. It's commonly referred to
as g underscore form. So g_form. This powerful API,
it allows us to interact with and manipulate forms
within ServiceNow. So if you were here for the GlideAjax
talk, we're manipulating things on the form
without actually accessing the server. So this is all stuff that will be
happening within the user's browser and it's not affecting anything in the
actual server, it's not saving anything, but it's useful because we might want
to change something on the form before we actually talk to the server. So use this API because it provides methods
to customize your form behavior. It does things like set field values
for the user. It can make things read only,
it can hide fields. You can do a lot of things. And we're going to try to show you
how important it is to use this GlideForm method because it's real time
and it's very useful for the user experience
that you might want for your users. Okay. So for example, one of the things that we might want to do
is update this short description to say, add the priority to it so
that it's always in the short description. So it's easy to read from this short
description. That's just an example. To do
glide_form stuff, we're going to do it via the client script,
so I'm going to jump into the client scripts table
and I'm going to say new client script, and I want this one to update the short description with priority and we're going to run this
on the incident table. And I want this to be when the priority field changes and the script down here, we're going to go ahead
and say g_form.setValue(), and we're going to change the short
description. we're going to say the new priority is and we're going to say new value. So on these client scripts, we're already given some fields
that we can use. So we're going to... we can have the old value of what that field was and we can have
the new value of what that field is. So that's what we're using here. I'm going to go ahead
and save this client script, go back to incident,
and let's pick a random incident to open and we'll see here. If I go
down here to show short description, if we change the priority. He noticed the priority changed
and then our client script activated And g form takes control
and changes the short description, because that's what we told it to
do, to the new priority is three and we can actually keep demonstrating
that now says new priority is five and if we put it back to high,
it'll say new priority is one. Notice that at no time
there was a loading icon or we didn't hit
save or update on the record itself. So nothing's actually happening yet
on the server side, this is all still within my browser. This is as if the user has to change
it themselves basically. Until they hit update. Nothing has happened in the server. Now if I go back to my new client script
that I just created, I can show you a couple of other things
that we could be doing. So maybe I don't want to replace
the entire thing with “The new priority is newValue.” What if I want
you just add it to the end of it. So we can say over
here, we're going to say g_form.getValue() of the original short_description. So getValue() is the way
to get information from your form. and we're going to add
that to the beginning of our of our script. Let's go ahead and update that, go back to incident, open our random incident and you'll see here, how do I create a
subfolders occurring, a short description And if I change the priority,
It didn't just wipe out the entire short description,
it took the short description and then took the new information in
and put them together. So that's how you getValue(). and then lastly, if I go back to my client
script and I keep it on when the priority field changes, we can do a little bit
of other things on the form. So just for example, I'm
going to say when the priority changes, we're going to setReadyOnly() to
and I'm picking a random field assignment_group and we're gonna say true
because Is it read only? Yes. and the reverse is true,
if it's an already read only field, you can say false
and it becomes editable at that point. But for this one I know assignment_group is not read only already,
so I'm going to set it to true. And then I'm also going to doSetDisplay(). I'm going to make the impact field
completely disappear. So when the priority field changes, that means the assignment group
should become read only and the entire impact field
should be removed from the user's display. It still exists on the table. It just doesn't show up
for the user anymore. So when go ahead and update that,
go back to my incident, I’ll open a different one this time and we'll see where... So here's the impact field and here... where’s the assignment group field. Here it is. Here's the assignment group field. So if I go ahead and change the priority, you'll notice that impact completely,
completely disappeared as I wanted it to. And assignment group now is read only. So there you go. That's how you can manipulate
the form using scripting, using g_form or GlideForm. Cool.

View original source

https://www.youtube.com/watch?v=9Cn6C-B_1YI