logo

NJP

Community Live Stream - API Adventures - Scripted REST APIs and Service Portal Widgets

Import · Oct 29, 2020 · video

[Music] 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 advocate at servicenow and i am here on october 29th to talk to you about scripted rest apis service portal widgets subflows putting this all together and really making it come alive and walking away with a number of useful assets at the end that are reusable so thank you for joining me glad you're here let's go through the pre-roll real quick if you're not already subscribed and clicking like and all that good stuff notifications go over to youtube at the url that you see there for the servicenow community add that to your stream and we would be happy to notify you much like that so you will see when it's live actually got a notification 30 minutes before you're probably well aware of that if you're subscribed to a number of channels sometimes i subscribe and i forget to turn on the notifications sometimes notifications turned on and go why do i still have this on so you do the right thing for you and we'll keep you informed if not you watch afterwards and appreciate it very much thanks for joining a lot of people checking in this morning on chat the uh mark is there of issue abhishek carolyn a lot of friendly names john people familiar names that we've seen in the past encourage you to join live there's a discussion that happens q a that goes on if you've got a question that goes forgot before i go on we also do this on twitch so i should have a secondary broadcast there if you're interested in that thanks for joining there as well i can't keep an eye on that chat as effectively though so it uh maybe someday i'll figure out a way to get all this rolled into one place and we'll do that if you do have a question that goes beyond today's topics of client-side http calls from service portal widgets and script address apis and subflows i know that's a lot that's a pretty broad range of things and we've covered some of this before if you've got questions that go beyond that please please engage the community over at community.servicenow.com tens of thousands hundreds of thousands of people that are there willing to learn from your question and answer your question and learn from those subject matter experts i am a self-proclaimed not expert in everything i know custom maps i know integrations and i know the platform in general so take advantage of those other people that are out there that know grc and secops and all that good stuff some of them are on the chat right now so please go into the community ask those questions in the right community forum and you'll get the right audience for that going on i want to remind you that the developer portal is out there oh thank you to everybody who attended creatorcon this past tuesday and all that content is available on demand i think do i have a link for that i don't have an image for that on my magic button thing but go over to the servicenow page in fact i'll show you how to do that right after this but developer.servicenow.com is where you can go and grow all those skills i think almost on a daily basis somebody on linkedin is saying where can i go and learn more about servicenow i'm new to the platform i'm new to service now i always steer them here first go through those learning plans learn the basics of servicenow development and everything from adding fields to a form to creating tables to creating scripts and flows and lots of great stuff that's out there totally free including a free personal developer instance and if you mess that up of course you can always go and reset it and start over good to see you oh such encouraging warm words on the live chat thank you very much for all of that i just was glad that you know i remember to unmute the microphone at this time it's any time i walk away from this for more than you know a a day or two i come back oh boy what am i going to forget today that's why a while ago i made a very long checklist right down to click this do that so when i have these kinds of days that list is there if you've got any kind of procedure like that you may think it's silly at the time and go i got this i know this in my sleep sometimes you don't we do this show if you're watching not live if you want to join us live we do this every day not every day mondays and thursdays barring any personal or business matters but mondays and thursdays at 2 p.m utc so you can set your calendar and as you change all your clocks you're going to have to adjust to whatever utc is don't forget to change your clocks this weekend let me see what else have we got we've got uh developer meetups are going on fast and furious over at meetup.com i have not got those up on the screen but i can go there real quickly lots of meetups around the world i encourage you to join your colleagues they're because they're virtual the nice thing is you can join meetups that are not even in your loca local area you can go i've seen dhruv from india on the texas meetup i mean it's it's great because depending if you want to stay up late or go to bed early or get up early or attend these i've got uh two planned for november 10th i believe it's utah and richmond are coming up check it out here this is where you can find out sign up join become involved with other chapters because different chapters have a different vibe there's a different topic going on some have just an open q a discussion for an hour hour and a half some have a specific topic or a demo or they call someone from service servicenow into uh talk about various topics it's a wonderful way to meet and greet virtually and you know what i think we're all living to work learning to live in this zoom world that we've got manny good to see you bach af where have you been uh it's been about a month without a video i have put this series on rest no pun intended for today but i put it on hold while we were working on creatorcon there was a lot of preparation a lot of people came to together to work on those four hours which actually extends again if the creator con content i meant to show that to you thank you for the reminder if you go to servicenow.com and clear the title for just a second servicenow.com i'll show you the easiest way that i always get there because i can't remember long urls underneath events come on underneath events is creatorcon right over here and i don't know what i hit to make it do that should have been something more like that there events creator con and it will take you to the creator con page go ahead and sign in if you haven't already signed in go and register many of you already know what your registration is i am going to sign in it does take a few seconds here this content all of the on demand content will be available through the end of 2020. so you've got another two months to go and check out graphql or process automation designer or re-watch the keynote where i talk to pat casey it's a lot of fun go check it out totally free there's about four and a half hours of recorded content and a lot of the uh the on-demand workshops and labs are done through now learning just like we did for uh knowledge 20 back in may the um we did learn a number of lessons we had a quick review yesterday as a team said what worked what didn't what we going to change and already looking towards next may for creatorcon don't know if that's going to be physical or virtual yet but we're going to have a plan in place we'll let you know so that's the quickest way to get to the creatorcon content anything else mr tomasi um yes haktoberfest is still going on oh look i'm purple and it's purple for a couple of more days this is from digitalocean if you want to create now experience components or spokes i'm working on two or three spokes right now if you get four pull requests successful pull requests they'll send you a t-shirt okay you got to go sign up with digital uh digitalocean i know it's a little late in the game and some of you probably aren't watching but hey it's been a lot of fun i've got to get two more pull requests in and i'm going to do that tonight tomorrow at the latest i've got tomorrow off but yeah i get up early i can do a couple of pull requests in the morning got to get that t-shirt can't let andrew and brad have bragging rights about that so yes we will get to the session right as soon as we get to through a couple more commercials sorry about that cigar uh it's been away for a while so i got a lot to tell you about new episode of breakpoint has come out talked to uh jaron guezz one of our product managers for playbook and process automation designer if you watch the tech now on tuesday the 20th of october that's who i'm going to be talking who i did talk to in breakpoint we've got a number of them coming up and i'll tell you we just had a brainstorming session that goes well into 2021 about some new and exciting topics things you may not have thought about for developer podcast so very excited at the lineup that's coming up for this go ahead and subscribe get it automatically delivered to you like any other podcast from your regular podcast channels apple spotify google amazon a lot of people have podcasts these days i don't know my phone just said yeah what do you want to know you ask my name we do do do do let me check this real quick i don't know if this is the right one or not nope that's not the right one episode 81 is going to be the counterpart to this tn 81 reg is where you want to go bitly tn81 reg we've got another tech now coming up the first one was on process automation designer november 17th is going to be on playbook so the two go together peanut butter and jelly or peanut butter chocolate or whatever you want to put with your peanut butter i generally don't but we will have some code snippets today so check out the url that you got right there bitly for the service portal widgets the sample data that i'm going to use in today's demo cover that good you want to learn more about javascript real quick commercial bitly slash sn-learn dash js the set this has been out since july of 2019 lots of great adoption lots of great feedback thank you very much enjoyed building this so much that i'm working on a new series for integrations i'm going to take you through everything i can think of on integrations whether it's email or script address apis or data sources and import sets or transform maps field mappings coalesce values yeah it's flow designer at actions and integration hub and spokes it's all in there as much as i can cram in there so looking forward to bringing you start of that content in the next few weeks and then it will roll out in various chunks so let's get going i am going to be talking to you as i mentioned about making http calls from uh from service portal widgets now if you watched the earlier episode on uh service portal widgets part one i showed basic how you set up the server code how you set up the html and a client script that did a c.server.update that calls the server that passes the data object in as an input object and then you can analyze that from form data or whatever to say oh let's have the server code do the updating and that's one method to do except everything is locked into that widget it may or may not be what you want to do i'm going to show you a little way to abstract that so that we can have reusable components many applications that you see on the web many web applications come out with their apis before they come out the ui so before you start building service portal widgets you may want to build the logic up first in fact that's how i build a lot of my apps that involve service portal build the table oh that we should probably bring up the diagram that we had before i build up the table and then i start building logic on top of that starting with script includes you know down here and sub flows to do that processing logic add a record update a record look up records do something that kind of logic is my library of stuff that i'm going to do and then to take advantage of it we're going to go up this if i can do this so this path of a scripted rest api and then we'll call a subflow from a script address api and then our portal ah my hand got cut off in the script addressed api i just like doing that on a green screen [Laughter] we get to do that and we're gonna go up that path right above my head so that's where we're going to go we're going to call that from the client script and and the advantage well the downside is it takes a little bit more code the advantage is it's easier to unit test because i can make sure that the script include or the subflow or the action the thing that i'm going to call does exactly what it needs to do it does all the error handling and whatever else it's rock solid i've got faith in that before i employ it to another layer up the other advantage is it's a reusable component that i can use from any other server side script right because if if i've got a subflow and i can call it with the flow api or i've got a script include and i can call it with a new instantiation of whatever i can call it from a ui action or a business rule or a flow or scripted arrest api or anywhere i can use server-side code i can call that logic over and over now the advantage of having a scripted rest api hmm that is primarily for the service portal widget but i can also test that through the rest api explorer i'm going to get to all this real quick in fact real quick being right now that's our game plan let me show you what i've built out for this i've got to find my notes and where i'm where i am in my note okay okay let's get started with my client list i built this out i think oop gotta show the screen show the screen show the screen okay this is a service portal widget that i built for a simple list of clients from a client table you might have users employees whatever okay i just go and grab them with a server side look up i'm only doing five records on purpose right now and i say show me those five people i've got an edit icon that will take me to a form and i've got a new icon that will take me to a form and similar to the way that we do this in the platform is i indicate that i want a new record by passing the id of -1 i indicate that i want to edit an existing record by passing the society of that record so visually i added an existing record and it comes up with a simple form okay first name last name email phone active pretty easy i can save it i can go back so let's say we change abigail's phone number let's go to the standard classic ui here and go to my cls clients table and i've got 3 000 people in there looking at who's been updated and recreated first let's go and create a new one just for fun let's go back got a back button gotta have a back button some way to get back to the list and i say new and i will say [Music] bruce banner and his email is the hulk at marvel comics dot com okay phone number an angry phone number something like this zero one and we'll default it to active so i hit save and it does exactly what you'd expect now it's not doing anything because i don't have anything hooked up prior to this i had a c.server.update that passed this form information and did that but again that's all very static and i may want to update or insert or hmm there's something i can show you something from paris that is pretty cool or maybe it was it was orlando that we got the insert and update okay it doesn't do anything so i've got to hook this up so to make a new record or to update an existing record i only really want one thing i know best practice for http calls is use a post to create a new record use a patch to change certain attributes on an existing record i'm going to violate that best practice and create one api so let's start with the logic first i said we're going to start with that library stuff at the bottom let's go into flow designer because there is a really cool feature that i like to take advantage of if i were going to do this entirely in script obviously you could say hey just go to glide record lookup if you don't find the record then you create a new one or if chuck if you passed in an id of minus one we'll do an insert or there's a lot of ways to do this through a script i don't want to do script if i don't have to do script if there's a scriptless way to do this i'm going to take advantage of it and my favorite scriptless way to do these things is with flow designer and a subflow makes it easy to encapsulate this logic into its own reusable piece so inflow designer i'm going to go to subflow call it create or update client okay let's avoid any issues with permissions we'll put that we don't need a category for this because it's not it it's it is its own scope but that's fine create that my inputs i'm going to need an input for all those fields i could pass in an object i'm just going to be a little explicit in this one so one two three four five it was first name is a string we should probably make that mandatory last name is a string make that mandatory in fact let's make all of these mandatory we will do um for something i say email was in there now i did notice that there is an email type don't know exactly what it's for or how it differentiates but kind of helps for readability right it also will show up in the comments automatically email phone p-h-o-n-e hmm must have got a new keyboard while i was on break this one doesn't work [Laughter] and active is going to be a true false i find it interesting i don't know if you've noticed this but there's like a radar boolean there's reference to boolean in here but there's also true false field go figure right now you can't make a true false mandatory because it's always going to have a value so this is kind of pointless all right outputs what do i want back well if i create a new record i want a cis id but i learned recently don't use the word sis id here it is a reserved word okay so i just call it id we'll call this id it will be a string and i'd also like the status of what that operation was you'll see this when i get to my action this is going to be the whether it was created or updated very important because i may want to return a 201 out of my http for an inserted record if you don't know some of your basic http results codes they're very handy just even knowing the 200s from the 400s from the 500s as you're debugging some of this stuff so i'm going to grab hold of that that will be a word so created or updated you'll see how that's done in a minute now the actions part of this becomes very simple i say action and if i say record created or updated create or update records some systems will call this an upsert i've heard that term before and this saves me the hassle of saying have you seen this record before if so let's update it if you haven't let's go create it it just washes all that annoyance away i can't tell you how many times i've done that in script or flow that says go try to retrieve the record if you didn't retrieve the record then it doesn't exist or create a function called exists that basically does that same kind of thing nah we don't need that we're going to do this on the client table which is down here on my cls app field values you got it i'm going to need five of them so let's do they don't have to be in any particular order first name last name phone email blah blah blah and phone is in here somewhere okay and this is pretty easy whatever was the input for active i put there whatever the input for phone i put there whatever was the input for email you get the idea drag and drop and drag and drop this is way more fun than code now for this create or update i'm going to need something that is an analog to the coalesce value to tell the system when is it a new record when is it an updated record now in my use case i'm going to click email that's going to be my unique id to say all right if you haven't seen this email before go create a record if you have seen this email before go update the record obviously the problem with that is whatever they wanted what if they want to change their email address not happening in this use case you can figure out some other logic to do that so that is my creator update then i need to set those output values and that is under flow logic this little equals y thing and i'm going to set two flow outputs one from the id and one from the status okay status comes from here id comes from society on the client record done and done so if it's a new record it's going to have a new sys id if it's an old record i just see the same society no problem there i like to return a few values always just some indication that it ran and try to think ahead about what something consuming this later may want i don't know if you can like over send information and say here's too much return information i haven't run into such a situation like that but i often run into the case where i need another return value that's it save it let's test it because testing flows and sub flows is always fun first name well we did the hulk um peter parker email spidey at marvel comics dot com phone number same area code same prefix uh we'll make him zero zero two default that to active run the test and if we haven't seen spidey before i'm not even going to bother checking the execution details i'm just going to refresh this we have spidey we have where'd the hulk go oh we filled in the form for hulk we never actually submitted it so there's peter it did not fill in the name so what i could potentially do that would be even better would be go here and add in the name field let's add one more input sorry my font is just a little too large for this i'm gonna add one more field add field name and be careful as you do this because first name last name and getting the cursor in the right place is always an interesting trick here i want it right there sort of space i've seen it where it doesn't add that space okay so if we want to update peter parker and we say there's spidey's address only i got the wrong phone number he's actually zero zero three run the test now that he exists that creator update will say same email address let's go update the phone number i've got zero zero two here i refresh the list and it's 003 here okay that was easy no script i like it flow is working let's publish that hit publish it's ready to go now i need the scripted rest api it's taken a long time to get back to service portal isn't it but there's a reason for this now i have that subflow that i can call from pretty much anywhere just give me those fields and i'll create or update the record pretty easy and one of the places i want to call that from is a scripted rest api which obviously implies script so if i go over here to code snippet and server code not sure why it's grayed out but hopefully i can copy this if not i have one in the oven that i baked earlier copy code snippet to clipboard and take a look at that script address api dot js let's put these away so we don't see all that and it says please give me all this information now i filled in this originally it looked like this said i need a first name last name email phone and active okay all i did was say you're going to get that from the request body data for the first name last name that's how i'm going to pass it in and it sets up this inputs object notice that it's also now using this is paris it's using this new flow api method it doesn't do a start flow or an execute subflow or something like that this is very easy to read i'm going to run the subflow called upsertclient that is not the right name that's not what i called it this is actually one called create or update client let's get the right name in there you put in your correct name i'll put in my correct name and it also put this all on one line which kind of bothers me because i don't like really really long lines i like this vertical format i think it reads easier almost like a glide record query where you can say here's the add query here's the sort by etc order by excuse me so i'm going to run this in the foreground it's going to be synchronous so browser's going to have to wait a second or two while this runs because i want those outputs back and down here i take advantage of those outputs and say if the status was created remember that status field that i passed back it's going to say insert or created and i how did i know that well that was in the execution details let's go look at the execution details for what this said in this case it said the operation was updated ding ding ding ding ding the right there can i zoom in i can there's the updated on the previous one it said created so i want that in my script address api which was over here and if it's just created i'm going to return a 201 just being a good web citizen and so that i can tell the difference on the client so this is my scripted rest api let's go and create that i'm going to copy that code that i've got from my subflow code snippet go into studio in my cls app shut that all down and create a scripted rest api scripted rest api there it is we're creating the rest api first and then the resources for that so you can have more than one resource you could have a get under this scripted rest api you could have a post you can have it update you to multiples okay depending on what parameters and how you call them so i'm just going to call this client submit that and down here change my default acls well i have to have something for default acls that's fine down here are my resources this is where i'm going to create the actual methods that i use to interact with this api this one we'll call observed client just keep it short it's going to be a post and i'm going to create an upsert drop in the code for my script i've got my request object i've got my response object i've got the inputs that are working on that of course you'll drop yours in and it will look a little different but uh hopefully you i pulled that information from the request body i could also get those from query parameters that would get a little long you could get it from path parameters that would be handy as well depending on how you want to set this up we've done scripted rest apis before so i'm not going to get deep into that but i'm going to put that code in there hit submit on that let's go test it out okay so actually what i don't need is these two that will make it harder for me to use in this case so turning off a little bit of authentication still requires authentication but it doesn't require all the specific roles and acl checks and whatnot down here after i've created a rest api resource i can test it with the rest api explorer very handy tool again i'm building and testing building and testing my way up this little api ladder why do i care about this i may have other applications or other systems that want to create clients for me hey this third party system wants to send a rest api to you to create or update an existing client great i've got an api for that if all i had was the c.server.update on my widget i wouldn't have that ability i have to go make a scripted rest api or engage them with the table api or something of that nature and it's just not really where i want to go so why duplicate things when you don't have to so this is a way of giving me lots of reusable components a library of this kind of stuff very simple example on this client table but you kind of get the idea all right i don't have to add any query parameters but what i do need is the body of this so i could do my builder thing here and say first name is going to be uh bruce we didn't do the hulk yet boom boom boom boom okay last name is banner email the hulk at marvel comics dot com phone number one two three one two three two three four was he two or was he three i think he was one can't remember and active is true now notice that it puts active is true in quotes here i am not looking for that in my api you got to watch this because active is one of those crazy things that's sometimes a string sometimes a boolean sometimes it's a number represented as a string keep your eye out for that and you may want to make your api a little more robust but mine is expecting a true false so i better pass it a true false so way out of that in scripted rest api explorer is let the builder do the builder stuff so they don't make any syntax errors and then change this to an actual factual boolean and you'll see in my server code on the widget that it's actually coming out of the databases a one or a zero represented as a string booleans are crazy little suckers okay let's send that and it has not i don't have this email address in my client table yet so it should in fact return me a 201. did it work there it goes 201 excellent my script address api is working i get back the sys id of bruce banner which is very nice and tells me that it was created now i could in fact return the entire bruce banner object to the client that's often done as well when when people send something they expect to get a similar thing back maybe i even get the name field back ooh fully populated with the space and everything go to my client table take a look there's the hulk they're spidey cool we're getting our superheroes mocked up here and it did the full name too which is very nice i like that andy dandy i don't need a separate business rule or calculated value or something to do that rest api i'm fairly confident that it's going to work now let's get on to the service portal widget this is where things get fun because instead of a c.server.update we've got to have one way to hook this up um fortunately oh abhishek might get disconnected well you can always come back and watch i'm going to be here on the internet forever now here is my widget that was doing the form we saw the form over here that is the widget this is what it's made of this is this was built previously this is very similar to what we did on the service portal widgets part 1 episode i've got let's take this in pieces so we can actually see it i've got the server code and the server code says hey if you call me from the list with the new button i'm going to have a sys id of minus one just create an empty object and default active to true that's kind of like a new record right otherwise if you didn't call me go get the record that came from the id okay the id is going to be passed as a sysparm under so underscore id go get that record and populate the data.client object with that goodness and here again is a getvalueactive which comes back as a string of 1. i convert it into a true and false so that the widget represents it properly in the html here's my html pretty simple stuff okay i've got an input i've got a form i've got various inputs for these fields nothing super spectacular here i'm hooking up each field with the ng model so when i change it in the form it changes it in the data model i had a little data dumper at the bottom of this so then we get to the part where it says okay there's my go back button here's my submit button i've got a c dot submit i need to call that client script before that would be just a function that called the server.update and said hey do you have input if you do go update or insert this no no i want to abstract all of that database management record keeping stuff away from the widget all this is doing is get me the record you told me to or show me a new form client script let's put the server script away that doesn't need any more explanation i need a c dot submit i believe i typed it that way and it is going to be a function that did it have a parameter i don't remember i think it did oh it's going to pass the entire object okay so this gets the entire object client now before i forget and i always do almost always do is i need to use the dollar http service and anytime you add one of these providers you have to put it up here http if you don't you're going to click that button and you're going to see an error on the console go i have no idea what this is so you have to say hey controller you're allowed to use this now there are a couple of different ways of doing this let me zoom in on that if i can not going to let me right now ah nuts okay so we'll do this in here give me those files back the client script looks like this this is one way you can do this i was looking through a number of examples on service portal earlier and let me just jot some of these in here you can simply say http and then the method name htv.get http.post and some url and it will go do that now if you want to process that you're going to need a callback of some type a promise that has the either the function in here with a response and then you go and do your function e stuff here that says okay you got your response go process it good good good this happens asynchronously which is why we have a callback in here the cer the the subflow was happening synchronously so we don't lose that connection of boom boom when the http call goes out and comes back he says i didn't get anything well if you run your flow asynchronously it says yeah i'm going to go do my thing and return when it okay let's not do that i am hooking these up intentionally this way you may want to do a little experimentation of your own find out what works okay that's one way of doing these there's also an example that i saw that had something like this success and then it called a function by name they quite literally looked a lot like this they constructed their url and then it was called uh my success or something and then down here there was a function called my success okay blah blah blah blah blah if i can spell my functional that's one way of doing it i don't prefer to do it that way because it i think it it it reads a little clearer your mileage may vary your methods may vary your standards may vary that i have the method in this object notice this is an object and there's an object within the object if you choose you can set it up out here and say var my http object something like that if you feel that's easier to read go do it that way too it's all going to produce the same result and then in here you would pass my http object okay different ways to do it this is the way that i've kind of standardized in mind let me back up a couple more undo's here get through all my typos that i made there it says what is the method post get update put patch delete that kind of thing what is the url you want to use well that's straight out of my api what was the api i don't remember where's my chrome browser this is where i have to go back to the rest api explorer or go into my scripted rest api there it is it's slash api upsert did i get that right try that again back to the editor yeah okay so that worked client upsert just in case just in case i'm going to paste that in good no change any headers you want to pass in this is where header parameters are authentication or something of that nature comes in if you want to call this http request this way you got to put in the headers i almost always put in content type and almost nearly almost as always put in accept tells the format of what you're sending content type and receiving accept okay very handy application json is probably going to be your most typical way of doing that then the data object of my client comes straight from the form you may choose to add additional things but this is going to be another object so we've got the request object we've got the header object within that and then we've got a data object in the request object as well see how that falls out there's a lot of stuff going on here but if you're familiar with json and i hope you are from watching other videos that how the request is all put together then i've got two parts to this there's a then which is our promise that when you get done go do this and i can determine a success or a failure i like this because just by naming the function success it's going to run this whether you name it success or not if it was successful it will run this and right after that one of the only places i've ever seen this is the curly brace curly brace comma another function so function comma function there is a way to detect a success and a success is anything that is not a 400 or a 500 okay that's a 400 you're going to get things like 404 not found 403 unauthenticated or authorized authorization restricted whatever the you know 400s are you didn't get in and you couldn't find it 500 is something happen on the server so i like to put in little messages add trivial message is one of my favorites it's that green message that goes up and that goes away as opposed to ad info message which we see on the community and i've put in an idea on the idea portal to please no it was member feedback please change that post posted or saved or updated message because it just stays there and i don't want it to stay there i have to go click the x okay you get the idea that's the difference between add trivial message there is no g format trivial message which there was but there isn't but there's an sp util did i add sp util up here yes i did in this case so let me take my submit function here just walking through that we've got the different parts if it was updated it will take us back to the list using dollar location you might choose to use dollar window this was just something i was goofing around with before if it fails it will show severe error so if there's something wrong with my scripted rest api or something else of that type it will bomb out and die let's take this bring that over to our widget editor put this in here now i said dollar scope i've learned recently that c dot submit is it's effectively the same thing but it's the more preferred way and if you call it from here as c dot submit you'll run into fewer errors i was doing something this past weekend that dylan lindgren one of our sc's very smart service portal scs in australia sent me and it mine wouldn't work until i put in the c dot function name i don't know why okay we've got our function that's going to call this and let's save it believe we just need to do a reload on the client form we'll create a new record tony stark iron man at marvel comics dot com phone number he can be all zeroes it's going to be active i save that and it takes a little while because it's got to go to the script address api that calls the subflow that comes back and does this probably not the fastest way to do this but maybe we could do this asynchronously did i hit an error this is where it's always handy to turn on the console log notice that i'm doing this out here rather than in the preview window on the edit i had preview on can't see the console as well and it doesn't allow me to navigate between two different pages so let's go check turn on the console and it says sp util is not defined see i do that a lot so let's go up here put in the provider for sputil save that try the client form one more time oh i have to type this all out well the worst part was that let's copy that refresh tony tony stark someone else is gonna have to come up in the chat and give me some more marvel superhero oh we got thor black widow iron man now my imagination's working morning must be getting on go a little bit more save that location is not defined you too will experience the excitement of missing providers did i miss anything else location header sp util window we got window gut scope okay that should be it hello from morocco there's a there's a trivial message that you just saw up on the top you hit say i hit save it shows up it goes away sp util get trivial add trivial message excuse me one more time tony stark got that and now he's not going to show up in the list because this is the first five sorted alphabetically but we can go over here and see them sorted by date created there's tony stark now to make sure that our same api works worked fine through did we test it through the rest api explorer i don't remember if we did but this will be our real test let's go and change tony stark um we kind of need tony stark's id that would help because we need to put that on the url because i don't have tony on here i probably should have done an order by on this list by created or updated let's go hit uh abigail but we'll swap out abigail's sis id with tony's id there's tony we changed him and he said um i'm not with marvel anymore iron man's going to dc oh wait you can't change that it'll create a new record actually that'd be a good test maybe there's another here tony starkey we'll make another one over at dc that'll be fun of course with a different phone number should create us another record now that one says updated obviously i could use my response code to put the right message up there i did not let's see we should have tony starkey iron man at dc iron man yeah that would be no copyright infringement there but sample data oops i want to go back to let's do new but instead of minus one this time we've already tested that put in the marvel tony stark not sure what i hit there but it was a big mess on the url marvel tony stark and change his phone number to you know just for fun 911 911 okay it's save it goes back to the list automatically let's check our client record did we successfully get the marvel comics updated phone number okay i know that seemed like a long way around to do a form insert or update but think about it i've only got one api to manage okay probably not the best way to go but you decide i'm passing the entire client object in first name last name email phone number active that's what the widget knows about and if any of those change it will update them accordingly if it's a new record then it's smart enough to say oh you're coming from a blank form this id is -1 actually you know what my rest api doesn't care the subflow doesn't care it just says have i seen this email address before or not that's the key that it uses to create or insert new records so very familiar topics from other places that you might have been like import sets transform maps coalesce values to do that sort of thing but it makes this totally reusable yes this is a little cumbersome to write i often have a template for that i copy and paste it everywhere what's the method what's the url what's the data i'm posting or what are the query parameters add those to the url pass that and i don't have a data object for gets delete same kind of thing is i need to do delete delete operations very sparingly of course as with anything in servicenow with that involves deletes this this piece just says hey when you're done go back there easy enough okay that's what i've got all of that code will be in the github repo in just a few minutes after i shut down the camera thank you for joining me again reusability it's it's a wonderful thing create those subflows or script includes to get that base logic going call those from script scripted rest apis and then call those from your client script this is the way i do the majority of these now why would you want one over the other well if you've got a need for those reusable components or if you forecast that you may need those in the future build it in this layered technology if there's something in the widget data model that you need to preserve maintain continue to count then the c dot server update or c.server.refresh may be the way to go for you i've got a couple of instances where one works better than the other if i'm passing massive amounts of data this very long list of objects the input actually worked well because i already had that in there and then i can pass that to a script include and it gets refreshed automatically i was writing a big list of a big list of records it said here just write this list out somebody modified it on the widget i want to save the current state boom it's all there if i had passed that it could have been done through the http but it would have been a little clunkier because i still needed to do a refresh on the other side it just it kind of got a little crazy so there are opportunities think about do i need that logic to be reusable or is this exclusive to this widget and not really likely to take a big long list of objects or whatever it is out of there so don't always go for the easy quick c.server.update it may lead to increased maintenance and less flexibility down the road hopefully that's the uh all the information i've got on that one appreciate it i will be back on monday november 2nd with the next episode where i'm going to be covering fix scripts so there's a few things that i learned about that i'm going to try and resurrect this information that i started writing back in july there's a lot of good stuff coming so fix scripts i'll tell you what they are when you can use them what they do when they do exactly what you want when you have to run them manually all that good stuff on monday until then take care be safe be kind and i'll talk to you again real soon thanks for hanging out with me bye [Music] you

View original source

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