Udemy

GlideRecord Introduction

A free video tutorial from Mark Miller
ServiceNow Guru
Rating: 4.5 out of 5Instructor rating
5 courses
136,339 students
GlideRecord Introduction

Lecture description

An introduction to the most important ServiceNow API, the GlideRecord!

Learn more from the full course

The Complete ServiceNow Developer Course

Learn how to develop on the ServiceNow platform!

08:41:44 of on-demand video • Updated July 2020

Develop on and customize for the ServiceNow platform
The ServiceNow APIs
When & where to script in ServiceNow
English [Auto]
Hello and welcome to Section four. Now that we've reviewed where we can apply scripts and introduce the concept of an API, it's time for us to look at our first service now API, the Glide record. We'll start this section with an Introduction to Glide record, followed by a quick analogy to help solidify our understanding. We'll then jump into the show me the code section where we'll go over a brief snippet of code to show you what exactly the API looks like in a scripting environment. After that, we'll cover a very important topic in ServiceNow called Walking. Next, we'll cover the Glide Record API diagram where we'll take a look at some of the properties and methods associated with Clyde record. We'll then look at a list of Common Glide record methods and discuss the functionality behind them. After that, we'll discuss the stages of a Glide record, and then we'll walk through the acronym Crud and how it applies to the Glide record. Once we've covered these topics, then we can finally jump into ServiceNow and actually start writing some code and background scripts. After the demo, we'll discuss Glide Record Secure and the Glide Aggregate API. And finally we'll end the section by discussing when we can use these APIs. A little bit about best practices and then go over the API documentation. Finally, at the end of this section, you'll find exercises as well as quizzes for you to try. All right. So before I introduce the Glide record, let's first talk about a fundamental concept about web applications or really just any application that persists data, which means that stores data for long term. So let's say we have our application server here on the left and our database server on the right. Let's say we would like to access data that is stored in the database. For example, we'd like to access data about a specific incident. Well, because ServiceNow uses MySQL as its database. And since MySQL is a relational database management system, we'll have to write some SQL in order to retrieve the data. But it turns out SQL can be a bit tricky and just really not very fun to work with. So we as the developer want an easier way to access and interact with this data. It turns out we can do so by introducing an API specifically designed to do just this. Here we have what we'll call our database API for now, which kind of acts like a middleman. As the developer. Instead of writing SQL, we can access data in the database by interacting with the API via JavaScript. Because we've introduced JavaScript, now we have the power of a full object oriented programming language at our fingertips. This gives us a level of abstraction that helps with security, ease of use and powerful helper functions. Well, it turns out this database API is called Glide record, which is what we'll be discussing in this section. Glad record is the most common API you will use as a ServiceNow developer. This API is accessible to the developer on the server side and is used for any type of database operation commonly summed up with the acronym Crud. Crud is an industry term used to refer to the four functions of persistent storage which are create, read, update and delete. Any time you need to read an existing user record, create a new incident. Update an existing problem record or delete a change. You will use the Glad Record API to perform the database task. As mentioned before, this API generates the SQL for us. So we as the developer don't have to worry about writing SQL from scratch. We can think of the glide record as having two stages and the first stage we build out the actual query. So we'll select the table we want to query on and start to build out the filter that we apply to the SQL. For example, we could say we want to show all priority one incidents that were created within the last two weeks. Again, we can create this filter and this happens within the first stage once the first stage is complete. The actual SQL is sent to the database where the records are received back from the database. And you can think of the second stage starting at that point in time. Now, in the second stage of our Glide record, we can think of this as a collection of records or a list of records from the query table. This is where we do the actual processing of the records. So, for example, if we wanted to update specific fields or even delete certain records, we could do so here in this stage of the Glide record, you can think of it as being an ordered list of records. For example, when we view a list of incidents on the incident table, Glide record is being used behind the scenes to query the database and retrieve the data related to a specific filter of incidents. All right. So before we go any further, I'd like to first introduce you to the show me the code slide. Now, I understand this slide may be a bit overwhelming to some of you, but don't worry, we'll cover all of these topics later on in this section. The goal of this slide as well as the show me the code slides in future sections is to give you a practical example as soon as possible, providing you the student with a holistic approach. Now I could talk for hours on the theory and concepts of Glide record, but that wouldn't do you the student, much good in learning this topic for the first time. This is why, when I've taken programming courses in the past, I've always enjoyed instructors who take a moment early on when introducing a new concept to show some actual code. So if you do not understand a certain concept in this slide or in future, show me the code slides. Feel free to even skip this slide and power through the rest of the section. All right. So in this example, we'll start simple. We'd like to print a list of all priority one incidents to the screen. Now let's walk through this line by line. On line one, We are instantiating an object by calling the new keyword along with the constructor Glide record. We pass in one argument, which is the system name of the table we'd like to query. This is simply a string type. We then assign this to a variable named incident GR for Glide record. Note that it is best practice to give meaningful names to variables such as incident. Instead of simply gr. Throughout this course. When I am teaching the concept of Glide record, I will typically use the name of the table or record that I will be querying from, along with a capital R afterward to signify that this is a glide record object. But feel free to come up with your own convention as long as it's related and has some type of contextual meaning to what you are doing. So now that we have the glide record object assigned to the incident variable, we can start to call methods on the object. On line two, we can call the Add query method, which accepts two arguments. First a string, which is the name of the records field. We would like to filter on. And the second argument is the system value. This ad query method is very similar to something you would see in the GUI interface or the back end of ServiceNow. When you apply a certain filter to a table. So even on the incident table, if you click the filter icon, you can apply these conditions to the filter. Well, it turns out that this gooey interface is actually using the Glide record API in the background and using something very similar to what we've defined here on line two. On line three, we perform the actual database query operation, which generates the actual SQL for us, sends it to the database for it to get processed, and then returns the records to us in a JavaScript object. It's the query method that ends stage one of the glide record and starts stage two of the glide record, where we can then think of everything after this line as a list of records, in this case, incidents. On line four. We can use a JavaScript while statement to loop through all of the returned records. We use the Glide record next method to determine if there is another record in the array. If so, we continue to line five. So finally on line five, we use another ServiceNow API glide system, which is referenced with GS in order to print the incident number to the screen. Note that we are able to access the number field by simply adding a period followed by the name of that field. All right. Now, let's take a step back and I'll provide a very simple analogy for Glide record. And this analogy will be buying groceries at a grocery store, and the process is as follows. First, we'll select which grocery store we'd like to go to. This can be analogous to selecting a database table to query. Second, we'll grab a shopping cart in the store. The cart can be thought of as the glide record object. Third, we'll place groceries in the shopping cart. The groceries can be thought of as records from the table. Every grocery item has specific attributes to it, like the name, its price and color. These item attributes can be thought of as field values for a record like the caller on an incident record. Finally, it's time to check out at the cashier. This last step can be thought of as performing some kind of action on the records, whether they are for display updating, deleting, or even creating new records. All right. So let's kind of apply this analogy to a real world example. So again, using our first example, we discussed in the show me the code section. We'd like to print all incidents to the screen that have a priority of one. While these steps may vary depending on the glide record methods that you are using, there are typically four steps involved in this process. Just like we saw in the analogy. The first step is selecting a table to query. In this example, it's the incident table. And the second step? We build our filter if required. Again, in this example, it's priority one incidents. The third step is where we tell ServiceNow to make the actual query against the database table along with our arguments. This is when the glide record actually generates the SQL. Runs it against the MySQL database and processes the returned rows into a glide record object. Again, this kind of marks the end of stage one and the start of stage two. Finally, the fourth step is where we would process these records, whether we would just be querying them, deleting them or updating them. In this example, we're just simply printing the numbers to the screen. We'll talk more about the stages in the third video in this section. However, for now, you can think of steps one, two and three as being in the first stage, the build query, glide record stage, and the fourth step in this diagram as being in the second stage or the process record stage. It's important to note that any ServiceNow table, so any of the 2400 out-of-the-box tables or any new tables that you create can be queried using this Glide record API.