Practice Exams:

ServiceNow CAD – GlideSystem Part 2

  1. Demo: GlideSystem

Hello and welcome back. In this video, we’ll actually jump into service now and start writing some code that leverages the Glidesystem API. We’ll start by quickly going over the GS print method. Again, we should be familiar with this method. It takes a string and simply prints it to the screen. It’s very similar to a console log you would see in the browser. So for this, we’ll actually create a variable hello text and assign it a string value of Hello World where we will then insert that variable within the print method.

We’ll go ahead and copy the code, paste it in a background script, and when we run it, we can see that we have Hello World here. Now let’s take a look at the log method. The Glide system log method accepts two arguments. The first being the string that will be written to the log and the second is the source. If we provide a unique identifier and the second argument the source, then when we go to the logs within ServiceNow, we can provide this source value and only see logs with this specific source. Again, this source is just a string value and it can be anything we would like it to be. So we’ll go ahead and log the message.

This is a log message and we’ll provide a source of marks underscore logs. We’ll go ahead and copy that and run it in the background script. And now if we navigate to script log statements under System log, this will take us to all log statements where the source is script. But we’ll go ahead and change this and insert mark’s logs here and we can see our message here. It’s important to note that we didn’t have to provide a second argument here. If we didn’t, we would have seen this message within that first script log statement list view we were looking at. However, that’s used for a lot of things within service now.

So if we only want to see what we just logged, I recommend using a source as a second argument as well. All right, now let’s take a look at the Glidesystem Air method. The Air method is almost identical to the log method, but instead of going to the script log statements, it goes to the Air’s log. And finally we can show the Glide system warn method, which will show up in the warnings.

Typically, I only use the log method when logging and debugging scripts, but in more complex custom applications, using the error and warn may be beneficial as well because it will give you a more specific form of log. Now let’s take a look at the Add error message and Add info message methods. Both of these methods accept a string argument that will be logged to the screen.

So if we simply try to run this in a background script, we can see that we aren’t getting the pretty message that is typically shown to the user since we are executing this in the background script environment. So in order to better demo this for you, let’s go to a business rule on the incident table. And I’ve created this add all messages business rule. And it’s a simple business rule, it’ll just run on any incident update. So if we navigate to the advanced tab, we can see that I’ve populated it with the add info message as well as the add error message and we’re simply logging welcome to service now 201 Development. This is an info message and this is an error message.

And again, this runs on any incident update. So we’ll go ahead and go back into that specific incident. We will update the short description, save it, and we can see that we are getting the add error message and add info message within the incident form view. All right, now let’s quickly demo the beginning of last month method.

So we’ll do a GS print and we’ll pop in GS beginning of last month. If we copy and paste this, we will see that this returns the first day within the month of April. Now let’s take a look at the Generate Guid method. So we’ll go ahead and print this one out as well, and we can see we get a 32 digit unique Identifier. This is used as the SysID in service now. Now let’s demo the Git message method. So before we run the script, we’ll take a look at the Sys UI messages table and we’ll first find a record we would like to grab. So let’s say for the purposes of this demo, we would like to grab the word ago. However, we would like to grab this in German.

So the key is the English word in this case ago. All right, now if we want to grab the German word for a go, we’ll navigate back to the background scripts, we’ll click the gear icon and actually change my session language to German. And now when I run this script, the system will use the argument Ago as the key and then my current session language as another argument in the background and provide us with the German word for ago. Now, let’s take a look at the Git property method. Before we run some code, let’s actually take a look at the sysproperties table and we’ll actually create a new system property. We’ll provide a name of ServiceNow 201 Hello World. And we’ll leave it as a type of string and we’ll put a value of World exclamation.

We’ll go ahead and copy that name, submit it, and now if we go back to our script, we will do a GS print hello and then we’ll add the GS Git property passing in the property name. And when we copy this and paste this in a background script, we should get Hello World. All right, and since we’re looking at Git property, let’s go ahead and take a look at the glide system set property method. So we’ll keep our log of the git property, but just before it we’re going to go ahead and set the property of our ServiceNow 201 hello world property and we’ll go ahead and set that to testing.

So now it should say hello testing. So first we’ll go ahead and set the property and now when we run the git property log we should see hello testing. All right, now let’s take a look at the get user method. We’ll go ahead and Loggs get user and we see we get a string back that is used within service now. Now if we wanted to get the display name of the current user, we can chain on the get display name method to the get user method and we’ll get back Mark Miller.

So you can think of get user as actually returning an object that we can then call methods on. We can also use the git first name method which will return the first name as well as the get location method. Now we can see since location is actually a referenced field on the user table, we get the SysID back. So this is the SysID of the location that is stored within my user record. Now let’s try the get user roles method. This returns an array of all of the roles assigned to my user account.

