logo

NJP

Master Pagination in ServiceNow: A ServiceNow Guide to Efficient Data Handling with chooseWindow

Import · Feb 14, 2024 · video

Hi all, in today's digital world, where data is 
key, managing and presenting this data efficiently   is really crucial. Welcome to our tutorial on 
the introduction to pagination service house   C AP using the choose window function, which 
is supported by Glide Record. Imagine trying   to fetch thousands of records in a single API 
call. It's not just overwhelming for your user   but also puts a strain on system resources. 
This is where pagination comes into play.   Pagination is a process that divides content 
into separate pages instead of loading all   data at once. You fetch a subset of data on 
demand. This makes data retrieval manageable,   faster, and more user-friendly. In ServiceNow, 
the Glide Record class is widely used and is   used to query tables. The choose window function 
is a powerful feature of Glide Record. It allows   developers to specify a subset of records to be 
returned by setting a start index and end index,   making it perfect for implementing pagination. 
Using choose window has several advantages.   It significantly reduces server load by fetching 
only a portion of records. This results in quicker   response time and a smoother user experience, 
especially when dealing with a large data set.   Let's dive into today's practical use case. We'll 
create a simple script added API in ServiceNow to   fetch configuration items or CIS using pagination. 
Our API will accept two parameters because we want   to define pagination, so we'll be accepting two 
parameters, which will be page size and page   number, to control the amount of data returned. By 
implementing the choose window function, the API   efficiently retrieves data in manageable chunks 
based on the requested page size and number. Okay,   so we can get started with the demo. As you can 
see on my screen, this is my personal development   instance. We'll zoom out to some extent, 
and what we'll do, we'll be trying today   to query this particular table of C C rest. Why? 
Because this particular table has the most number   of records in my instance, so it has two and four 
84, and we'll be creating a simple API which will   return only the society and the display name 
of the CIS. So let's get started. For that,   what you have to do, we'll go to script add API, 
and we'll create a new API, and we will name it   as what? What should we name it? We can name it as 
cm DB CV APS, okay, and we can simply save it out,   and we will not add anything out there, and 
we will go to resources, and in resources,   we click new, and we will be performing a get 
operation only, so we'll name it out as CCS,   and in the relative path, as we discussed, we 
will be passing two parameters out. We'll be yeah,   so I will give a prefix cmdb just to 
differentiate all the APS out, and then   I want to make two attributes but dynamic, so we 
can use curly braces, and we can give page number   and the next one we can give in curly braces 
page size. Okay, so basically, how it works,   for instance, if we are providing a number as 
if you are providing page number zero, okay,   if the page number is zero, that means it's the 
first page, and if we are passing size as 20, so   it will pass the first 20 records. If we pass page 
number as one, okay, or we can consider as two,   okay, one, two, so if we consider two and if you 
pass the data set size as 20, so it will, it will,   it will send out 20 to 40 number of records, 
okay, not the initial ones. So, how to write   the logic for the same? So, first and foremost 
thing, we have to pull the path parameter, so   how we can pull it out, I will give variable page 
size equal to request.pathParams.dot. Exactly,   I have given it's page size, right, so I will 
just take this one, and I'm pulling it from   this particular place, right, the dynamic path 
attributes, I'm just taking it from there. Okay,   and in a similar way, we can have it for page 
number also. Number, p number, now we have both   attributes, perfect. So, I can expand it out 
because I don't need any reference now. What we   want to do, we want to set some default values to 
the page because there's a possibility that we are   not passing any values, or when the API is called 
from the third-party system, they are not, they   forget to pass it out or something like that. So, 
we will set some default because we don't want to   send corrupt data, junk data, right. So, here we 
will say default pagination values, and for that,   how we can do that, page size equal to p size, is 
it present? If it's yes, then we will do parseInt,   and it will be p size, we are just converting 
it into an integer, okay, 10, and I will set the   default value as 10. If nothing is passed, it will 
be 10, perfect. In a similar way, we can have the   page number also. I'll just copy-paste it out. 
This time, it will be page number, and it will   be p number, parseInt, page number, and we want 
to set the default value as one over here. Why?   Because you want, if nothing is passed, you don't 
want to pass the data from the 10th page, right,   you want it from the first page itself. So, that's 
the reason. Then, we'll create an offset. So,   calculate offset, offset will be like, what is the 
start, we can, we can consider, right. So, we can   variable offset equal to, I will just use basic 
math, page number minus one into page size. So,   for instance, if the page number passed as two, 
right, um, and if size is 10, so how exactly it   will count, it will count, um, 2 - 1 is 1, okay, 
one, and what will the page size, okay, so it's   10, so it will start counting from 10 number. Why? 
So, because the counting, it follows the index   logic of the string because it considers the first 
iteration as zero and then starts counting 1, 2,   3, 4, 5, 6, 7, that's why we need this. Now, after 
that, we need variable cList to, I will make it   a blank array, then we'll do a GlideRecord, I 
will not shortcut call to new GlideRecord, and   it will be on the same here, C. Now, what needs 
to, so we have to set, we will not add any query,   but if you want, you can add queries also, but 
I will directly jump to the window, so you, you   can do grci.chooseWindow, and here we will pass, 
it's like, it has two attributes if you consider,   so the first attribute is the number, okay, and 
the second attribute is also number. Force count   is like, if, if it's gr, it's a, if Force count 
is true, gr count method will return, return all   possible records, okay. So, the first number is 
like, from where to where it should start, and   you can consider the simple language, right, so we 
will give it your offset, and what should be the end, so it should be simply offset, sorry, offset, 
comma, offset plus page size. So, for instance,   I just want to define it over here, so for 
instance, if page size is equal to 10, and we   want for page number three, right, so, how, which 
record will get processed, which record we'll get,   so the offset formula is like, where exactly it 
went over here, page number, which is 3, minus 1,   so 2, 2 multiplied by 10, it's 20, so it will 
be like, from 20 to, what is this particular,   what is the end, is that offset plus page size, so 
what is the page size, 10, right, and 20, so it's   30, so it will return 20 to 30 records, 20, 30 row 
number records, perfect. So, that's how it works,   so just comment it out, just to keep you in the 
loop, and then what we want to do, while, no,   we want to query first, right, so grci.doQuery, 
and then, while grci.next, next should have this,   it's function next, what we need to do, we 
have to just push it out, so how we can do,   we have to push it out in the cList, so 
cList.push, and we want to push a JSON object,   right, so how we can define it, push, I will 
give the curly PR, which stands for JSON object,   right, and over here, I can give sys_id equal 
to grci.getUniqueValue, and then, comma, the   next attribute, you can add as much attribute as 
you want, okay, when you want to consume the API,   but just for the purpose, and we are not serving 
this particular as a use case, we just want to   explore on the choose window, and how exactly it 
will work, the script. So, I will just add two   attributes, second one, I will keep it name equal 
to grci.getDisplayValue, okay, no, no comma, that,   I'll just format the code out once, perfect, 
and then we can simply return cList, perfect,   we are done with the code, now let's test it out. 
So, I will just save this out, if you want to test   it out on the console, on the script API only, 
so you can simply do this, uh, REST API Explorer,   okay, so I will just click on this same API, we 
have this particular API, and as you can see,   ask for two path parameters, which is page number 
and page size, perfect. So, let's consider,   I want it from the first page, and I want only two 
records, so now you will understand the power of   this choose window function and pagination. So, 
first page, two records, if I send it out, right,   it gives 200, we got two, perfect, it sounds 
like perfect, right. Now, if I want five records,   make it completely, this API, if I send this, 
then I got 1, 2, 3, 4, 5, that's also good. Now,   if I want four records, but from not from 
the first page, but from the third page,   okay, so it will return those records also, the 
specific window records. So, if I send it out,   so it has returned 1, 2, 3, 4, and these records 
are not from the start. So, if you do order by,   so in the order, you can put order on anything, 
name or any, any unique context, right, and it,   we do the ordering, and basically, it will pull 
the record based on the window which you have   provided. So, this is how it's pretty important, 
okay, this is where it adds value, right, because   pagination is really important because if, if some 
third-party application is consuming this API,   right, and whenever they're calling this, if I'm 
throwing them around LHS or two lakhs of records,   you know, CMDB will at least have this much 
of record, right, then it will be screwed up,   the next system will be screwed up, right. 
So, pagination adds a lot of value over there,   okay, it's really, really important. So, this is 
all for today's tutorial. In today's tutorial,   we have seen, um, how we can use the choose 
window function of the Glide Record in script   address API, and what are the advantages of it, 
with one use case. So, thank you for watching,   and don't forget to like, share, and subscribe 
for more tutorials. Dive into the world of top   data handling with us. Okay, till then, stay 
safe, stay happy, have a nice day. Thank you.

View original source

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