ServiceNow CAD – Exploring Other APIs
Hello and welcome to Section Eight, where we’ll explore other APIs within ServiceNow. We’ll start this section by first looking at the Glide Date Time API. We’ll go over some of the popular methods and then take a look at a demo. Then we’ll transition into the glide element. API. We’re already pretty familiar with some of these methods due to glide record, but we’ll take a deeper look at these. Next is the XML document two API which might be used for things like web services. Then we’ll quickly take a look at a list of other APIs within the ServiceNow platform. And then we’ll cover some of the Undocumented APIs and I’ll show you a few tips and tricks on how to find out more about these Undocumented APIs. Then we’ll take a look at Script Includes and even creating our own API using Script Includes. We’ll end this section with a section recap.
So let’s get started. The ServiceNow docs explain the Glide Date Time API as the Scoped glide Time Class provides methods for performing operations on glide date time objects such as instantiating glide date time objects or working with glide date time fields. As a developer, you’ll use the Glide Date Time API anytime you need to do date time operations or format date times or convert between date and time formats. This is a server side API and cannot be accessed on the client side. This slide shows us three ways to instantiate a Glide Date Time object. The first way online two is we just simply do new Glide date time without any arguments. This will create a new Glide Date Time object with the current system time and assign that to the GDT variable. The second option on line five is we can actually pass in a string value of the date time we would like the Glide Date Time Object to be representing of. So here we can see we pass in 2017 525, space Twelve 230.
Finally, the third option shows a variable with that string already assigned to it. And then we’re just simply passing in that variable as an argument. So lines five and nine, the Glide Date Time are essentially identical. There’s just a different method of passing in that string value. Here’s a list of some of the common Glydate time methods. This doesn’t represent all of them, but just some of the more commonly used methods. So we have an ad method that adds milliseconds to a Glide Date Time object.
We have add days UTC, which adds days to the Glide Date Time object, add months UTC, add seconds, add weeks, add years, et cetera. We have before and after methods that will compare two glide date times to each other. We also have the Compare to method. We have an equals which will compare to glide date times and return true if they are equal to each other. We have a get date method which will return the date stored in the glide date time object.
We have get day of month UTC, get local time, get month UTC, get time get week of year UTC get year UTC. We have an is valid method which will test if the glide date time object is valid. We have on or before or on or after methods. And then we also have a subtract method which we’ll take a deeper look at here in just a second. All right, so the subtract method will return the difference in days, minutes, and or seconds between two dates. So on line one, I have a new glide date time object assigned to the variable start, which represents January 1, 2017 at 09:00 A. m. . On line two, I have another variable, end, which is assigned to glide date time that represents January 7 at 08:00 A. m. Of 2017. Then on line four, I create a new variable difference and assign this, the value returned to us from Glide date time dot subtract, where we pass in two arguments, the start and the end time. Then on line five will simply print the display value of this difference.
So here we can see that the difference between the start and end dates is five days and 23 hours. All right, enough slides. Now let’s actually jump into service now and take a look at the API. So for the purposes of this API, we’ll be using our handy dandy background scripts where we’ll execute our code. We’ll start things off by recreating what we saw in an earlier slide. So we’ll create three GDT variables GDT, GDT two and GDT three, and assign these the values of our instantiated glide date time object. Finally, we’ll go ahead and print each one of these to the screen.
All right, now let’s take a look at the add days UTC method. So we’ll keep line one, where we just create a new glide date time object of the current date. And then on line two, we’ll go ahead and log that. So we’ll give it a value of now plus the GDT. And then on line three, we’ll do GDT add days UTC, where we pass in an integer three, representing the number of days we’d like to add. Now we’ll go ahead and print this to the screen.
So if we copy and paste this code, we can see that the new value represents three days in the future. All right, now let’s quickly look at add month UTC. So for this, we’ll simply change the method name and leave it as three. Copy and paste that code and we can see that it’s three months in the future now. And we’ll also do the same for add years UTC. So we can see that this is three years in the future. Now let’s take a look at the before method. So this will test two dates and see if the second date is before the first. So on line one, we’ll keep our new date, and then on line three, we’ll create a new glide date time and we’ll do a GS print is GDT before GDT two, and then do GDT before, where we pass in the second glide date time object as its argument.
So we’ll copy and paste that and we can see that we get false here. And this is actually the case because the glide date time object is evaluating these based on the second. And because the system is processing these within the same second, GDT isn’t technically before GDT two. So to fix this, we’ll go ahead and throw a string value in our first glide date time that we know is in the past and test this again. And we do in fact see true. All right, now let’s use the compare to method. So the compare to is similar to the before or after, except instead of getting a boolean value back, we get a zero if the dates are equal, we get a one if the initial date is before the compared date, and we get a negative one if the initial date is after the compared date.
So let’s take a look. We can see that we get a negative one here because GDT two comes after GDT. If we reverse them, we’ll get a one. And then if we make them both equal to each other, you can see that we get a zero. All right, now let’s take a look at the subtract method. So for this, we’ll go ahead and pass in a string value to our first glide date time that we know is in the past. We’ll go ahead and print that to the screen, and then we’ll create a new glide time object, GDT two, with the current time, and we’ll go ahead and print that. And then finally we’ll create a new variable difference and assign if the value returned to us from glide date time subtract GDT GDT two. Finally, we’ll go ahead and print this to the screen. So you can see that there is a three minute difference between the two. It’s important to note that seconds are not evaluated with this method.
Hello and welcome back. In this video, we’ll take a look at the Glide element API. So the ServiceNow docs state the Scoped Glide element API provides a number of convenient script methods for dealing with fields and their values. Scoped Glide Element methods are available for the fields of the current Glide glide Record so as I stated in the last video, we’ve actually played around with Glide element before, we just weren’t aware of it because Glide Record and Glide Element go hand in hand. It turns out that anytime we want to manipulate a field value with Glide Record, we’re actually using Glide element. So as the developer will use Glide element when accessing Glide record fields and manipulating Glide Record fields, this is a server side API, so we cannot use this on the client side. All right, so let’s take a look at Glide Element’s Get display value method. Now obviously, we should be very familiar with what this does. However, let’s take a deeper look at it.
Because this is a reference field, the SysID is actually stored in the reference field. On line nine, we also see that we get the SysID of that field. So this doesn’t work either. Every table that’s being referenced from a reference field has a display value defined. Oftentimes it’s the number on something like an incident field or maybe the user’s full name on the sys user table. Well, it turns out that every table that is referenced via a reference field actually has a display property that’s set on something like the incident or problem table.
This is commonly the number field on the sys user table. This is the name field. So when we use the Get display value, it’s actually pulling whatever the display value is set on the user table, since color ID references the sys user table. So for the third example, we can see that we get Mark Miller instead of the SSID. All right, now let’s take a look at Glide elements get HTML value. So this does just what it says. It gets the actual HTML stored within an HTML field. So for example, we’ll use Glide Record to query a specific knowledge base article. And if we print Kbrarticlegr text, which is the text of the actual knowledge base, this is an HTML field type.
But if we print the text using the Gethtml value method, we can see that the actual HTML is fetched and sent to us. Now, let’s take a look at the Get Journal Entry Glide element method. So there are certain fields in service now, such as comments on an incident or problem table that actually keep a journal. So when you leave a new comment and that comment gets saved, it keeps track of the time and some other metadata about who saved that comment.
If we tried to retrieve these comments just using Incident Gr dot comments, say on line three, we wouldn’t get anything back. Because this is a journal field, we need to use the Get journal entry method. And if we pass in a negative one, this will go ahead and send us all of the journal entries affiliated with the comments field. All right. And now let’s jump into service now and take a deeper look at Glide element. So to start with, we’ll actually copy the SysID of an incident and we’ll quickly provide a caller and the caller field. So we’ll provide Mark Miller. We’ll save that record. And now let’s jump into our text editor. So in this first example, we’ll actually recreate what we just saw in one of the slides. So on line four, we’ll print Incidentgr Caller ID using the direct access. On line nine, we’ll print Incidentgr Caller ID two string using the two string method.
And finally, on line 14, we’ll print Incidentgr dot caller ID dot git display value using the Git display value method. So as we can see again, the first two have the SysID, which is what’s stored in that reference field, but the third has the display value. Now let’s quickly jump into a knowledge article and copy the SysID of this specific knowledge article. We’re actually going to be grabbing this HTML text found in this text field here. So let’s jump back into our code editor. We’ll do a Glide record and pass in that SysID within the Git method. And now we’ll print the HTML using Kbrarticlegr text githtmlvalue. Here we can see that we have the raw HTML sent to us. Now let’s go back into an incident and let’s add some comments to the comments field. Next, let’s copy the SysID and then jump in the code editor to take a look at Git journal entry.
So we’ll create a Glide record and pass in the SysID within the Get method. And then on line three, we’ll print incident comments, get Journal entry negative one, which will print all of the comments stored in the journal field. So here we can see the comments, we can see the metadata associated with them. So when this comment was created, who it was created by, and the actual comment itself. Now if we provide a one, we’ll get the first comment back. All right, so as we saw in the Glide system video, it turns out Glide element also has a nil method.
So let’s test this out now. So let’s say we want to print all of the incident numbers to the screen whose short description is nil or null. So we’ll create a new glide record. We’ll do our while statement, and then within that we’ll do an if statement. If incident short description nil. Then we’ll go ahead and print that incident number to the screen, all right? And here we can see a handful of incidents without their short description to verify. We’ll copy a number and we’ll search for it and we can see that there is no short description within the short description field.
Then on line four we can do XML Document Parsexml and then pass in the XML string. Now to test that this works, and now the XML Document variable represents an actual XML document. On line five we can print the value found within the active node by doing XML Document Get Node Text and then using XPath, so we’ll do Slash Active to grab the text found within the active node. And this does in fact print true to the screen, which is the value found in that node.
All right, so now let’s jump into service now and take a look at an example. First we’ll recreate our example we just looked at. So on line four we’ll do XML Parsexml and provide the XML string. And then on line five we’ll simply print the text found within the active node. So we’ll go ahead and copy and paste that in a background script, and we can see that we do in fact get True printed to the screen. Now we’ll jump back to the Code and Print Activity due.
So we’ll copy and paste that. And now we can see that we get a string of the date and time. We can also create a new element on the XML Document. So on line five we’ll do XML Doc create Element and we’ll pass in Pet. Now on line six we’ll simply print the XML Doc variable and we can see that we have a Pet node.
Now let’s add some text to that node using the create element with text value. So on line five we’ll do XML Doc create Element with text value and then pass in two arguments, the first being Pet, and the second being the actual text found within the node, in this case Oscar. So we’ll copy and paste that code. And now we can see that we have a pet node with a start and end tag with the text Oscar found within that node. Now let’s take a look at the git first node method. So in line five, we’ll print XML git first node and then pass in XML incident. And when we log that, we can see that we get everything within the XML node. If we add slash active, we can copy and paste that, and then we see we get the active node printed.