logo

NJP

ServiceNow Scripted REST APIs: Part 8 – POST

Import · Mar 16, 2023 · video

foreign to this next video on creating scripted rest apis in servicenow in this video we're going to look at how to create records using our API using the HTTP post method we'll come back here to our original scripted rest API record and we'll scroll down and we can see we've got the two get resources there so just go ahead and click on new to create a new one and we will give it a name create vehicles this time we'll select the post HTTP method and the path this time will be slash Vehicles similar to the last get resource that we created okay so just remember that pass can actually be shared by different resources so it's important to have a good naming convention in the script again I've got a predefined script that I composed earlier so I'll just pop that in there and we'll just talk through it section by section so similar to the previous two resources the first piece of the puzzle here is just to declare an empty array which will be pushed out to the object towards the end or at the end of The Script and then we go ahead and declare some variables which will be will Define what the API will accept as input so when you're creating vehicle records in our table we will accept make model VIN year country and city nothing else if anything else is included in the request it will simply be ignored okay so we declare those as variables and then we go ahead in the next section here and we need to actually save the request body and work with it so the first thing is to declare a variable for that request body we then convert that to a JavaScript string and then we go ahead and convert that to an array so if we go a little bit further down we see we've got this try section here which we didn't have before but I'll explain why I've included it here when we scroll a little bit further down when we look at error handling but in the first section here and in the in the try we go ahead and assign values to the variables that we declared earlier for each element in that array and then we go ahead and do a simple Glide record operation again but this time we're doing an insert we're actually creating records for each object that we find in that what was originally a Json object that we've converted to an array and then finally we push out a confirmation to the user with the VIN and importantly the sysid we could also include some confirmation and since there if we wanted to or anything else but at the moment we've just kept it simple and then finally at the end here I've included a catch error section there is an out of the box API that you can use for error handling in servicenow I've included probably the most generic method for declaring errors here the service error method and I've just declared the status the HTTP status of the request if anything goes wrong I've set it to 500 and I've included a couple of error messages there as well okay if you have a look at the documentation for defining requests you'll see there are a few other methods that you can use here as well depending on what kind of error you've encountered okay but as I said in previous videos it's really important to have error handling well in scripting in general but also with your scripts in your apis to inform the client if anything does go wrong what the error is and how perhaps they could correct it okay so we'll go ahead and submit that okay so now we're ready to test this we'll come back to the rest API Explorer here and this time we will select the new post resource that we've created as you can see it doesn't accept or we haven't defined any query parameters and we've got the standard request format and response formats that we'll have in every resource and then we come down to the request body because this is where we specify the content of the records that we want to create so there are a few ways to do this here in the rest of API Explorer you can actually come here and just click on add a field and add your value see it but if you do this you really have to ensure that the object that you've defined in the script matches what you've got here because this is a very simplified object that we're creating so instead I'm just going to go over to the raw tab here and paste in a JavaScript a Json object that I created earlier as you can see here it contains an array called Vehicles okay and we've got two vehicles here all right so just go ahead and click on send scroll down we can see we've got the status code 201 which we defined in the script which means that's good and again anything with a 2 in front usually means it's a good thing and then we'll come down to our response body where we've defined the VIN and insert the sys ID that actually confirms that we did create a record in the vehicles table we can carry out a similar test in Postman as well so I'll come over to my collection here and create a new request again similar to the rest API Explorer I will Define the post method here put in the URI again it's the same one as the second get request that we defined earlier with Slash vehicles at the end there are no query parameters to Define I'll just go ahead and specify the authorization that we were using before basic authorization and we'll paste in the body here in Postman you'll just have to select Raw as the format and then come down to Json here and select that and once you do that you can paste your body here Okay click on send and there we have it okay we've got our response back so now we've enhanced our scripted rest API to include Post in other words we are allowing clients and external applications to create records in our vehicle table so that's it so hopefully you're getting the idea now of how we can use JavaScript and our apis to take input from the request and then return output that they can then deal with in this case we've created a new record in the next video we're going to discuss modifying existing records in servicenow so we'll be looking at put and Patch methods and we'll take a little bit of time to discuss the differences between the two okay so see you then foreign

View original source

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