Learn MORE JavaScript on the ServiceNow Platform: Lesson 14 - GlideQuery
Welcome back, everybody. You made it to the very last
lesson of the Learn More JavaScript
on the ServiceNow Platform series. This last lesson we're going to be talking
about GlideQuery. GlideQuery is a modern way to query
your ServiceNow database. GlideRecord is our older version, but GlideQuery is a newer addition
to the platform that offers a more readable
and a more maintainable way to handle those kind of database
operations. GlideQuery is designed
to make your server side scripting cleaner more efficient,
and it's built on top of GlideRecord but it provides a much more fluent API
for querying tables, which makes your code easier to read
and to understand and to write, actually. So let's go ahead and jump right in
and then we'll kind of make it a little bit more complex every time
we run through these GlideQuery queries. So some basic usage
if you want to start a GlideQuery, we're going to say new GlideQuery
and we're going to say what table that we want to run it on,
and then we're going to add a method on each line to keep it readable. But every method that we add
will just add a dot. And then what we're doing on each one
and I'll show you what I mean. So on this incident, GlideQuery, I want it to be where all of our active is true. So only active incidents. And I'm going to add another parameter. I'm going to say where the priority is. So let's say one high priority,
and then we're going to say, what fields do
we want to use in this script entirely? So this is one of the things that is
different than our simple GlideRecord. We're actually explicitly saying which
fields we want to be using in the script so that it saves time that it makes it run
faster, and it's much more obvious when we're writing it what fields
that we want to actually be utilizing. And so from there I'm going to say forEach() record that you find
we're going to run this piece of code, but we're going to say info and we're going to say this incident, the incident number. And so this part right here
incident is coming from this parameter which is being populated
by the GlideQuery itself. So each record that it finds is
being placed into this incident variable. So incident number and I
could have named it anything I want to, but I named it incident to make it easier
to read. The incident number
because I did ask for it and I'm going to say colon and then incident short description. So pretty straightforward script. And then I'm going to go ahead
and close this off. So make sure you match all of your brackets inside so that one matches to this one. And then this one matches
that one and we're done. All right. And this ends the entire GlideQuery. Notice that this is all one statement. This could have all been on one line. But to make it readable,
we can put on new lines for each method that we're calling. So let me run the script. it worked first try! Yay I typed it in correctly, my first try. So I usually use a better script
editor for this. So typing it in plain
text was an adventure. But here it is for every incident. I probably have added a space there,
but it's working as intended. Here is all of my active high
priority incidents and the incident number
and the short description. So some basic usage of GlideQuery. Okay, Let's spice it up a little bit. Let's make it
a little bit more challenging. So just like in GlideRecord, we can make
our parameters a little bit more complex. So this filter where priority is one,
we can actually say when priority is greater than two, for instance, next
we can also say we want to order the list of records by sys_created_on,
So by the creation date, we can also say we only want ten. Don't give us any more than ten. We can also change up what fields we want to work with. And then in our for each,
we're going to fix that space
that we did earlier, that space in there, and then we're going to change it
to sys_created_on here because we're not using
short description anymore. Let's say was created on the create date. All right, let's run it. There you go, so incident was created on 2015 11 2. And you'll notice that it is in order
because we ordered it and this is ten. Yup, ten. So we were able to order it by the date and we were able to limit it
to just ten records. And if we go and drill in to see what the priority was for each of these,
they should all be greater than two. Perfect.
All right, so let's go back a little bit. Let's say we want to do updates via
a GlideQuery. So the syntax for updating
is very similar. So this time I'm going to go ahead
and store my GlideQuery in a variable this time,
which is a standard practice. So new GlideQuery on sys_user and where active is false. So we
want to grab all of our *in*active users and we'll say for this example,
if their last name is Duque, and then we're going to say
update multiple. And so this is the syntax
that you want to use. If you want to update records
using GlideQuery. And then inside of it,
we want to give it a object of parameters. So here's our object and we want to say turn all of those records back on
Active is true. So if we were to run this, nothing,
I didn't print anything, but it did run. No errors have popped up. So if there was any user on the table
that was not active and their last name was Duque
then now they're active again. So that's the syntax that
you want to use for that. Similarly, to add new records, we're going to say
we'll call this variable fred and we're going to say, new GlideQuery
on the sys_user table And then we're going to say dot insert similarly to obtain multiple, we're going to give it
a object of parameters. So first name is Fred and we’ll say the last name is Luddy, our founder of ServiceNow. And so these variables right here,
first name and last name, they match fields that exists on sys_user. So that means, hey, I want to create
a new record on the sys_user table and the first name field
should be set to Fred and the last name
field should be set to Luddy and so if I were to run this,
it would insert it. But what if I want to keep working
on this object, on this GlideQuery? Start updating more fields on
and stuff like that we can say at the very end, get(). And so what that means is
go ahead and create the record, but then store that record inside of my Fred variable
and then now I can work on it further. So if I run the script,
you'll actually see that that insert of that
new record has happened. And if I go to my user table,
there's a new record called a Fred Luddy. There you go. My new user is Fred Luddy. And it's probably a good idea
that I did .get() at the end because there's a lot of other fields I need to update here for it
to be a good record. and, and there you go. That's how you do
update or insert using GlideQuery. Cool. There's one more exercise yet
before we end, but we'll see you on the next one. Bye.
https://www.youtube.com/watch?v=a-5i1Wf46bo