ServiceNow Scripted REST APIs: Part 7 – GET
foreign in this video we're continuing to create our own custom scripted rest API and in this video specifically we're going to look at the get method we're going to look at how to retrieve a record and Records in our servicenow vehicles table in the previous video we actually looked at what a resource in a web service is so if you haven't looked at that video please check that out so as I said in this video we're actually going to create two resources one for retrieving an individual Vehicle Record and another one for retrieving multiple records so when we create a resource in servicenow like many configuration object types in service now you'll need to specify various metadata such as the name but apart from that the meat of the resource itself is actually a script now what does this script do it performs the operation defined by the resource so if we're going to create two get resources to query table that script needs to query the table in our database now what is the main servicenow job script class for working with crud operations in the database that's right it's Glide record so in our script we'll be using Glide record to query the data and return that data to the user through the API in Json format if you don't know what Json format is I've provided a link to a really good video by Chuck tomasi on that topic and for those of you who may already be questioning well Jason why are we using Glide record for what about security etc etc well we will look at security we will look at the issue that Glide record poses when we're using this in particular in an API so if you want to fast forward to video number 15. in this series where we discuss that and also discuss a solution to that problem please do so and then feel free to come back so in this video we'll look at the get operation we'll query the database and then in the next few videos in this series we'll look at the remaining resource methods as well for post delete and so forth so as maybe you can start to see an API a scripted rest API it's nothing more than an interface or a way in which clients and third-party applications can interact with the servicenow database and in particular give it authorization basically to execute a script to perform that action and that script will use Glide record but as we'll see a little bit later there are a couple of other methods you can use as well that a little bit more or actually a lot more secure but we'll start with glide record because that is maybe the most familiar one that you know already so let's get started so we'll come back to our vehicle scripted rest API and we want to create a new resource but you can't create one using the create application file Library here it doesn't appear so you'll need to come down to the related list and click on new or specify a name get vehicle we can see we've got different HTTP methods there we'll look at those later we only need get for the moment and our relative path will be slash vehicle and then in parentheses Vin for the vehicle identification number this will form part of the endpoint or the URI that your applications your clients will need to specify to make that get request okay the VIN in parenthesis is classified as a path parameter there's actually two ways you can specify parameters in the call to the API one is in the path such as here and the second way is using query parameters and we'll look at those a little bit later in this video but at the moment we only need one parameter for this call and that is the vehicle identification number so it actually suffices that we've specified that in the path because that's the only one we need so now if we go ahead we need to create a script so I have one here already ready to go so I'll just pop that in there let's just take a look and break down this script chunk by chunk so line 12 which is basically the first line in the script that's actually doing anything we're just declaring an empty array so that array will contain the vehicle that we output towards the end of the script if we continue and have a look at the second section there we actually need to declare the VIN variable and get that variable From the Path parameter okay and if it's not provided then we're going to throw an error the next part of the script is actually to take that VIN and perform a simple Glide record query of our vehicles table if no vehicle is found we also return an error now this is one thing you'll have to pay particular attention to when you're creating your apis I've only implemented this sporadically in my API what I mean is you'll need to implement proper error handling what happens if particular parameters that are required are not passed what happens when records are not found what happens if if something goes wrong you'll need to inform the client that's calling the API of that error so as I said I've only implemented error handling sporadically in the scripts that I've implemented here just for simplicity's sake and if we scroll down to the very bottom of the script all we're doing is just taking the result of that query for that table and putting selected values from that record into an object into a Json object that would then return in the response to the HTTP call so we're taking the make model year or the fields that we want to return to the client and nothing that we don't want to return so you may have noticed in the table API you're getting all fields from the table but in our API we don't want to return all fields we only want to return the fields that we determined that we specify and that's one of the great things about scripted rest apis because you are in control you determine what is possible what requests are possible and what responses are sent back okay if we go a little bit further down in the record there are some security settings here but again we'll look at these later in this video series so all we need to do is submit and that's it congratulations everyone you have now created your first scripted rest API in service now it wasn't that difficult was it the only thing to do now however is test them you need to make sure that it's working so to do that we'll go over to our rest API Explorer and we'll just refresh the page so we get the latest version of our API so at the moment or previously we were using the table API but now we want to use our own scripted rest API so I'm going to select application namespace we've only got one method here at the moment the one that we just created get so let's use that and then take the vehicle identification number from some record and pop it in there we can see the requests and response formats only except Json and we can send the request okay we get a 200 okay which means it's good and we've got the response back okay with only the fields that we want to return from the record okay so that API that resource it's working okay so let's just perform the same test in Postman previously we used this tool to test the table API as well and we also saw that we got all fields from the record returned okay so in my vehicles workspace I've created a simple collection here called scripted rest API and I'm going to add a new request here with the same name as the one in our API get vehicle and I'll pop the URI here as well so here we just need to replace the VIN which was part of that endpoint and I'm going to replace it with the actual environment variable in Postman so that will just be resolved at runtime to the VIN that I've saved in my variable here the next thing we need to do is just to find the authorization method again we're going to be using basic auth and I've got the integration user credentials already saved as variables which I'm going to use here that's it so now we can go ahead and test it and click on send and we get the response back it works well done everyone Okay so we've created a get resource to get a single record from our vehicles table let's go ahead and create a second get resource now to retrieve more than one record so in our scripted rest API we can quickly just refresh this list so we can see we've got that resource we've just created and we'll go ahead and create another one this time the name will be get vehicles plural and the relative path will also be modified so we'll just have slash Vehicles here these relative path names are quite important because they can actually be shared by different resources so it's good to have a good naming convention so in general you should have nouns for your paths rather than verbs because the verbs refer to things that you want to do and as I just mentioned the relative path can actually be shared by different resources doing different things so it's just best to have nouns here so I'll just put in slash Vehicles here and again I've got a script ready to go and I'll pop that in there again let's break this down okay so just as before we've got a empty array for the answer declared right at the beginning and then the next section here we've got a collection of query parameters that have been saved as new variables we haven't declared these query parameters yet we're going to do that in the next step okay so this is the second way that you can Define parameters in your web service call the first way was using the path parameter which we saw in the previous or the first get and in this example here we're going to use Query parameters because we actually want to give the client an opportunity to provide more than one parameter in the request so this is why we're going to do it this way again if no parameters are defined at all we're going to throw an error to the user and just like we did before we're going to query the vehicles table again and this time we're not going to just use the VIN we're going to use these query parameters that are specified make model Etc we get the results back if nothing has been found we'll throw an error once more but again just like we did before we'll output the response in a Json object this time it won't just contain one vehicle it can actually contain more than one so now that I've saved this record let's go down to query parameter associations now if we have a look here we need to specify the query parameters that we're going to reference in the script so we need to create an association between the resource here and query parameters but at the moment we don't have any query parameters defined okay these are actually shared between resources so again it's important to define the ones that you need and to enable them to be easily shared across different resources if they need to be so if we come back to the scripted rest API Vehicles record and go to the query parameters related list here there's nothing here at the moment so we need to go ahead and create those query parameters let's do that quickly now so we'll create one for the make the model the year the country and the City okay refresh the list they're all there now if we come back to our resource we can go ahead and now associate those queries to our resource so we'll just do this one by one and fast forward through this so you don't get bored and now we have them okay so again these query parameters will be used by The Script for the query to the vehicles table that's it now we're ready to test again so this time I'll come back to postman and I'm just going to copy that URI there and create a new request here call this get vehicles and just paste that URI there and change it so that we have slash vehicles we can specify query parameter here such as the country again the authorization basic authorization send it that's it okay we've got our response back for all the vehicles that are located in the country of Sweden so that's it everyone we have now created our own scripted rest API in servicenow with two resources one for retrieving just one record and the other one for retrieving one or more records so hopefully we've taken some of the mystery out of scripted rest apis in case you're unfamiliar with them or a bit unsure about web services and JavaScript and how they all kind of fit together it's not that difficult by the way the sample scripts that I used in this video are available in my GitHub repository the link is in the description below stay tuned for the next video we will look at creating another resource for creating records in our vehicles table
https://www.youtube.com/watch?v=JeymIT6deCI