ServiceNow CAD – GlideRecord
- GlideRecord – Demo 2
Hello and welcome back. In this video, we’ll be continuing our demo. So let’s start with the order by method. The order by method accepts one argument, which is the field that we would like to order the list by. So we’ll go ahead and create a new glide record object and we’ll order by the short description. And on line three, we’ll go ahead and add the query method. And then we’ll simply print the number of the incident, followed by the incident’s short description. So we’ll copy the script, paste it in the background script, run the script, and here we can see that all of our incidents are in alphabetical order. So by default, the order by method is ascending. So if we want it in descending order, there’s actually an order by descending method. So we’ll go ahead and apply that. Now copy it, paste it and run it, and now we can see that it is in descending order. All right, so now let’s go over the set limit method. The set limit method accepts an argument which is an integer, and this will set a limit for the query. So if we only want, let’s say, ten records to show, we would pass in the value of ten and only the first ten records would be returned to the glide record.
So to test this, we’ll actually create a new glide record. And this time we’ll provide a table of problem. So we’ll be querying the problem table, we’ll do problem gr and set limit of five. So we’ll want the first five problem records on the problem table. We’ll go ahead and query and then simply print the problem number to the screen. We’ll copy and paste run the script and you can see here that only five problem records are showing. Due to our set limit method, we can also combine this with the order by method. So we’ll order by short description and retrieve the first five records in alphabetical order. We’ll actually go back and change the number to the short description. So this can be a little more clear to us. We’ll copy, paste, run it, and now we can see that the short description is in alphabetical order, showing the first five problem records. All right, so now let’s quickly run through the can create, can read, can write and can delete methods. These methods will check the ACLs and verify that the current user can perform these functions.
So we’ll create a new glide record on the problem table, we’ll go ahead and query it. And then after we’ll just do a simple if statement and we’ll do if they can create and if they can read and write and delete, we’ll go ahead and print to the screen. I have access to create, read, write and delete. Now, obviously, if a specific user did not have access to do any one of these functions, then line four would not get printed to the screen. Since we’re using and logic. All right, now we’ll go over the git row count method. So we’ll go ahead and create a new glide record on the Incident Table. And we’ll just do a simple print of the gitro count method. We’ll copy and paste this into the background script, run it, and you can see that we have a total of 154 incidents on the Incident Table. Now let’s go over the Has Next method. So although the Has Next method sounds similar to the Next method, it’s actually quite different. The Has Next method returns a Boolean value either true or false if there is another record in the array like object, it’s important to note that this does not perform the actual iteration like the Next method does. Let’s take a deeper look. So we’ll create a new glide record on the Incident Table, query it, and then on line three, we’ll print Incidentgr Has Next. Because we know that there are incidents in the Incident Table, we can expect to see True printed to the screen. Let’s copy and paste this code and run it, and we can see that True is printed.
Now we’ll actually change the Has Next to Next, copy and paste this in the background script and then run it. So here we can see that we’re getting True as well. So it turns out the Next method does return a Boolean in addition to iterating to the Next object. All right, so now let’s take a look at another example. Oftentimes we’ll find the Has Next method and an if condition. Sometimes we want to do a glide record where we only expect one record to be found based on the filter we apply. So if we perform an if condition such as Incidentgr Has Next, then we know for a fact that we found our matching record. So before we go any further, try to guess what will happen here.
All right, so let’s actually copy this code and paste it in the background script and run it. And we can see that we’re not getting anything printed to the screen. Strange. So if we go back to the code, we can see that we’re using the Has Next method on line three, which does not perform the actual iteration to the first object in the array. It’s just simply returning a Boolean value. True or false if there is a Next value. So even though there is a Next value on line four, when we try to print Incidentgr number, there’s nothing at the pointer or the actual Incident hasn’t been iterated too. So there’s nothing for us to print. If we were to change this Has Next to simply the Next method, then you see we successfully get the number of the first Incident and the Incident Table.
Hopefully now you understand the difference between the Has Next and Next methods. Next is what actually performs the iteration to the object in the array. And now to test the has Next method, we will add a query of priority and we’ll pass in a value we know doesn’t exist. So we know that there is not a zero priority on the Incident Table. So we’ll go ahead and apply this. And when we print Has Next to the screen, and when we print the Has Next method, we should expect to see False. And we do see false here. Now let’s revisit the Git method. So before the first time we used Git, we passed in the SysID. But as I said before, that as long as we provide a truly unique value passed in the Git method, then we can use any field we’d like as well. So in this example, we’ll use the Number field and we’ll pass in a specific Incident number, Incident 20168.
So here you can see that we did retrieve the Incident using the number field with Git. Now we’ll take a look at the Git link method. So the Git link method will retrieve a link to the current glide record. So in this case, we can expect to see the link printed to the screen for Incident 20168. And you can see that we have the Incident Do question mark SysID equals the SysID to that Incident, followed by some additional parameters. So if I copy this and add this to the URL, you can see that we’re taken to this Incident Record. Now, let’s test the delete multiple method. For this, we’ll actually use the Addencoded query method as well. So let’s go to the incident table.
And let’s say we’d like to delete all incidents that contain a string of Incident Space Pound. So we’ll do short description contains Incident Pound, and you can see that we have four here, since we’ve already deleted the Incident number three Incident using the Delete Record method earlier. So we’ll actually come up here and copy the query and then head to our text editor. So actually, before we delete any records, let’s first practice logging these to the screen. So we’ll simply add a while loop, and we will add the short description of the Incident to the screen, copy the code, paste it in the background script, and you can see that we have the four incidents here, incident One, two, four and Five. So now that we’ve successfully logged them to the screen, let’s go ahead and delete these using the Delete Multiple method. So one nice thing about the Delete Multiple is that we don’t have to add the query method or the while loop. The Delete Multiple method actually takes care of all of that for us. So we’ll copy this code, paste it in, run the script, and we get nothing back. So it looks to be successful. And to test this, we will actually use the code we just ran to log the incidents to the screen earlier. Copy this, paste it in, and you can see that we’re getting no incidents back again.
To verify this, we’ll head to the incidents table and verify that there are no incidents with a short description containing incident space. Pound all right, so we’ve covered inserting new records and deleting records. Now let’s cover updating records. All right, so before we actually write the code to update a record, let’s go to the incident table, select an incident and we will copy that number. And let’s say we want to update the urgency of this record. So you can see this incident currently has an urgency of three. Let’s actually change that to a two within our code. So we’ll go to our text editor, create a new glide record, use the git method and pass in the incident number. And then on line three we’ll do incidentgr urgency equals two.
Now, this hasn’t actually been applied yet, so on line four, we need to actually call the update method. So we’ll do incidentgr update and this will actually save the changes to the database. So we’ll copy and paste that code, run it, and then head back into that incident. And we can see that the urgency is now two. Now let’s run through one more update example. Let’s say we would like to update all of the incidents that have an urgency of two and update them to have an urgency of three.
So before we do this, let’s just simply print all of the urgency two incident records to the screen and you can see we have those here. And now on line six we’ll add incidentgr urgency equals three, and on seven we’ll call the update method, copy and paste the code, and now we can see that we have no incidents that have an urgency of two. All right, now let’s cover the add null query method. So the add null query method accepts one argument, which is the field that we would like to apply a null query to. So we’ll jump into the code and we’ll create a new glide record. And then on line two we’ll say incidentgrad null query and pass in short description. So what this will do is find all so what this will do is find all incidents that do not have a short description. In addition to the ad null query, there is an add not null query which is the opposite. It will return all incidents that have some sort of short description. You.
- GlideRecordSecure & GlideAggregate
Hello and welcome back. In this section, we’ll discuss the Glide Record Secure as well as the Glide Aggregate APIs and how exactly these relate to the Glide Record API. So Glide Record Secure is a class which is inherited from Glide Records. This means it has all of the same methods. In addition to doing all of the things Glide Record does, it also automatically checks ACLs for us so we don’t have to use methods like can read, can write, can update, or can delete. Within a standard glide record script. Glad Record Secure automatically handles this for us. If a specific user does not have the privileges for a given crud operation, then the code within the Glad Record Secure won’t be executed. Glad Record Secure is commonly used in script includes because script includes can be called or invoked from just about anywhere in service. Now, like the service portal, it is important to keep security and who the current user will be in mind at all times. For example, Glad Record Secure isn’t as important in, say, a business rule that already has security defined within the business rule condition filter or for a table that already has all ACLs defined in episode 15 of Tech Now ServiceNow’s podcast. Andrew Concay does an amazing job at showing the differences between glide record and glide record. Secure. If you’d like to know more about the differences and when to use each, check out the video podcast on YouTube.
All right, so in this slide, we can see that if we use Glide Record Secure we don’t have to write nearly as many lines of code as opposed to Glide Record. Glad Record Secure automatically factors in these conditions for us. So if a write process is being done and the user doesn’t have the access to write, that code won’t execute. Now we’ll take a look at another API that is related to Glide Record glide Aggregate. Glide Aggregate extends the Glide Record class and is used for performing aggregate queries such as count, min, max, sum and average.
This is commonly used within reports and any scripts or lists that perform calculations, like the average time an incident stays open. It’s important to note that the Glide Aggregate API only works on number fields. It doesn’t make sense to find the average for the short description field. Also, it’s much more efficient to run Glide Aggregate instead of Glide Records Gitro count method. This is because the Glide Records gitro count method actually loops through the entire return list of items, incrementing a counter variable each time. This naturally takes time to process and go through the list. If this list is hundreds of thousands of records in length, this can cause a significant performance impact. Instead, use Glide Aggregate’s Count Aggregate to retrieve the total number of records for a given filter. This is different since it leverages an SQL aggregate statement and does not to loop through each record on a Glide Record list. For more information about Glide Aggregate, check out the API docs.
- Section Recap
Hello and welcome to the last video in section four. In this video, we’ll go over where we can use Glide Record glide Record Secure and the Glide Aggregate APIs. Then we’ll do a section recap. And finally, we’ll end this video by going over the ServiceNow Docs API website. All right. So you might be asking yourself, where can I use Glide Record? Glide Record Secure and Glide Aggregate within ServiceNow. Well, because these APIs deal with the database we can only access these APIs on the server side. It’s important to note that there are ways to use Glide Record and a client script on the client side. But this is not recommended and is now deprecated. So we won’t be covering it in this course. Instead, we’ll go over an alternative and the recommended approach to retrieving information about a specific record on the client side and section seven Glide Ajax.
All right, now let’s review what we’ve learned in this section by going over a section recap. So we learned that we use Glide Record when dealing with database operations and that Crud stands for Create, Read, Update and Delete. We also learned that Glide record as well as Glide Record Secure and Glide Aggregate are server side APIs. We went over the two stages to the Glide Record API. The first stage is the Query Building stage and the second stage is the Process Record stage. This is where we actually process the records that we’ve queried from the database. Then we covered one of the more complex methods a part of the Glide Record API, the next method. And we learned that it actually iterates over the returned records from the database.
We also covered the Git method and that we use this method to retrieve a specific record when using a unique field such as the SysID. We also discussed that it’s best practice to use Glide aggregate over Glide Record when dealing with aggregates like counts. The Glide Aggregate API is just more efficient at performing these aggregate calculations than Glide Record is. We also discussed that it’s best practice to log records before deleting them with something like a background script. Although I mentioned you can use Glide Record and client scripts, it’s actually best practice to avoid this and instead use something called the Glide Ajax API which we’ll discuss in section seven. Finally, we discussed when it’s appropriate to use Glide Record Secure such as in script includes when we need that additional layer of security. All right, now let’s jump into a quick demo where I’ll show you how to use the ServiceNow Docs API website. So here we have the current home page and we have a number of options in the header here.
If you do not already have a personal developer instance I highly recommend signing up under the Program tab. Under the learn section. There’s some training documentation as well as some step by step guides I recommend checking out as well. But for now let’s hover over the API section and click on Server. Here we’ll be taken to all of the server side APIs within ServiceNow. We’ll scroll down and locate, glide record So to start with, the API documentation provides a brief overview of the specific API we’re looking at. In this case, Glide Record, and then it lists all of the methods associated with this API. It also has examples to most of the methods shown on the right hand side as well, which can be very helpful when learning new methods.
So if we click on Delete Record, we’ll be taken to this method where we can see that it says what it does, deletes the current record, and on the right, it shows an example of doing so. The documentation also shows any parameters for a given method as well as the return type. In this case, we can see that the Git method accepts two parameters, one being the name and the second being the value, and it returns a Boolean data type.
While we don’t cover every single method in this course, I do recommend going through this API documentation sometime and just simply reading what the method does so you get a better overall understanding of what’s actually out there and what’s available to you as the developer. It’s also important to note that on the documentation page in the bottom right hand corner, there’s the version of ServiceNow. In this case, we can see that we’re on Istanbul, but if you’re working on an older version, you can select that and the API documentation will change according to what version of service now you’re on. Even with my years of experience on the ServiceNow Platform form, I still come to this API documentation quite a bit.