logo

NJP

Learn MORE JavaScript on the ServiceNow Platform: Lesson 12 - GlideAjax

Import · Dec 12, 2023 · video

Hello, ServiceNow friends, builders,
developers, admins, everybody. Welcome back to learn more JavaScript
on the ServiceNow platform. Today we're going to do a deep dive
into GlideAjax. So GlideAjax is the bridge
for client server communication. It allows your client scripts
to talk to your server side scripts and get information from the server when you don't have that connection
going on at the moment. That sounds like a lot and you might not have known what anything
I just talked about and that's okay. I'm going to try to explain it to you and illustrate
some of that for you in this lesson. So it's not one of the easiest concepts
to get inside ServiceNow if you're brand new to things. But it's very important because it comes up a lot
when you start building your apps. And let me kind of show you
what I'm talking about. So if I'm navigating a website,
not just ServiceNow, any website, when you're clicking around
and you click on a link. So when I click on this incident link, I'm
asking the server that has all this information
to give me information to show and display on my screen
that is interacting with their server. But now that the web page is loaded, I'm no
longer interacting with that server. I can edit all these different fields
and nothing is actually happening on the server
until I press update or any other action that would say, Hey,
it's time to update the server. So even though I have changed
some of these values, it doesn't look like that in the server. It's still in my browser and then it doesn't do anything until I call the server again with clicking on
another button like update. so, what we're going to learn with
GlideAjax today is say that we want something to happen
based off of information that we have from the server,
but after the page has already loaded. So even though by normal behavior, if I change impact to low
and nothing happens on the server, what if I do want something to happen
from the server, whether something updates on the server
or I just get information that I don't have on the screen now
to display to the user. So it's a way to communicate to the server
after we've already hung up the phone. We're going to go to this place
called Client Scripts, and we're going to create
a new client script. So I'm on the client script table now,
and I'm going to say new client script. And in this client script, I'm going to name it, we're going to just say test client script. Just for the sake of this exercise, and I'm going to run it
on the incident table because I want it to happen
when I'm on the incident table and we're going to do it whenever over the a certain field changes. And just like we were talking earlier, we're going to say
when the priority changes, we're going to do something
with the script. So And honestly, for a lot of people using
GlideAjax, you're honestly going to go look up a GlideAjax template
and copy and paste it. But for this lesson, I'm going to go ahead
and type it out for you. Ee're going to go ahead
and create a new GlideAjax. We’ll say new GlideAjax,
and we're going to call it my server code,
and we'll come back to this later. But this is the going to be the server
side script that we create later, and we're going to say, Add a parameter and we're going to call that parameter,
sysparm_name. And that's kind of like standard of how people name
their GlideAjax parameters. They start with sysparm,
and then this one’s specific, the sysparm_name is looking for a specific
method within your server side script and we'll get there again, a lot of
this is like I'm talking in circles. So you'll see when we get to the end,
we're going to look for the method
specifically called getServerTime. All right. So how GlideAjax works is once
you have parameters, you're going to say this thing called
get XML and we're going to name a function that we need
that we want it to run. Once it gets that payload, once it gets the answer
to whatever we're calling right now, we're going to create a function called handleResponse,
the one that we're calling earlier. And we're going to take that response and we are going to say the server time
(what we're looking for) is the response, the responseXML. Stay with me. It's a lot. I know. Then we're going to say
the document element and we're going to say get attribute, answer. And with that we're going to say alert. This is what happens
when we actually get that information. Server time. The server time is and the server time, the variable
we just created, that's a lot. Okay. Let's let's talk about it. What is
this monstrosity response.responseXML.documentElement.getAttribute(”answer”). Honestly, don't worry about it. So much when you're starting off. Honestly, a lot of us advanced users
don't worry about it so much. It's just how GldieAjax was built
to be able to handle the different information
passing from server to client side outside of your normal
clicking around the interface. It's doing this thing in the background, taking the information that you gave it
you the information that was coming from the server, the packages into this thing
called an XML response and XML is read by certain parameters
and methods itself. So all you have to do is honestly copy
and paste this long string into your script and just debug it
and test it and make sure it works. So we're going to go ahead
and do that now. We're going to go ahead and submit this
so it saves and then we're going to move over
to the server side script. Inside of my instance
I have script includes and we're going to go ahead
and go to script includes now and we're going to create a new script
include So this script include So a script include. It's kind of like a new library of code
that I want to reuse in the future. And for this instance, I'm going to be using this code for my GlideAjax. And remember earlier
we called our script include MyServerCode. So we're going to do the same thing here. The name of this is MyServerCode and we're
going to make sure it's client callable because it's coming from a client And so inside here, I'm going to go ahead and create a new method called what did I call earlier, get server time, very straightforward function. We're just going to get the the DateTime
according to the server, and we're going to build a new method
in here and we're going to say return
new GlideDateTime and getDisplayValue(). All right. So really simple. You'll notice that I'm returning something
because if it's if we're calling this function from a client mean
to return something to it. So I'm returning what I want
in that return statement. So that looks pretty straightforward. I'm going to go ahead and save this now and it's going to ask me user role for access control on this one,
and I'm just going to give it admin for the sake of testing this. Okay, So now we have created the client side of the script and now we've created
the server side of the script. Let's go ahead and test it. Let's go back to incident,
because that's where we asked it to run. And I'm going to open up in random
incident. okay, so now in the incident table,
the way you change the priority field is by changing the impact or urgency,
in case you didn't know that. So if I want to change the priority,
I'm going to change the impact. And it changed to moderate
and you'll see that it changed. But the moment that the priority changed,
it triggered my client script that was waiting
for something to change like that. And then in that script
it called the server side script and that server side script
got the information that I needed. The server time, went back to the client
script and then displayed it in an alert. Goodness, that was a lot of stuff. All you need to remember
is that a GlideAjax is a way for your web browser or the user's web browser
to interact with the server when it's not already
talking to the server. That's the main thing you need to know. And GlideAjax looks intimidating at first, but a lot of us still copy and paste it
from other people's examples all the time. So it even me, I will look up my own old versions of GlideAjax and copy
and paste them into new versions because I haven't memorized that long
strings of code and stuff like that. So it's okay. But now you're exposed to GlideAjax
and honestly you'll probably use it. So see you next time.

View original source

https://www.youtube.com/watch?v=VpLgJoSlprM