Pagination with chooseWindow - Did You Know
[Music] Chuck here with another service now quick tip for you now let's say you've been asked to create a scripted rest API so others can retrieve records from your instance but there's a catch the people asking for the API require you to build it with pagination did you know there's a Glide record method that can help you with this let's take a look for this video we'll use the example of our cmdb table now currently we have about 2800 records in the table if we were to use a script like the one shown here it would get all records that's a lot of data to retrieve and send at one time if we were to try this in a custom inbound API this is a great use case for pagination to allow the consumer of our API to require a group of Records or a page at a time the way to do this is with the Glide record method choose window let's say we have a list of Records by using choose window we specify a starting and ending parameter like this one thing to note the ending number is zerob based which means if I tell it to stop at 20 I'll get 20 records but it doesn't retrieve record number 20 it actually simplifies some things as we'll see later on and for what it's worth this is the same behavior we see with the JavaScript substring method so keep that in mind now to get the next page of Records we would specify 20 comma 40 and then 40 comma 60 and so on let's see how this might look in that original script before the query statement We'll add a choose window and pass in 0 comma 20 there is a third argument for choose window to force a row count query now normally choose Windows row count works just fine but there are some edge cases such as text searches where the row count is not performed setting the third parameter to True May fix this for simplification we're leaving it off here since we don't have any issues now let's run that script and see what happens GS info tells us the total number of rows in the table and sure enough we got 20 records starting at the first one now we can tell this because the order by statement in the script is sorted by name this script is all well and good for demonstrating how to get one page but let's make some modifications so it's a bit more flexible in getting a page using a function that accepts two parameters only we'll take a different approach rather than passing start and end we'll pass start and Page size if we know the page size we'll know where to end which is calculated here we'll also use a counter to indicate where we are in the overall table row rows the rest of the function should look pretty familiar using choose window passing in start and end values here's another common thing to do with paginated apis return a token or account to let the caller know if there's any more pages to retrieve we're going to get ready for this eventuality by passing back the count variable if we started at zero and requested 20 records count will be 20 when we return from this function which is exactly where we want to start the next page that makes it easy for the consumer to put in a loop let's call the function like this looks pretty good now let's try page size of five nice and now we'll start at five and get five more well it looks like we've got the basics of choose window mastered now let's take a look at how the rest of the scripted API would work now we've already built the API and started building a get resource the main differences between the function we were just looking at and the one here is that we need to get the start and Page size from the rest API request query parameters which we've done here note that these come in as strings which is a problem when you try and add start and Page size without converting them to integers using parse int that's something you learn the hard way now I also added this notation to the page size to set the default value if nothing was provided for the page size query parameter now here we've created an empty array to store the objects we wish to return for now we'll just send the row count CIS ID and name back now like before we calculated the end parameter for choose window and here's the running row count we set up our Glide record query and here we'll get the total number of rows because if we're on the last page we need some way to to tell the requestor to stop because there's no more data this is often done in the response payload with a next page or next page token property here's where we'll retrieve the records and create a list of objects while tracking which one we're on when the loop is done fetching a page we determine what the next page should be if there aren't any more records we return a null finally we assemble the response body and send it on its way let's try it out with the rest API Explorer we'll set a query parameter for the start of zero and leave the page size empty to test our default for 20 click Send scroll down here and there's our payload with 20 records and next page tells us where to begin for page two if we pass this in as the start parameter click Send and look at the response body we've got 20 more records and a new page start of 40 let's try starting at 40 and get five records to make sure that P page size is working looks good now we'll test the logic to make sure our next page works when we hit the last page of Records we can see from the response payload that there are 2784 records total let's start at 2760 and Page size of 20 and we get 20 records and the next page starts at 2780 now you and I know there are only four records left and the next page property in the response payload should be empty if we try to ask for 20 more so let's do that and sure enough there's our four records and the next page is null which the consumer can use to stop requesting Pages just a quick note do not use choose window or set limit for that matter with delete multiple and update multiple that's because choose window and set limit are not applicable if you try to delete a p age at a time it will delete all records on the first call now armed with this information the next time you get a request to create a paginated API for thirdparty services Custom Service portal list widget whether it's in rest graphql or whatever I hope you remember choose window as a way to group the records one page at a time thanks for [Music] watching
https://www.youtube.com/watch?v=6A4H9lhju4s