There’s also a get user ID method we can call to grab the SysID of my user record. The get user ID method can be used within queries as well. So for example, if we wanted to query the incident table and find all incidents where the caller is myself, I can use add query, provide the field caller and then use GS get user ID which will pass in my SysID. So you can see here that I’ve opened up quite a number of incidents. And real quick, if I print the encoded query using the get encoded query method, we can see that our query is call or equals the SysID of my user record.

So that is working properly. Now let’s take a look at the has roll method. This method accepts one argument which is a string value of the role. So we can perform the following logic if the user has the ITIL role or the admin role, then print a message. Now let’s quickly take a look at the get session method which will return the session ID that is used within service. Now we can also train the is logged in method on the git session method which will return a boolean value if the user is logged in or not. Now let’s take a look at the Nil method.

So as we went over before, the Nil method will test whether a specific variable contains a false value. And the variable doesn’t have to be a string, it can also be an object. So let’s go ahead and query the incident table and let’s say for every incident that does not contain a short description. So if the short description is nil then we would like to log a message saying this incident has no short description.

So we can see here that we have a number of incidents without short descriptions. And just to verify we’ll copy one of the numbers, go to the incident table and we can see here that there is in fact no short description. Now let’s take a look at the table exists method. So we should be able to guess what this does. This will test if the argument is actually a table within the system. So if we provide a table name of incident it’ll find whether or not incident is a table within service. Now so we can see that we get true back.

And to verify this we’ll go ahead and pass in some string value that we know is not a table and we can see we get false back here. Now let’s take a look at the XML to JSON method. So for the purposes of this demo, I have an XML string variable which contains a string of XML. We can see we have a root node and then a test node with the value some XML and a test one node with some more XML. So what we want to do is turn this into JSON and we can see on line two we’re using the value returned from XML to JSON method and we’re assigning that to the JSON variable.

So on line three we’ll go ahead and access the test property of the JSON object. So we can simply dot walk our way there by doing JSON root test and we should see that we get some XML back. So this method can be very powerful when working with web services. And for example the payload we’re getting back is XML but we would like to turn that into JSON. We’ll end this section by going over the event cue method. So first we’ll quickly walk through the example we saw in the slides.

So first we’ll navigate to the event registry and we’ll click on the ServiceNow 201 hello World event. And here we can see the event registry record. So this is informing ServiceNow that there is such an event that exists out there and that we have a fired by and description as well. Now let’s take a look at the script action. So here we can see that the event name is the same as the registry we were just looking in. So whenever that event is triggered, this script will be called and the script is just one line of JavaScript and all it’s doing is logging Hello World to mark’s logs as its source. So now we’ll head right into the background script and type GS eventQ and we’ll pass in the name of the event and click Run. Now to test this we’ll go to the script log statements, we’ll change the source to mark’s logs. And we can see here that Hello World has, in fact been logged to the system.

So that was an example of a script action. Let’s take a look at an example of a notification. So here in my system, I have a KB expiration email notification one day event in the event registry. So you can see here that it’s actually fired by a scheduled job. So let’s go into that scheduled job. All right, so this script is pretty long, but we’re really only concerned with lines 26 and 29, where we are using the GS event queue. And as the first argument, we’re passing in the name of the event, followed by KB, which is the current glide record object, followed by a third argument, the send two, which is the recipient. And finally, we’re passing in the fourth argument, which is the second param KB number, which is the knowledge base article number. So next, let’s go into the notification. And we can see here that under the event name, we have selected the KB expiration email notification one day event. So when that event is triggered, this email will be sent to the email address found in palm one. You.

  1. Section Recap

Hello and welcome back to the last video in section five where we’ll go over a section five recap to Glide system. So where can we use Glidesystem and all of its methods within ServiceNow? Well, Glide system is in fact server side so we only have access to this API I on the server side. However, as we’ll see in the next section there are some APIs that are exposed to us on the client side that provide some functionality that we’ve seen within Glide system. So as a ServiceNow developer, you’ll use the Glide System API when you’d like to access any of the helper methods that we’ve discussed or data about the current user or session. You can reference the Glidesystem API by simply using GS and any serverside. Script glidesystem does not need to be invoked.

It’s automatically available to us. Events and service now have three main components the event registry record the event action such as a script action or notification that will be triggered once the event is registered and finally, the event log. So once the event is executed, it will in fact be logged to the event log table. We can use the event queue method to add registered events to the event queue where they will then be processed. And finally, we can use the Nil method when checking if a JavaScript value is empty. This should be used over simply evaluating using the Equality sign since many values in service now are actually objects. And simply checking if a variable is equal to an empty string will not suffice for us in Service now.