NOWCommunity Live Stream - API Adventures - Scripted REST APIs

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] so good morning good afternoon good evening wherever you are and whenever you are welcome to the community live stream my name is chuck tomasi senior developer evangelistic servicenow and i am here on august 2020 to bring you information about script include no sorry scripted rest apis not sure where that came from the benefits of a live show there's no editing that goes on here it's one guy pushing buttons running the lights running the cameras it's all happening here live so any mistakes you get to see you get to hear that's just the way we roll here this is the compute community live stream so good morning to everybody who's joining me on youtube thank you very much you people crack me up because uh they're they show up very early and there's this conversation that's going on before we even get started i think we've got uh six or seven likes before the show even started you guys are amazing that that's just awesome i invite you to go over if you haven't already done so go to the youtube channel that's uh you see there on the link right there in the lower thirds subscribe like you know what to do this is youtube it's 20 20. you're smart people you figure it out uh somewhere i had my phone and if you turn on the notifications you will get a notification that looks like let me get rid of the other ones that aren't supposed to be there something like that says it's now time and the notifications actually show up 30 minutes early because i do roll the stream early so you can jump in and join that crazy conversation that's been happening thank you very much to everybody who's been doing that the the point of this show if i didn't mention it already or you haven't seen a previous episode is to give you the tools to put in your toolbox to become a more effective servicenow administrator and developer and builder we're going to do everything from no code capabilities to things that are highly scripted a lot of this influ is influenced by your contributions to the community by your emails to me questions that come across the the community are very important to the content and the generation and the thought process that goes into this so that i can give you the explanation the tutorial somebody asked me a question that i thought was i thought everybody knew it but apparently they don't so means i've got to go back and cover that topic no matter how simple the question may seem or how complex i am a specialist in the platform on developer topics that's what i do integrations notifications if you're looking for things that are deeper on things itom grc mobile we have specific videos for that i invite you to again subscribe to the youtube link and you'll get that information as it comes out we've got specialists that do that i tend to gravitate more towards the core platform stuff that developers do thank you thank you for all the accolades on there as a backup plan just in case youtube is acting a little finicky as it does sometimes we do have twitch as a backup plan those videos are posted for a couple of weeks and you can go and check them out there also if you've got something that is not directly related to today's topic please go over to the community and ask that question this is about scripted rest apis so i invite the question the dialogue and the tips there's people sending the information back from that live chat saying oh you know this is what that's for and hey if you do this you can save yourself some time i love it that's this this is the live community interaction we have on this video so please participate when you can 2 p.m utc which i'm not going to say you do the math because i wrote it down here for a number of time zones that's seven a.m pacific time 10 a.m on the east coast of the u.s note that these are the times between march and november when everybody changes their clocks around i'm in arizona i don't change my clocks so i always get up at the same time it's always 2 pm utc for me and it will always be the same time on my clock so it's not 6 a.m in the winter and something crazy so i have that benefit some of you do some of you don't but right now it's 10 a.m morning eastern time 3 p.m in london 4 p.m in berlin 5 p.m in moscow 6 p.m in dubai 7 30 p.m in most of india i don't know if i'm safe enough to say all of india somebody maybe correct me that all of india is on the same ist time zone 10 p.m in singapore 11 p.m in tokyo and midnight the following morning in sydney until you all change your clocks again and then the world goes crazy and i'll update you then so hopefully that gives you a guideline of what time you can set your alarm put it on your calendar maybe you do wake up at 3 a.m if you're in hawaii or 4 a.m if you're in hawaii and listen to this terrific thank you very much for joining me on that thank you again for everybody joining wow this is a crazy crowd popular topic today so give me just a minute to finish up the pre-roll and i will get through this somehow my cursor got way way off on this let's continue on we talked about the community get those questions in the community hundreds of thousands of people that are there waiting for you to answer also want to invite you to check out the developer portal if you haven't already oh wait my cursor is on the wrong screen i goofed let's do that again there we are developer portal over at developer.servicenow.com you can check that out get a free personal developer instance start working with the latest release paris which has been out for in early access about a month now close to a month the api documentation examples free learning plans code snippets a share community over there where you can share applications integrations code snippets is all blog andrew and brad and i have been contributing heavily to the paris content so i think last week was flow designer week this week is integration hub week so look for blog content and live coding happy hour all that good stuff is over at developer.servicenow.com invite you to go check that out and as part of that is the developer community if you check the uh connect tab on the top see that up there where it says connect over there where my hand gets my hand disappears okay fun with green screens where where go to connect and there's events and the events will not only tell you what developer meetups are coming along which we have three next week minneapolis pittsburgh and oh shame on me i forgot the third one it is minneapolis tampa bay i'm sorry so it went on tuesday wednesday and thursday i think one of them moved i thought we had two on thursday but keep an eye on this calendar that's where things move around and on that connect page is also under events we have upcoming podcasts upcoming webinars upcoming community live streams you will know what i'm covering in two or three weeks before everybody else by watching that page so invite you to go over to the developer community as well as the meetup page and get that if you are like me and you build apps in your off time please go over to bitly slash tell us about your app and volunteer some of that information you will help the covid situation for every app you submit i know some of you have done that thank you thank you very much we'll donate 50 to the covid relief and recovery program so please do your civil duty and generous contribution costs you nothing but a few minutes and we'll donate up to 250 for uh up to five applications that you do there if you haven't heard by now well i don't know where you've been because for the last few weeks i've been shouting from the rooftops that the break point podcast is now available and the second episode with babe slusher talking about flow designer and integration hub topics is coming out this wednesday so in two more days if you go and subscribe through apple podcast google podcasts uh spotify you know wherever you find your favorite podcasts we'll be there if we're not on one of your podcast directories let me know and i will submit it there i just turned in the submission for amazon and audible they're doing a podcast thing coming up super secret only if you have a podcast you know about that but we'll be on there as well so this is the developer podcast we're going to be having behind the scenes discussions with our people with with customers with old-timers it's going to be a lot of fun and look forward to bringing you that content in the upcoming weeks so bitly slash sn-break and dash point podcast is called breakpoint i know there's a tennis term called breakpoint as well apologies i didn't know that's not a tennis ball on a stick that's supposed to be a magnifying glass representing me i guess we'll move on from there tomorrow the 18th of august is our tech now episode 78 and we are going to be talking about how to build uh now experience components with wolfgang and interacting with the data so look forward to that that will be in a little over 24 hours from the time i'm doing this live and i'm using that as an excuse to really sink my teeth in and get ready for the now experience components go over to the bitly link you see there tn 78 reg and get all that information we will be doing a little bit of scripting today and i will put this into the github repo that you see right there on the bottom of the screen bitly slash sn-cls so that you can pick it up and try it and break it and twist it around and mangle it up and make it all your own that's a lot of fun i'll have that available just minutes after i hit the stop button so give me just a couple of minutes somebody put in the chat right at the very end before the show was done hey where's the code you know you got to give me a second i will have that pretty much everything else is pre-produced except the script you see me writing live that goes into the repo right after the show so look forward to seeing that if you are not comfortable with javascript and you'd like to learn more then i offer you this javascript series at bitly slash sn learn dash js thank you so much to everybody who's made this a huge huge success it's fueled the fire for more series like this i'm currently working on one for integrations end to end all about integrations but for now we're going to stick with javascript it's been out for a year lots of great comments a lot of people sharing this with their colleagues and with their employees to help them get more comfortable with javascript and this is specifically for the variation of javascript we've got on the platform because rather than a console.log in some places you may need to do gs.info for example learning some of those basic apis to do database queries this is all for you it starts at basic syntax and it goes right through some pretty heavy duty scripting stuff so thank you mark says the now components is that also now used on virtual agent for the custom control definitions or is that different i am not a virtual agent expert but i do know some people who are and we can definitely ask that the now the now experience and the now components right now are limited to the workspace experience when we get into quebec and rome in san diego those releases that are coming up in the next 6 12 18 months we will be seeing it more widespread throughout the ui but i don't know if it ties into virtual agent at this moment okie doke let me uh last comment i'm enjoy sorry if i mispronounced that but uh mentioned that uh helpful information about a what yes i will be doing a specific video about credentials not every single one of them that we've got but oauth and api keys and jwt lots of questions i had a wonderful discussion last week with andrew barnes one of our developer advocates and brad tilton also another developer advocate in the developer program about what topics should we throw in there they were very clear that we needed some information about authentication credentials and aliases and connections that can be a bit confusing we've got some things to make that a little easier okay now that i've covered that let's get into today's main topic as i scroll down to my outline notes where are they here we go i am logged into my personal developer instance that i got free from developer.servicenow.com look at that ding going and i want to do a quick basic explanation of what is rest because some of you may not know this is a piece of that integration we've got lots of ways on the now platform to get information in and out of the system so one way is you could go to a list of course we all learned this in our basic course let's go to incident.list and we could export something as an excel spreadsheet very common way to do it if you go down to export excel and then a spreadsheet gets downloaded to your computer you could do xml there's lots of ways to also import information through import sets and data sources and well one thing that i absolutely love about the now platform is the rest apis rest stands for representational state transfer there's a trivial pursuit question for you it actually was on our jeopardy game at one point it might be in the sample data i don't recall but if you go to the shared download jeopardy you can find that all out yourself i wrote a jeopardy app for servicenow have fun with that okay represent what does that mean representational state transfer well it allows applications to talk to each other over the internet and pretty much everything on the internet travels in text even images are represented as text and then decoded on the other end well we can transfer some information from one system to another using this uh format called rest and it's done over http or https same way that browser pages are done you can use either the format of xml which looks a lot like this xml it stands for extensible markup language i'll show you a quick sample of that if i say show xml it's down here somewhere why do i not see it ah we'll find a different record then fine be that way here we'll take a an email record somewhere on here there it is show xml and xml has these various tags in it you can see it says body text slash there's nothing in there checkpoint to slash checkpoint a lot like html has h1 and slash h1 very similar in format just one way of transferring data from one system to another so that they understand here's a bunch of information that you can use for structured data the other one is json which is javascript object notation and i'll show you that in a little bit very common it's probably the most common way that i've interacted with with rest apis so what is the point the point is that an application or a system can make an end point which when you think about it is a lot like a url a web address says go to this location on the internet ask for this specific information and it will return you something assuming you have credentials if there are credentials required let's look into some of the examples of doing that we have on the side menu this thing called rest api explorer and this is a built-in tool to the platform similar to like a postman only it allows you to quickly look into and evaluate what is in the system we have a number of namespaces let me put this away for just a moment these are the different name spaces or scopes that we have things including some that i've built i have one called ghc for my uh is that my garage door controller i can't remember i've got one for most of them that start with now or sn are the ones that we came out with out of the box you can create your very own uh namespaces and rest apis which is exactly what we'll be doing in a moment under now we have look at all this this is all the stuff that's built in the most common ones used are the table api or the import set api i'm not going to go through every one of these today i invite you to do a little investigation and then nice thing about the rest apis versus their predecessors called processors well that's almost a dr seuss thing like the scopes and the spokes we have predecessors and processors scripted rest apis you can version them so let's say you wrote an api a year ago and it was set up to accept certain parameters maybe you give it an id and a table and it returns a record for that okay we already have a table api but let's assume you wrote one for your own special application a year later someone comes along and says well i need to also get this additional information say well to do that i need a third parameter you don't have to go back to the old software that was communicating the third party service that was using the two parameter method to get information you could say all right that was version one you keep using version one and the new application will use version two the three parameter version so you can have different versions along the way much like you see with youtube or linkedin or twitter a lot of these systems come out with their apis before they even do the user interface they're thinking about not only what information is needed but how do i connect these third-party services to interact with mine and i've done that a number of times as well before i write the user interface i'm thinking about what is that third-party system even service portal you can have service portal interact with your system as a third-party application through rest apis in fact that's how i do most of my record updates and data fetches is through rest apis we'll get into that another time i'm i'm building out little building blocks along the way so we can start to connect these up into higher functions as we go through in the community live stream so this is one of the things i want to show you the rest api explorer let's do a real quick example i've got different methods over here this is important because this is how the http connection communicates with the other system if i said get when you're doing a web page fetch you're actually saying go get this page and the remote web server will send you that information in html and the javascript and the css and all of that comes down with a get command if you could look behind the packets that are flying around somewhere in there it says get url and it will get that a post is if you need to create a new record on the other system and then there's this put patch and delete delete obviously is instructing the remote system i want to delete a record and again we've got full protection over this with acls and permissions and authentication to make sure that the right people are doing the right thing won't dive too deep into that today but i want to mention that different sweet put and patch is put implies you're going to send the entire writer they're very very similar both are used for an update method okay but put you need to send the entire set of data let's say i have a record with five fields and i want to update fields one and three well if i use a put i still need to send all five fields with the updated values in fields one and three with patch i only need to send the two pieces of information that i'm updating if i make a mistake and i send two pieces of information with a put i gotta make sure i say these right because they're very close it will empty out the other three fields that i didn't send it goes well you didn't send them so they must be blank so be cautious in how you use those it's technically possible although it's not politically correct it's technically possible to do a post with a an update you can do that and there's nothing wrong with that but you are in full control in a scripted rest api when you're not using a scripted rest api follow the the standards that of put post patch okay post create a new record you're giving it all the information it needs to correct that create that record and it will often result in an http status code of 201. go look them up http status codes are pretty standard 200 means everything worked everything was okay 201 is i created something for you we're all familiar with the 404 that means i couldn't find whatever you were asking for and it goes on from there the 500s are where you have server errors all right just never hurts to be a little familiar with these as you're building out a scripted rest api you can look those up just google http status codes there's even one in there a funny one i think it's 419 or 429 that's the the status messages i am a teapot i think it was used for testing at one point okay so hope i explained that abhishek about the difference between put and patch excellent because it confused me for a long time too and i will admit i don't always adhere to the proper standards the rest api explorer is very handy especially if you're going to be doing your own scripted rest apis let's take a quick example my favorite incident table let's go get the incident table very easy to put together this is again a very postman-like experience if you use a third-party application i could get a query in here this is an encoded query string on cispar underscore query if you want a lesson on that we've covered that in various other videos i'll keep it very simple and priority equals one now for testing i can say give me one or give me 10 i am going to ask for 10 records it won't give me all of them that are all available and i could also specify some additional query parameters we'll get into the different parameters that we can pass here i can also add different header parameters for now i'm going to ask for this in json format i can add i can get json xml or text.xml most of the time i do my interaction in json this is how i'm going to ask for the information from servicenow it says if i have say a post command i'm creating a new record expect the input in json this is what i expect back and they don't have to match they commonly do but they don't okay in this case i'm going to send you json i'm expect back json and when i hit send it says hey there's my 200 okay that's good i like to some i saw a shirt at a conference one time that just said 200 okay this is the actual http information that it sent there's my get because i specified a get command over on the left this is the url and it goes right up to table that's the api the incident is the table i specified there's one of my query parameters this perm query and here's another one sysparm limit so the query parameters go on the url or the uri okay that's where we can pick those off and i'll get into those when i start in receiving information and getting those there's a whole bunch of other information in here you can look through it at your leisure it'll even tell you how many records it actually found how many it's returning how long this query took to process 369 milliseconds and down here in this grayish window is my response body this is the content i get back from the server and it comes back as an object with an array inside it called result and here is one record that goes from parentheses you can see i get all this wonderful stuff out of incident even if i'm not using these tables or all these fields excuse me it tells me the link if it's a reference field assignment group is a reference field excuse me and the ssid value a lot of great stuff in here that i get back what if i didn't need all of this or i didn't want all of this that's just one record it goes on and on i can go up here and say sysparm fields let's just say let's use the example that was already in here number short description and caller id that's all i want hit send again and i get just those three fields number short description and caller id with both the link and the value very nice now notice here right in this section i have some code snippets so let's say i'm working with my friendly powershell programmer or my php friend across the hall sorry php isn't in here but my favorite python programmer who's writing their own private system and wants to interact with servicenow i can click this and up comes a code snippet for them to get them started on they put in the right credentials in here in basic authentication username and password and away they go here's the code you need to start talking to servicenow using this get command exactly what i typed in rest api explorer so very powerful tool for creating third-party connections very fast i use this a lot when i'm writing my scripted rest apis because i want to test does this work from a command line let's not get too involved with adding third-party applications or going into a script or creating my service portal widget and trying to make that communicate let's see if i have basic simple communications if i get what i expect out of my scripted rest api okay let's go build one of these things enough talk let's do let's go to studio you could also do this from elsewhere i'm in my community live stream app and i already have one called demo so let's create a new one create application file scripted rest api down here under inbound integrations i'm going to click that twice and up comes my form let's give it a name called i'm absolutely unimaginative today i'm calling it chalk it gives it an api id we have another question several releases ago there was a bug in the script you get back from powershell don't know if they fixed it i don't know either i'm not a powershell expert but uh probably wouldn't hurt to check in paris b real quick hey chuck are you going to cover sending attachments in today's session probably not this is basic getting started with script uh with scripted rest apis there are methods available in javascript to help you encode and decode and put that stuff in so i'm going to do just basic data transfers so thanks for the question rohit um that's it that's the protection policy is really for my benefit as a developer if i want to make this protected nobody else can see the code which means it's a black box i publish the api and away we go let's submit that and that is the scripted rest service there's two parts to this there's the service and there's the methods each service can have a number of methods i could interact with this service as a get or a post or put or a patch in fact i typically have one service per application and then let the methods do all the rest you may choose to divide it up a little differently if you look at something like youtube they have a service for videos they have a service for playlist they have a service for channels that's all how my my api path is up here this is how i'm going to interact with it and i'll show you that in the rest api explorer in just a minute i could enable versioning for this example i won't remember i could have a two parameter three parameter and then somebody would specify in here i want the version one chalk or i want the version two think about that early on because if somebody's using the latest version and you go and change some parameters they may actually break on their other end now down here in the related list i have resources this is where i start to specify the gets and the puts in the posts in the back let's create a new one and i will call this um see if i have something special on my notes that i was intending to do we talked about get put post patch we've talked about xml json and all right i don't have any specific code written so we're going to write this live come up with the idea as we go i have courses programs students that kind of thing let's go get a course and have it respond with the number of students okay in that course very simple all i'm going to do is specify uh an id a ssid for a record and it will come back so this will be gets course student count give it a good name you want to make it descriptive it's going to be a get i'm not updating anything i'm just asking the system for some information we'll start very simple and notice over here the relative path very important because each of your methods should have a separate path if you use the same slash you're going to need a different service for each method then you can only use one if it's if it starts with slash it's going to go well which one you want so i'm going to get call this get course count that way i could have a get i could have a put i could have a okay so this is very important to have if you're planning to have more than one method per service all right requires authentication good idea requires snc internal also good idea know the part about the security you may or may not necessarily want all of these on requires acl authorization you may have for example a form that's publicly available well in that case you don't want authentication if you have a service like tell me the temperature tell me the stock market price we don't authenticate with services like that is just publicly available if you've got information that you feel safe publishing to the public to publishing to third-party applications then you don't need authentication uh the acls a lot of interesting stuff in here study up on the acls i'm not going to get too deep into the security i want to get you a quick uh piece of information fast in here is the scripted part of the scripted rest api okay i have i am going to simply return some information and note the process function parameters up here i've got two objects available to me in this function one is called request that's what's coming in that's what somebody is requesting from my scripted rest api the other is a response that's what i'm returning to them okay so this is the center of the conversation request what are you asking for response what am i giving you and i will dig into those a little bit uh in just a moment give me a second just to create a quick request excuse me a quick response in the meantime i'm going to go over to docs.servicenow.com and look up rest request i think that's what it is i can't find it rest request there's an object and i'm just missing it rest api request there it is rest api request gives you the format and the methods and all the functionality of this there is a rest api body i've got a method called get header and this body is of a an object type as well called rest api request body we'll get into that this is the request i can get all of these different parameters there is also a rest api response so all of this is very well documented in the rest api response we have a whole bunch of other methods get stream set body set headers i want to set the body of the message that i'm returning back so back in my scripted rest api let's do request and what it sends is an object i don't need to stringify this first that's done implicitly but let's create a little object var obj equals we'll put status in here i like to put status messages in okay and i often return some kind of message hello world okay just a simple little status message and when i do response dot set body obj it will return that for me so now that my code is pretty trivial but still works i did not mean to hit that i meant to save it when i save it it gives me the full resource path that i need to engage with this which i could give to another developer or i could go down to explore rest api right here and it brings up the rest api explorer with the correct namespace the method that i want everything's pretty well populated i don't have anything to add to this except hit send and in my i get a 200 which is nice okay i didn't set that in my response body i have my result object it's always going to be there you can have to deal with it there's no way to get rid of it and i have my status and my message hello world i've created a scripted rest api very simple get method to return some information and of course being a server-side script i can add any server side code that i want in here like var course gr uh no i can't remember the name of the uh i need a mini attendee table i have to go to my script includes look at my constants i wrote this what last week on my many-to-many table do not remember it was course user okay so it's called attendee m2m that's the table i want i name these simple so i can remember them and now i failed okay attendee gr equals new glide record you know what i'm just counting so let's do like glide aggregate on that table we'll do an attendee gr dot add query our course is ah where are we getting the course from well our api needs to know the name of the course this is where we start interacting with the request now we could pass this information to the api in a number of ways if you played with the rest api explorer you may have dealt with this there are three main methods that i use one is called path parameters the path parameters would go up here for example i want to specify an id a sys id a name some sort of employee id i don't care i put this curly brace notation up in the relative path to say i expect a parameter in the path okay that's one way another way is with a query parameter a query parameter which we saw from the incident example is where it shows up in the url itself question mark something ampersand something else those are query parameters and they typically have a name uh a name equals value part to them whereas path parameters are simply all right it's in this format you better give it to me this way first i expect a table then i expect an id and it's all slash something and it's very prescriptive about what it is query parameters can be reordered they don't need to be in a specific order and you can usually pass a little more information in there however it does take up more space on the url i don't know i'm i'm sort of mixed between when to use one over the other there are times when i say yes i definitely want an id i tend to use query parameters more than path parameters you may have your own choices uh check patrick comet you spelled course wrong where did i spell course wrong oh i spelled course wrong up here thank you typos are important i didn't spell it wrong anywhere else i hope where counts in the scripts thank you all right i need i'm going to ask for a query parameter for the course and i'm going to get it as course id and i do that with something like this var id equals request dot body that's a built-in object this is where i love the autocomplete because you can see it's got a lot of stuff in here i could get at the path parameters i can get at the query parameters ah that's what i'm after i'm after the query parameters request.body would be if i used my third method of passing information to this which would be in a post put or patch method there you have to specify the remote system needs to request with a body of information we'll build one of those real quick we are starting to run a little short on time but let's do this request.query parameters whatever this name is is what needs to show up on the url so let's make it a little more url friendly course under id and then i can check if not id maybe somebody didn't surprise maybe maybe somebody didn't supply one then i am going to do a response dot set status come on and tell them you are wrong this was bad something happened uh let's just return a 500 and i can also do a response dot set body now notice i don't have my obj up here i could move it to the top or i could just return an object in here that says message [Music] no id specified i like to try and do as much error catching with my rest apis as i can we still haven't finished this down here for now let's comment that out i want to test and make sure that my error handling works that it actually will fail if i don't specify that query parameter back to rest api explorer again very useful i'm not going to change anything i do a send and it returns 500. oh bad things happened it's even red and i get my okay hello world for whatever reason it didn't send back the response oh because it fell out of here i need an else that should be an else it just went and reset things and sent me a really lovely message even though something bad happened the reason it set the 500 is because there's no other set status down here let's align that i'll do a command a shift tab try this again this y rest api is really handy when you're building your script rest api explorer there no id specified so in order to specify that i would tell rest api explorer to go get and add a query parameter course id well i need a course id because i can't remember any of them course id here's a course of course of course let's go to cs210 it has some attendees yes yes it does we have 15 of them and i am going to get the sys id from there paste it into there congratulations now when i pass this in i do my send it includes it as part of the url as a query parameter it doesn't matter if it's first second third fourth it figures out all the question marks and ampersands and decoding and every everything else and it gets back yay i have an id i don't know if it's a valid or not but in any case let's continue on with our script to count those things with a simple glide aggregate query i can now use id in here for all of the attendees that are in there attendee i don't know what i call the gr that's more of a glide record let's just call it ga which means i have to change these typically i'll use a gr for glide record ga for glide aggregate dot add aggregate count attendee 10 d ga dot query and i also need bar count equals i this is something i like to do is set account variable outside of my condition for example if they gave me a bad society and i didn't find any records it's going to go i go whatever okay rather than getting a zero which may mean there's nobody signed up for this course i want a negative one almost like that index of function on a string where it says it wasn't found that's what i'd i'd rather have for an error condition now my third-party system that's making this query says if i got back a minus one that means things really went seriously wrong and i probably have the wrong id if i get a zero it means it's truly no attendees registered yet so if attendee should have made a shorter variable dot next we did a query so let's do a next to get that and attendee count equals parse int because it's going to come back as an object possibly stringified let's not do that attendee dot ga dot get aggregate count and i'm going to assume that's a decimal number now i have a real true integer and i can put that in obj dot count equals i could put it in count let's just do that you know what skip that we'll put make it part of the object right out of the gate count is minus one assume a fault condition until someone proves otherwise and this will become this we'll do this in one line there saved ourselves a variable don't know why but now that curly brace is in the wrong place now it's in the right place we should be able to get a number out of there in the count attribute of that object what do you say we try it again we have our society up here let's just hit send and it says 15 hooray wasn't sure if that was going to work or not what are the differences url in your eye url is a uniform resource locator and it typically just includes the the point up to the the end point like index.html okay that would be your uri a url the uri will include all of the um the query parameters that's that's my knowledge i'd have to look that up to verify but i'm 95 sure that's the difference they're often used interchangeably so it just means here's an end point you can go get uh i honestly can't remember what uri stands for at the moment somebody in the chat want to look it up and throw it in there for me that'd be much appreciated let's go make a post operation now i need to find something safe to post so that i can do that let's you know what we could create um we can make this simple we'll just we'll just make an info out of this so i've got my scripted address resource to get the course count let's create another one and if i refresh this list i can see it down here let's create a new called um simple status you know what i actually do want to create a record somewhere uh you're passing a society directly from the client never trust the client you're right what kind of checking occurs to ensure that other invalid parameters didn't get sent out in outside of intended what is google for 500 uh chris i'm not sure where you're going with that but um the client could send you just about anything on that society they could say id equals chuck that's where you need to have some pretty strong error checking in your scripted rest api let's build a post real quick i am going to look for a very simple table that i can populate on this application employee table sure that's fine because it has a minimum field number of fields for example we just want the name and title we'll create a new record in the employee table called we'll do name and date of higher so in honor of chris's question we'll create a new scripted rest resource called create employee this is going to be a post i very often forget to set this and when i'm creating a new resource why isn't this creating records because it's you're not telling it something right i give it a very descriptive name create employee and i'm not going to be doing too much error checking on this one particularly but we want the body of that message and the body is going to be in a json format so we'll call this body equals request dot body dot data dot something okay maybe they pass it in as user we specify all of the inputs and all of the outputs and the processing logic that happens behind this that's the beauty of scripted rest apis apologize for not specifying it earlier and as we are getting closer to the summary that's what makes scripted rest api so powerful you when you build one of these you are in total control and you can say i need this or i don't need that and i'm going to pass this back and i'm not going to pass this back different from table apis which go and query the records you could combine multiple tables lots of other great stuff so i get my hands on the body with a request.body.data.whatever meaning i need a packet of information something like well we'll do this as soon as it's built okay let's not get ahead of ourselves and i could have all of the information in there or i could break this apart and say var name equals body dot name bar what did i say title equals body dot title that's typing that way i don't have to say request a body.dated.user.title.request.data.user. okay you get the idea i like to break it down into smaller things in this case i will burn a variable let's just um i i could go through the whole glide record and insert the record but in the interest of time i'm simply going to do a gs.info new record request new employee request and say name equals name and title equals title save that that's create employee let's go refresh this so that it knows i now have hey i've got a get and a create let's go to the create i could specify additional query parameters but i'm not going to this is a post and i need to fill in the request body here i could do the builder this way it's a name chris add another one title wizard and it builds this json payload for me or i could go to the raw tab and build something like that as well you can choose which you like you may already have something you want to copy and paste from another editor that's very helpful if you don't remember or you're afraid you get your quotes and your colons and your commas all mixed up then this would be a good idea as well so i'm going to go from the builder in this case i built my json payload i send that and it says warning you are doing a post it's going to impact something on the remote system are you really really sure yes i'm really really sure and it says you made an error cannot find type property from undefined oh bummer data okay anybody spot it maybe let's create an incident that's a good idea i probably should have done an incident i forgot to say user i need a user object it doesn't work because i should have put some error checking and you're saying hey you didn't really give me a user object so my data format is incorrect is what it's telling me i really need let's go back to my builder take this raw information that belongs inside of a user object so i could say here user is an object i did this to myself right and in that object is my other stuff and now i have too many curly braces i did that on purpose okay that should work better it was just a formatting issue are you sure yes i am the post worked the body went out like this i got a 200 everything's okay and i don't have a response coming back because my rest api did not return anything by default it says 200 everything's okay unless of course you have a syntax error like i did or a data error but if i look in my logs i should now have a message that says new employee request there's the name and there's the title so i can now interact i can pass information in with a query parameter or a path parameter or in the case of a post put or patch i need to supply it in the body if that's the point of the body it says hey i want you to post this information so how do we know what data is hitting our api ah very good question very good question and i think i did a data logger episode not too long ago about sticking a statement in there that would say where did it come from and what was the payload and all this good stuff so go check for that in the in the backlog of these community live stream videos i think it was either late april or early may when i did that so i don't have all of these memorized unfortunately by topic and date but that gives me a lot of power there what else um yeah i think that just takes me to the summary where the uh tell you about scripted address apis great way to interact we will be building on this in the future but it's it's a very powerful interesting way one of my favorite topics to to do because i often build an application as the foundation logic in a script include so i've got a way to count records or return a json object or build up things that i want to then pass to a scripted rest api or the scripted rest api is calling methods to do these updates so they work very closely don't put all of this crazy logic i i did this just for demonstration purposes and it was pretty trivial on both of these that if i really wanted to count attendees i would have created a script include called count attendees that simply returns a number okay what's the input what's the output make it a very black box easy to unit test easy to build see previous episodes on script includes if you want to hear me pontificate about that that abstracts that information and allows you to change the apis there then the scripted rest api is simply the the vehicle for accepting those parameters and scrubbing them and making making sure that everything is clean before it calls the script include and then passes back the result that the script include return done that a number of times now your third party application or your service portal widget can call this with confidence that it will get the right information at the right time uh that's not opening 404 pager i'm not sure where the 404 when will es6 be available in service now vivek excellent question i get to ask that about once a week and i turn around and ask the developer team about the same i don't have a specific date or release on that but it is on the roadmap we hear you there's there's a number of things in the rhino implementation that are causing us a bit of delay but it is very uh very high on the priority of things we're looking into resolving with that i am going to invite you to be part of the community the oh thursday thursday i'm going to be getting into flow designer basics so there's a number of people out there who still are resisting the jump from old legacy workflow to flow designer i want to give you a bit of a primer to get you started on that understand what the triggers and actions and logic and how these pieces fit together with the data pills if you're new to the platform this is also going to be a great start on flow designer so also a way to get people like me off of business rules and schedule jobs into flow designer concepts more to get you started on that so thank you for all that information one more last question from chris i was attempting to reference injection and the known item when the add query is invalid it removes that so may unintentionally return more than you wanted oh yes if ed there are properties and methods you can use to investigate to see if the query is valid so if you're concerned about invalid queries check out the property i think i can't remember what it is but just look up invalid query there's also a gs.get session that lets you set a strict query on that or or you could start looking into glide query which i will have in an upcoming episode that's a new api that we've come out that sort of uh wraps around and and builds that expect syntax into glide record but it's a lot safer and the data types don't always come out of strings which is my favorite feature so i'll be covering glide query shortly it was one of our most popular things at creatorcon 2020 and we will likely be having it at cratercon in the fall so we will be having another conference don't want to give you too much information about that it's still in the early planning stages but watch for information about that as well head over to the community if you've got more questions about scripted rest apis or flow designer or anything else there's a lot of very smart people there willing to help and hundreds of thousands of them as a matter of fact until next time i'm chuck tomasi thanks for watching if you learned something go share it and be helpful till next time take care bye you
Info
Channel: ServiceNow - Now Community
Views: 9,704
Rating: 4.9433961 out of 5
Keywords:
Id: PLuE-iMcDgs
Channel Id: undefined
Length: 59min 13sec (3553 seconds)
Published: Mon Aug 17 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.