logo

NJP

Learn MORE JavaScript on the ServiceNow Platform: Lesson 8 - GlideSystem (more than just gs.info!)

Import · Dec 12, 2023 · video

Hello, ServiceNow scripters. Welcome back to learn more JavaScript
on the ServiceNow platform on the next batch of videos,
you're on your home stretch of videos. All the videos from now to the end
are going to be pertaining to all things Glide. So we have so many different libraries
within ServiceNow. For JavaScript, GlideRecord is something
that Chuck has already talked to you all about. We're going to be doing
a lot more different things around Glide, and we're going to go through them
one at a time. So the first one we're going to look at is
GlideSystem. So you're all pretty used to seeing this gs.info() and GlideSystem,
the gs part of it is so much more
than just logging statements. So we're going to look more further
in this video into GlideSystem. So the GlideSystem API, often
referred to as gs, this API is pretty
much like the backbone of server side scripting in ServiceNow and it offers
a lot more than just logging. It has methods to interact
with the ServiceNow platform from things like user session management
to querying records. So the first section of GlideSystem
that we're going to look at is user information. So user information is pretty helpful. One of the very easiest things
that we can do is something like what is the current user name of the user? And we'll say gs.getUserName()
and go ahead and run that script and you'll see that
my username for this account is admin. Pretty straightforward. That should be the same thing as you to
if you logged in as admin. Another thing that we can do is stuff like get the user roles or get their ID,
but before we do that we have to dive further into GlideSystem
with the specific user object. And so that can be done
by saying let's create a new variable called currentUser
and we're going to say gs.getUser(). So now GlideSystem is saying, Hey, we're trying to find out more information
about the logged in user right now. Go ahead and get that information
and store it in their variable. So we're going to store it
in that currentUser variable. Now that we have
that we can do some fun stuff like what's the current user ID of the person. So when I say user ID,
I mean the unique ID for that person inside of the database .getId() If we run that, there's
my unique sys_id for my user account. And then other things that we can do
is find out all the user roles and currentUser.getRoles(). That's right. .getRoles(). So if I run that script,
it's going to be a doozy. It's going to get all the roles
that my account has, and it's a lot because I'm admin, so it's
going to grab a lot of different roles. But from there I have a nice list of whoever is logged in's roles
so that I can use it for my scripting. It already has
put it into an array for me. So if I run a for loop through this array,
I can actually get very individualized
with that long list of roles. All right. Next. So in GS, our GlideSystem,
we actually have a lot of information that we can get about our instance. So let's take a look at that. So say
I want to know the name of my instance so that we can refer to it inside
our logging statements or we can put it in our emails
or we can build URLs with it, all we want to do
is gs and use .getProperty(). So get property is a very interesting
method within GlideSystem. What it's doing is, there is a system
properties table and if I show you, you can type go into your filter navigator
and type in system properties. You'll see that there is a list of system
properties that you can go to to set any number of things, and it's all the name of a property
and the value of the property. That's the entire list of system
properties. And so when we say get property, we're
going to say get a specific property. And in this example, instance_name and
tell us what the value is set for that. And this instance_name property is default
for all of our instances, we have that instance_name property
in all of our instances and we can rely on it being accurate
whenever we run the script. So if I run the script there it is, there's my personal developer instance instance_name dev145418
and it was able to get it via a system property. Likewise, maybe I don't want
just the instance name, I want the entire URL for my instance, the URL for this instance is we're going to do getProperty() again. And this one is it's called glide.servelet.uri. And if I run that,
you'll see that there's my entire my entire URL for my instance, it's very convenient way
to get the instance URL instead of having to build it yourself
whenever you for example, build a link and you want someone
to be able to get to your instance, say you might put it in an email. And so that's a really simple
one line code for you
to be able to get your instance URL. And even from there I can actually start
adding to it if I want to link them to specific places, like maybe
I want to send them to the service portal. So if I put plus ‘sp’ at the very end, you'll see that
my URL that I'm providing now says my instance
and then slash sp which is service portal. So just a really cool way, very fast way to build your links for your scripts. And then finally, I just want to show you some random silly
things that shows that there's a lot of things within GlideSystem
and sometimes you might just need to look up the documentation for GS
and see what's available to you. But just to get your brain moving
there is stuff like what's the beginning of last week or what's the beginning of next month? Let's run that and you'll see that as of today, a week ago was December 4th and the start of next month
is January 1st. So just interesting shortcuts
that you have available to you. Another fun thing
is gs.info(gs.isLoggedIn()), So .isLoggedIn(). So if I press run, yeah, I'm logged in. True. Why would I need this? Well, this script can be run
in a lot of different places. Maybe this script was running for a user,
so yeah, they're logged in. But what if you need to
run a script only if it's not based off of a user
session, but it's based off of the system. So maybe that script is running
just on a random flow that's not attached to a user,
then it'll show false. Now you know that
this script is running for no particular person,
and so some of those methods that are inside
GlideSystem can be pretty helpful depending on your context
and what you're trying to run. So again, look it up. You might, you'll be surprised by all the fun little
shortcuts that you have available to you. But I just wanted to show you some user
based ones, some instance based ones,
and then just some random fun ones. Cool. I'll see you in the next video and I got to make sure
my mike is on this time. oh my goodness!

View original source

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