logo

NJP

Community Live Stream - API Adventures - API Model

Import · Sep 14, 2020 · video

[Music] good morning good afternoon good evening wherever you are and whenever you are welcome to community live stream my name is chuck tomasi from servicenow this is september 14th 2020 and our topic today is the api model this is the the connections between the different places you can script on the platform i'll get to that in just a bit i've got a wonderful diagram to show you it's probably one of the few things i've used from powerpoint on this series ever so let's continue on with the pre-roll while others are joining in there was a bit of a technical issue i forgot to set the date properly so you may not have gotten your 30-minute warning let's see if we have our official youtube warning now i just got the twitch and i just got the youtube so if you turn on those notifications you will get the notifications at the appropriate time as long as i remember what day it is it's the 14th not the 13th 14th thank you very much drew for pointing that out and we got that all squared away so we've got people joining thank you come on in the admission's free the water's fine the drinks are on you let's continue on as i mentioned we do this on youtube every monday and thursday at 2 p.m utc as long as it's not a holiday i'm healthy and the technical things all work so subscribe to the link click the like do the notification thing you know what to do on youtube right got any questions put them in that chat i'll be doing live chat on this topic so remember i'm not this isn't a live fire you can't just throw anything at me today i do this topical episode by episode now if you've got a question that goes beyond that then please well you can go over to twitch and watch too if you've got a question that goes beyond that please put that in the community at community.servicenow.com that's right behind me lots of people there very smart willing to help way quicker than i am these days because i've got videos and podcasts and what not to create and produce and script and organize and lots of things happening so i i want to make sure that you get this content in a timely manner but the community is always there 24 7 to help you out wonderful people wonderful group great answers remember it is crowdsourced so you may not get the exact answer and they're not there to do your professional services bidding for you you can't say hey can someone help me implement an integration so i can populate my cmdb that's probably not going to happen in the community but what you can get is hey what are the best practices what have you done how can i do this one piece remember put in functional requirements so people understand what context they're trying to help you with it's a great thing but it's you're only going to get out of it what you put into it and if you don't put into it enough information well that's a problem i also invite you to go over to the developer portal developer.servicenow.com if you haven't done so already why not why not go over and get your free personal developer instance running the paris release i believe paris is officially generally available on wednesday the 16th this is the 14th so we're still in early access for a little bit sneak preview release notes apis lots of great stuff over at developer.servicenow.com including blog posts from andrew and brad and me and some of our mbps the developer mvps that were about to make a post about how to become a developer mvp for the next year so watch for that that's up at the top if you go under connect there's a blog keep your eye on that i believe we even have an rss feed that you can subscribe to to keep it on that blog when you're not online so lots of great information over there again the apis we'll be looking at this from time to time but again the biggest value that a lot of people get is that free personal developer instance that you can use as your sandbox your learning aid free learning plan so as you learn and you test and you try and all that good stuff you can do it there as i mentioned there are meetups so one of them is coming up next week in vancouver we had a couple last week there's always something going on check what's available in your time zone you can find that at meetup.servicenow.com pro excuse me meetup.com pro servicenow dev program i'm not sure how i got those urls all mixed up that's what happens when you try live streaming at 7 00 a.m the lights are on but sometimes the brain is not fully engaged just making sure i didn't miss anything in our notes that we have lots of people's joining in thank you good morning good morning good morning happy to see everybody on the live stream thank you i understand some of you have been here for over an hour or at least you click the youtube link and you're there waiting for an hour and you're just probably waiting for me to talk hey there's noise coming out of my computer or headphones i better put them on and go listen that's where you can find that information another quick reminder i'm going to do this just for a little bit longer tell us about apps you've built over at bitly slash tell us about your app for every app you contribute we'll donate 50 to the un covid 19 recover response and recovery fund up to five apps up to 250 dollars you can do your part to make the world a little bit better and let us know what you're building we're very curious to know what customers are doing out there and this is your chance to contribute another quick reminder that the breakpoint podcast is still going on we launched it in earlier uh april no august august what month is it it's it's september about a month ago launched a podcast for developers called breakpoint discussions with some of our product managers we've got customers just customer conversations coming up and maybe the occasional special guest here and there i've got one coming up on wednesday it is scheduled to release every other wednesday so the second the 16th and then the 30th will be another one but i've got some other things planned for you just keep subscribe to that link that you see there go to bitly slash sn-breakpoint you'll see a host of links where you can find this on apple and stitcher and tune in google spotify it's on your favorite directories just be sure to look for breakpoint and then it will look more like a servicenow logo if it looks like a tennis logo you're probably going to get tennis information and if you're a tennis player that's fine i'm not going to discourage you from doing that but just want to point out breakpoint is also a tennis term that could be confused with the developer term one final reminder that we have a tech now episode coming up tomorrow on the 15th at 8 00 a.m pacific time we'll be talking to jivin core about how to make your next upgrade simpler faster easier we're going to be talking about the new paris features we might even get into a little bit of atf and some of the other things that she's worked on go over to bitly slash tn79 reg and get that taken care of i do have some code snippets today that we will be sharing they are available or will be available immediately following this broadcast in our github repository in a dated folder so go to bitly sn-cls look in the folder 2020-09-14 and you will find everything you need there if you want to take this and experiment with it and play and twist it around and make it your own plus all of the other scripts that we've had there over the past several years so you can find that over at github by following that bitly link looking forward to that if some of this looks foreign to you then you can certainly brush up on your javascript skills by going to bitly slash sn-learn dash js i am just a bitly fool the last few years sweet steve is online as we pointed out in technorama last night sweet steve is an anagram both parts have the same letters it's kind of cool didn't recognize that very clever on you good luck thank you for sharing that can you explain about the database view not on this episode maybe on a future one thank you very much for the question but uh great question for i do have i think it's on my personal youtube channel that i did something oh man years and years ago don't know how useful it still is but take a look at that thank you steve for interjecting today's topic is the api model so that's all i've got let's get started i'm going to go over to my personal developer instance got my herbal tea at the ready voice is feeling a little rusty from the weekend maybe too much singing had a lot of fun saturday night doing saturday night karaoke let's begin with not the pdi i have a diagram here you're free to take a screenshot of it let me get my head out of the way did that work that did not work that's small head that's no head okay gotta hit the right buttons there charles so this is my interpretation this has not been published anywhere else but my interpretation of a well-built application stack as you're building your applications there are many many places to write script in in the now platform and knowing when and where to write them can mean the difference between getting bogged down in a mess of spaghetti and maintenance and upkeep or spending more time doing building adding value doing constructive things now how you connect these pieces might be a little confusing at first and that's why i wanted to jot this down and take a look at this because at the base we want to include things like script includes flows and sub flows and actions these components that you can build quickly they're very specific and can be called from just about anywhere on top of that that second layer is a number of things you could have a client callable script include something that says abstract ajax processor in there i'll give you i'll show some examples trust me this isn't all about this diagram but i want to use this as a reference we've got other core places in the platform i call them core maybe that's not the right term but common places like ui actions and business rules and flows and notice that the flow could call a subflow the flow could call a script include a business rule schedule jobs you can obviously script in those you can have a script include call a script include probably should have put that in a reference but that green box in the middle is really where a lot of people commonly put a lot of their script and they don't abstract it down to that bottom layer in the library section as a flow or a sub flow or an action to make it reusable make it maintainable make it replaceable and you'll find yourself with a lot less maintenance as david lew told me when he first started with servicenow fred luddy had a mantra or a phrase he said look if i catch you copying code maybe it was from a ui action to a business rule maybe it was from one script include to another if i catch you copying code you're fired because all you're doing is copying a potential error around if they get a prb opened up on a particular piece of functionality whoever copied it has to go and find out is it being replicated elsewhere that's not a good idea use these object oriented languages for what they're worth build objects on top of objects and call them from other classes we could also call these library functions from a scripted rest api now i'm not get this isn't everything because you may trigger a business rule from an update done in a scripted rest api okay there are little nuances that you'll want to be aware of and there are many other places that use script fields like ui policies i'm not getting into all of that today i actually do have a ui policy up there but this gives you an idea of where things can be called from other things and this is a good model to follow to reduce your maintenance so that is the general idea let's take a look at some of the examples that we've got let me bring that back and let's go to this guy again all right i'm going to bring up my script editor and i have some notes jotted down here so let's do example one let's take a script include and let's demonstrate the three methods for the instant script uh okay i think i know where i'm going let's do a straight up script include i've got the instance here i've got an initialize which is the code you run when you say new something or other this is simply called instance i've got a get name get id and get url function in here they just return some properties rather than doing gs.getproperty all over i'm using an example and then i've got a get info that takes a parameter and then calls either get id get name or get so i can call one function with a parameter rather than calling three functions but just a little sample script include to do that i also have a test script in here that i'm going to use and by putting all of that code it means i don't have to go chasing around all of my scripts to find out wait was that the instance name was that the instance id what am i looking for let's take this sample script and put it into scripts background paste that in and it calls all three it says new instance get name new instance get id get url and get info with name so i can call these either directly or i can call them by parameter run that script and it says your instance name is dev 7900 79 000 excuse me the instance id is this wonderful society let me make that a little bigger for you maybe a little bigger bigger i've got my url how did i get the url well if you look at my script include over here the url was created from this dot get name and servicenow.com that's a very common thing that a lot of people do especially in male scripts but you don't have to do that in male scripts by the way outbound mail will be tagged with the right instance name automatically it'll take relative urls and turn them into real urls and then if i say parm of the parameter to get info rather than saying get the name i can say i want to get the instance name what was it i've got id name and url that i could pass into that generic function so i create a script include makes it very easy to do it makes it very easy to call very easy to test this one test script could be my answer to saying is this fully validated do i get what i expect out of here yes i am convinced that this script include works great now other things can call this as well i'm testing very small discrete pieces of this application let's build on that the next layer up if we go back to this diagram let's take that center piece and put a ui action on there that calls my script include now that i have complete unequivocal let's try to try that unequip complete trust in my script include i know that's working i can build the ui action if i were to try this all in one ui action and it turned out to be you know 1500 lines of code that's too much to test and it's just it's hard to modify that so i like my ui actions i like my script scripted rest apis i like my business rules as small as possible so that they're calling these discrete pieces they've called they're calling pieces that have been tested and like i said maybe those pieces have other pieces within them that have been tested as well if get info starts giving me some grief i know that it's either going to be get info or it's going to be one of the functions that it calls but it's not my ui action it's not my business rule i can easily break it down and say where is the problem occurring easy to test less time maintenance more time lost my cursor there it is again multiple pointing devices getting on my nerves let's go what is the api method for getting actual browser's url there is one called glide uri so check that out you can use that class i'll be talking about that in a few episodes i do have that in the queue to talk more about glideape glide uri and its classes its methods let's go build that ui action that calls my get info stuff so if i go to i don't know if i completely reset this app go back to my ap my pdi too many things in here with letters in them i am going to create a ui action ui action ui action and i hope i didn't not that one it's under system definition ui actions and i hope i didn't create one with the employee table already if i did i left things lying around that i shouldn't have and didn't do a good job cleaning up before i prepared for this add instance url to notes hey somebody already built this for me but i'll show you how easy it is sounds as easy as it sounds i mean it is as it was easy to build as it is so i've got my name i've got a table i've got the order i'm a good citizen and i give my action a name whether i need to or not because it may be client callable at some point i'm going to make this a form link from over on that side my conditions i don't have any conditions in here it's always going to be present on any updates and take a look at this simple simple script okay i'm not going out and doing gs.getproperty here i'm saying hey go get me the url now according to my script include according to my api i could call this two ways i could do new instance dot get url i could do new instance dot get info and then in parentheses send it a string called url totally up to you i would recommend the latter make the get id get url get make those kind of private functions and don't publish them there is no true private and public concept in javascript so we're going to do this put the result of this call into the notes do an update and stay on the same page all right very easy ui action to do so if i go to the employee table and it's nice you don't have to watch me type anymore and i've got my employee table here there's my notes field there's my link down here right there add instance url to notes you may choose to do something different similar concept though when i add that ta-da it adds the url to the notes not sure what happened to my number field maybe it didn't have a number i wasn't watching so i've got a very easy ui action that calls a script include now that i know that those two things are working let's build a little further we have example number three i'm going to take a third-party application i'm going to take this right-hand side third-party application or scripted rest excuse me service portal widgets to me are considered third-party applications because they are running they're not on the server service portal is a client-side browser thing and occasionally you need the client to call and i generally do those through rest apis then the scripted rest api catches it i'm going to be talking about that uh later in october i'll get to that in a second i think that's when it was oh i've got a scripted rest api and service portal widget coming up so let's take that right-hand tier where i've got a third-party application or service portal applet widget that talks to a scripted rest api what does that look like well let's get the right pointing device again go back to this screen and code for that looks like my service portal widget client this gets a little a little scary sometimes i don't again i have to check and see if i pre-built this or not i am going to you know i think i just have that available let's go to dev79 slash sp not that one and i'm pretty sure maybe you know what let's just go to the widget editor service portal configuration i don't remember what page i even put it on we'll go to our widget editor and we will create edit existing widget i don't remember not the benchmarks menu how did i get there i clicked a little too fast let's say do we have anything in cls nah all right let's go make one let's create a new widget back to the designer not the page designer wow i am all over the map widgets thank you create a new widget i'm going to call this um cls 914 terrible widget name but that's the date i'm on i like to create test pages just because sometimes testing in the widget editor doesn't do what you need it to i'm going to copy out my widget server script which has a simple text message click a button pretty tough okay i did a service portal thing in august if you want to watch more about that you're welcome to let's take the client script copy that into the client script i will walk through this all in a second and get my html on to here now save it preview it let's enable preview and over on the side right hand side i have a tiny little form okay it says name id url if i click the name it puts in the instance name what's happening here is i don't know why i said click a button i didn't even use that in the in the html let's look at the client script just on itself i think the html is pretty self-explanatory one input field i've found it to the data.txt oh i know what it was the click a button goes inside is the preview text that's what happened the client script grabs the event the on click where was that well that was attached to every one of these buttons says get info get info get info get info well here's the get info this is not the script includes get info method this is the client script calling this function can i blow that up i can blow that up good i'm going to throw a message to the console make sure i got the right parameter that's always a good way to test to make sure your click event is working before you build this out and then again i'm going to get into this deeper but i use this http call to call a scripted rest api that then we dig in a little deeper in fact i'm going to go to studio since all this stuff seems to be pre-built except for the widget let's find out collapse that inbound scripted rest apis and here is my was it instance or employee i can't remember what i was calling i'm calling instance okay so i've got an instance service with a get info resource yeah i talked about scripted rest apis recently too one i think it's one of the most popular videos that we've done if not the most and i call it with this call right here api blah blah blah instance info and it's expecting a query parameter not a path parameter if it was a path parameter it would be up here next to info right there in curly braces but i'm not doing that this is a query parameter and then it's calling there's like zero error checking in here so be aware of that it's calling the script include which i already have implicit trust in it's going to work if i call it properly and it is proper because it's calling get info with url name or id then it returns that in the response body as the answer my scripted rest call says go get that answer that is now the value of c.data.txt and since these things are all bound together it updates dynamically so if it didn't work i would say look it was a 400 or a 500 and it did this while i was building it out so having a failure function in here works really well for capturing that again going deep dive on this in a couple of weeks a month or so i forgot so now when i click one of these buttons i get the result back from that script include we just traveled down that right hand side i could also do this with a call i think i turned off authentication on this i think does not require authentication so i could use a third-party application to do this as well if i go to the rest api explorer by clicking that link it will come up with the correct namespace the correct api correct everything and i can add my query parameter of parm id let's test that and it comes back with a 200 and it says here is the instance id what good is that well if i take my curl command and of course those are not my credentials and in fact i don't need credentials so i'm only going to copy that part go out to a command line we'll use my terminal program come on thank you come on terminal window and i know you probably can't see that so let's blow that way up and i send that curl command same thing this could be coming from a shell script could be coming from python could be coming from a lot of different places any third party application could call this api and get back that payload right there result there's my result object there's the answer with my id and everything else in it so it's working it works externally i've set this stack up again if i were to try and put the all of the logic from my script include into the script address api it would just make things really complex because now i've got some code in my ui action i've got some code in my scripted rest api and it just gets crazier from there what exactly is this in the code we just saw i believe you're talking about the service not the service portal widget but the script include which would be this guy this simply refers to the current instantiation when you do a new class new glide record new in this case i call it instance new instance it creates the the script include is a map it's a blueprint and when you say new it instantiates that think about a blueprint for a house you can't live in a blueprint you live in an instantiation of that house and you can use that same blueprint for 20 houses on the same street that are all identical to that blueprint so when you go into one of those houses you can say this kitchen which is different than that kitchen okay i like the house analogy and blueprint analogy it's very easy to visualize that's kind of what's happening with classes and objects again not to not to toot the old horn but javascript series all right go over there and uh check that out there is at least one video on that topic and talks about this so great question thank you for bringing that up it's just a self-referential way to say hey i need to call my own function or i need to call my own property of that so that is what this is about or this is what that's about i don't know i've confused myself again where were we going oh yes we were going on to another example let's go back to the pdi and next example is a client callable script include you may have done some of these before if you wanted to do clyde ajax see episode 33 of tech now that's that's where i'm going to steer you for that one maybe i'll put a link in that in the description client callable script include let's go back to our map go back to our map is way up there on the upper left client callable script include is going to call either a ui script that it can use as a library think of a ui script as the client version of a script include okay everything else on this aside from the gold stuff is pretty much server side well and the orange stuff that's obviously happening on portal blah blah blah but client callable script include uses http xml ajax whatever you want to call it to do a server call we're abstracting a lot of that away so you don't have to worry about it somewhere there is going to be on the server a client callable script include that we can use to return information now even better would be if the client callable script include called a lower level script include because now the logic for get info isn't in your client callable script include if it were just in this one just in that dark green box on the left over there similar point to it doing the weatherman thing there's a cold front moving in from canada the uh if you just had it there it wouldn't be as accessible to all the other components so the basic core simple functions go on that library section on the bottom and then the client callable script include not everybody does this i didn't do it originally either but i think i was watching one of stephen bell's videos about this and i said that makes total sense get as much out of that client callable script include into a regular standard server-side script include so yes it is a bit abstract and for a reason so i can take advantage of different components in different places and reduce the maintenance there may be an extra layer and it feels like you're over engineering at times but if you stick with this model it will pay dividends later let's take a look at server development script includes and i believe i have my instance ajax script include yay right there okay what is it doing it is a little bit special in that it has this client callable checkbox whoops let's get back to here this client callable checkbox sorry about that's why i have a monitor up there to see what you see this client callable checkbox says i am now enabling outside client calls to the script include it has an api name and you must call it in a certain way then the script varies a little bit from what we had before we had an instance script include look at that i'll show that to you it just said hey this is a new instance of this it extends prototype this one you'll see this very special line in here that says object extends abstract ajax processor there's already a script include that does a lot of the hard ajax work for you i don't have an initialize because i don't instantiate it what i do have is a function name that i call from the client script [Music] says so in the widget 2 it refers saying not sure where that's going but thank you steve for responding to that just wanted to kind of catch up with the conversation there what i do is i pass in a parameter that drives the function and i say fx because that's the little icon that does it's not special effects it's it's shorthand for function which function do i want to do i can use this.getparameter to get that information from the client script let's take a look at the client script we'll start at the top actually that might have made a little bit more sense let's back off of that and go to client development client scripts display instance url so client scripts many of you are already familiar with these if not i define a table that i wanted to run on when do i want it to go on load on submit on change this was actually on change was it you know what let's reload that since i don't even remember what it was and didn't expect it to change let's reload the form it was unchanged okay if i change the band field back over here my cursor was in the wrong place if i change the band field it's going to run this script and this script says i want you to do a glide ajax call to this script include again episode 33 if you want to go in deep on that i'm going to call this function because that's what sysparm name does i'm going to pass you a parameter you could pass 100 parameters if you want just keep stacking up add parameters farm this and whatever there's no special naming for this says parm name is special the rest of them are completely up to you i could have just called this fx but i wanted to do it this way so i'm going to pass you a parameter url that's just a string and i'm going to do an asynchronous callback that means i'm going to call the function and whenever you get me a response back that's fine i'm going to go do my other stuff let the user go and do what their form usually it just takes a few milliseconds but if it takes a while you don't want the user going my browser's locked up i can't do anything okay well the response is coming back synchronous generally bad except for unsubmit we'll get to that another time i think this get answer is this function down here which does a simple gform.net info message so it's going to say hey server what is the url well we asked that's pretty cool i forgot about that you can open the definition which is already open in studio so i'm going to back off of that that calls instance ajax get info here's instance ajax here's the getinfo function and it goes out and calls my other script include now i know this is a very simple example of getting an id from a property getting it making a url but this could be very complex stuff that has several lines of code in it or could be several functions that have several lines of code in them making it easier to abstract and say i have complete faith that get the get url function is going to work so i don't need to worry about that in my instance function it's already been tested a unit tested the life out of that thing with good data with bad data with good parameters bad print i know it's going to do what it needs to do return errors when it needs to return errors and away we go so this ajax this script include is calling another script include and it's eventually going to return the string to the client script so what does that look like when we look at our little api map means that top yellow whenever i can get this on the screen for you i don't know if that's going to show up or not was it control space control space just wanted maybe no yes i don't know catalog client script other client script whatever is going to show up there all the client scriptable call to client callable script include which calls my script include down here three tiers not that hard to figure out really and it could be even more abstract but again this is the foundation for making the maintenance easier what do you say we go test that bring my cursor back to the land where it should be there it is let's go test that it was on the employee table i believe bring that back down my script was on the employee table if i change the band field so if i go to my employee table els employees go into one of these and change the band field novice to intermediate it's going to go fetch the url which returned nothing oh horrors something happened something went wrong where did it go wrong i don't know debugging glide ajax things are not one of the easiest things to do but the first thing you want to do is check the system log to make sure that nobody freaked out and said you have no idea what you're doing and i don't see that here in the last minute or so that could possibly have been me so the next place i would go is into my ajax script include and put in some gs info messages or i think it's happening asynchronously so i don't know break points are going to work in the debugger but find out that way oh we can't see your screen thank you sorry about that got to push the right button get back to the screen share what i did is i went over to my employee table changed the band and i don't get any url so there's a problem there somewhere i thought i tested this all out apologies but next way to do it would be go over to my ajax script include the one that says ajax abstract processor how can we do a synchronous ajax call if required you would change the get xml to get xml weight okay and it may not be available in scoped applications i don't know if you can save with that i haven't tested in a while the only place i would suggest you do that is in an on submit because if you do an on submit it's going to go ahead and submit your form and then the response will come back it'll be too late you can't do anything with it because it's already moved on and submitted the form that's the only place i've seen any use for synchronous ajax all right let's do one more example i'm going to move on rather than debugging that one because we are running a little short on time this is uh using another script include this time i'm going to use the employee script include which has one function in it called add points you can see that there i don't know why it doesn't zoom in quite as far on some of these add points does a glide record look up on the employee table add some points to that employee it comes back i would like a manual ui action that this time doesn't call a script include in that library it's going to call a sub flow as i mentioned think of subflows now as your component libraries of logic they can do a lot of what you would normally do in a script include i have been using script occludes for at least 10 years and i've gotten really used to putting all of my basic little functions that update records here and do records to that i've gotten away with it now we've got sub flows which are easier to build easier to test easier to maintain easier to watch and say when did that execute we've got execution logs that you can then go in and interrogate and say what was the value of this so learning to retrain my brain from script includes to flows and sub flows just for that reason make it easier for somebody later to come along and say oh i get it now and the two can work in harmony as we will soon find out so i've got this script included and it's going to be called from a flow action not even a i thought it was a subflow but let me look in flow designer checking my notes these were written a few weeks ago so i apologize if my brain isn't quite as fresh as it should be and under actions i've got one called add points it's not even a flow it's just an action that i could use in a sub flow i could use in a flow so this is this is like as small as it gets and yes it includes a script but look at this whopping script called the script include it's really what it's doing so now i've got if we look over at uh i'll try to remember to go back to screen sharing i've got my finger on the button already what i've got is an action on right there on the right hand side calling i can't even reach it my hand gets cut off calling a script include on the left hand side pretty sweet let's go back here and now i can attach that to say a scripted rest api that does that if my notes are correct i should also have something down here in my scripted rest api resources here's add points i've got a resource on my script address api dom over on the right side again okay over on the right side i'm going to use the scripted rest api to call an action to call a script include you can hook these up in a variety of ways which makes it really really cool so i've got my scripted rest api that needs an employee id and the amount of points you want to add and that then this code came right from the action editor check this out right from flow designer after i publish it i could say oh where's my code snippet i didn't save anything but i copied the code and went right into not that one where's my studio again there it is i'll call the flow api get use the inputs call the api blah blah blah and get the outputs and then send the response body back so i get a look at my notes who's calling the script address api um could put it on a widget could put it on a url let's go try it with the url only i'm going to turn off authentication so i don't have to expose any credentials to you on this i need an employee id well there's an employee id chuck currently has 100 points we'll copy the sys id put that in my editor in a temporary place and i thought i had a test scripted rest api there it is i've got this script already in here but i could also get that from the rest api explorer let's go over and test that out uri does not represent any resource okay that was instant's name that was not the one i wanted to test and that is not the screen i wanted to be on i thought i had an employee nope test scripted rest api i don't have another test script let's go make one then very easy go to the browser in my scripted rest api resource i have explore rest api i like to test with the script address with the rest api explorer first i need two query parameters i need my id which was the employee id which i no longer have in my copy paste buffer but i can get again real quickly by going to copy sys id go back to rest api explorer here there was another parameter what was my other query parameter i don't remember the points points that i want to add and i put those in as a query parameter important note it will come out as a string so let's put points 100 we'll add 100 more points to me that's going to go into my scripted rest api that's going to call the subflow which is going to call excuse me this is going to call the action through the flow api and that's going to call the script include again very easy to put these pieces together to make them reusable easy to test easy to maintain and if somebody says well i need a slightly different version you can go and create another api resource but still make use of the same script include you're not copying that code from one scripted rest api to another or from one business rule to another you can make little modifications on these and make different versions of them so when i send that i get a big 500 which says the undefined value has no properties apparently the scripted rest api doesn't work which is why we like to test it in the api explorer something about this is not happy let's oh yeah that's employee id that's why i'm not doing a whole lot of error checking in here but it is employee id i just named my query parameter wrong employee this is why it's really important to document your apis kids send that and now i get a 200 it says this is the new value if i look at chuck's record yes the points have been updated by me to 200 and i could keep doing this all day long if i want to do the same thing and copy it out to a curl command or a python or perl or ruby or put that in any api you want could be a lambda function on amazon that comes in and hits that end point that allows you to continue to use this functionality of adding points through this function and a script include that's called from a subflow why wouldn't i just have my my scripted rest api directly call that script include there may be functionality in the the the action that i need that i don't necessarily need in the script include so i can layer these things up much like task table and then you've got incident incident has a few fields on it that task that all tasks don't need it's specific to that use case um any other questions in here i don't see that apologies for the problem with the client callable script include not returning the url properly but we will get back to that could put all of this into a widget as well if i wanted so in summary let me just check my notes building your api stack with script includes and flows subflows and actions at that library function let me bring that up one more time at that bottom library function really helps to abstract and contain and make it easy to test okay that's the main thing you can unit test them have faith in them that they're going to work as needed and if they don't you can fix it in one place easier to manage easier to debug less maintenance which means you get to spend more time adding value and if you're coming up to someone else's project and they've used this model or something very similar easier for you to understand what's happening and what's going on oh this calls that that goes there this is connected to okay you can you can navigate around this map if you want print this out frame it put it next to your monitor make a t-shirt out of it i don't care it's up to you if you've got suggestions on something i missed or how this can be improved certainly send that along to me i will put you know i should put this image in the github repo as well so you can get that i'll put that in there along with the code so that'll be available in just a few minutes i am done for the day thank you for joining me uh if you've got any other questions about this or other things go over to the community join me next time on the 17th in three days and i will be doing live widgets in service portal teaching a little bit about record watch and how you can make your widgets react to changes to data on the instance it's really fun but it can be really complex there are some things that i ran into that i want to share that i don't want you to run into when you're building live widgets as well it you can get carried away and before you know it your widget isn't working or is working very very slowly as i found out so thank you very much again community.servicenow.com right there is where you can find all of your additional information let me bring that back up on the screen get your questions answered if you don't want to wait for supporter you don't want to put in a ticket or you just want ideas from other people of concepts this is a great place to ask so i invite you to join that and join the developer community and i look forward to talking to you in just a few days see you around take care if you learned something share it be helpful to others be kind be safe and we'll talk to you again real soon take care bye [Music] you

View original source

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