NOWCommunity Live Stream - API Adventures - Script fields

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] good morning good afternoon good evening wherever you are and when every well welcome to the community live stream my name is Chuck Tomasi from ServiceNow and this is Monday June 29 2020 the topic today is script fields and I'm going to be bolting on the glide scoped evaluator class to show you how to run the scripts from those scripts so something I learned a few years ago that I found very very powerful also carries with it a little bit of risk we'll get into that a little bit as well thank you for joining me today it is my pleasure to bring you this content this the inspired by the questions I get on the community to give you the answers behind the answers to give you the journey of discovery you can take these skills of reverse engineering or whatever it is bring those to your organization to be a more effective administrator and developer on the now platform so once again thank you for joining me welcome to all the people that are out there while they're just flooding it on the chat so thank you for joining me on YouTube live where is my youtube caption there it is and to look around it's around here somewhere YouTube caption if you haven't already done so don't know where you're watching this it may be through the community it may be on Twitch it may be who knows you may be listening in the background on somebody else's computer so thank you very much go over to YouTube slam youtube.com slash user slash ServiceNow community click that like button if there's something you find helpful maybe mildly entertaining in here click the share button if you want to share this you know what to do subscribe notifications the whole thing on YouTube lets other people know that this is important and informative and got some good content there oh I'll forget the twitch twitch TV slash now community is our back-up plan in case YouTube isn't working so well today but let's get on with it we also invite you to go over to the developer portal get a free personal developer instance running any of the latest three releases Orlando New York and Madrid I think that's what it is right now and you know about a month we're going to be starting the early access of Paris so get signed up this is where you're going to get your hands on Paris first the documentation will be released the PDI's will be available to be upgraded if they haven't already you can go in and upgrade it yourself start playing around some of those new features learning plans are all being updated thanks to Ben and Mary Ellen they are working hard to get all that content up to date so that you could explore those new features and try some of these things out turn on some of those plugins on your PDI great stuff that we've got all for free over there of course all the api references many of which we'll be talking about today are going to be available there as well developer dot service.com is your home as a ServiceNow administrator developer to learn more to build your career to get smarter to dig into those things and play around on your PDA and you know what if the if the PDI goes and you say you know I just don't like it it's not working right anymore wipe it out start over there's zero risk to your organization to do that so good morning to everybody Alan and Aiden and Nihad grew it everybody is joining in thank you very much for watching this it makes me very happy that you could join me we do have an upcoming tech now on the Paris release July 28th I believe it is I will get a social media card up here and the registration link as soon as I get it hopefully I'll have it this week or early next week so we could start sharing that and get you signed up because if you miss the tech now that we do live on July 28th you will have to wait until mid late September maybe even early October before I can release that publicly and you can watch it on demand so don't miss that I wanted to make sure you're aware of that that it is exclusively for developers in that early access period and that's your window to get that good information as I mentioned I'll be doing some JavaScript today but hey if you're not up on JavaScript if this is in your bag yet please go over and learn from that link bitly /sn - learned jas and walk yourself through some of these completely free courses this is not you know one of those $100 packages on udemy or Linda this is free did this out of the goodness of my heart for the community to bring their skills up to where you need to be to enroll in maybe the ServiceNow scripting course maybe the application developer course maybe you just want to play around and learn on your own this is a great spot to do it and the script that I do right today will be available on github at that link bitly /sn - CLS so all my instances are offline today so maybe an issue with the AWS I could be the PDA PDI's are not hosted on AWS however they are posted in our own data centers there could be some maintenance going on it might just be patch time so PDI's do go down for maintenance occasionally that's part of the cost you pay for getting a free instance is we got to do maintenance it's not a guaranteed you know 99.999 uptime because it is free you know if you wanted to pay us a bazillion dollars for a URL you could certainly do that I'm not sure how I hit that we were over on this screen and really need to go to this screen okay so if we have a data center down I would be very shocked that that happened and our people are not they would be on it immediately so apologies if the PDI is not available right now if you are having it is take a look in the community here I'll show you real quick in the community under communities there is instant help in excuse me instance help is where you want to go so I go down here to all forums which is at the bottom of this list and in there you can find I can never find it fast enough so I'm going to say instance help up here in the search bar and there is a forum called instance help if you see a common thread that says my instance is down my instances down - is it down subscribe to one of those it's C so you have what's going on okay status is offline it's it's not connecting update ten minutes ago one minute ago okay I'm not saying you have to put a post in here you can you can if if you want this is where the instance help people live monitor watch respond you get the idea so if you're having problems with your PD I little side note there since that seems to be what's going on in the thread today live stream I'll try to respond live okay into today's topic we're going to be talking about script fields how do you create them what's the difference between the script fields how do you make a script run in your script because there's often times that you need to run a script in a script I know that sounds a little bit of an inception moment but they are they are very powerful let me give you a use case where I use one I am importing data from one data source through a REST API and this data is coming in but it may be coming in from different vendors different providers and they have their data formatted slightly different so I need to parse that out different per vendor generally they're usually pretty consistent let's say you know it's it's its name serial number asset number well this person uses commas to separate it this other vendor uses pipe symbols this other one has dots in there and they're in a different order you can configure a vendor record so to speak or a supplier record to parse those differently per provider and normalize it on your own data so I will show you how you can interact with these scripts and not only just run the script but pass parameters into the script field and have something return out of it so lots of interaction you can do with this making it extremely powerful to put this in let's begin at the source and what I mean by that is let's go to studio and look at our data model I have a pre-existing application that I built I'll have an update set for you if you want it but it's very very simple I have a lab record it's called lab 4 it was a 20-17 self-paced lab that we had at College 17 and in here we have just a couple of fields you can see we've got a number field which is a string typically you know your usual number numbers and have a user field which is a reference to the SIS user table nothing magical here very simple I am going to add a script field so that I can put some code into a field and run that code later so to do that very easy we go to our little table editor and I am going to unimaginative Lee call this script and when I use a script datatype there's two types and I know some of you have seen this and you probably scratch your head and say what's the difference between a script and a script plain I thought I knew but when I dug in deeper turned out to be a different answer than I originally thought so the script plane field is the standard script editor that we're all aware of I thought it was script plane was oh we just don't have any icons it's a plain text field there's no line numbers there's no indentation ah blah blah not true and one time I think it was that but it's not anymore so that might be where my falsehood came from was from 2008 2010 era lots of changes on the platform that they don't tell me about the script plane is this field that we're all used to let me just show you real quick I'm going to believe it defaults to very very large but let's just an update and see what we get I want to go through just a few things that had me scratching my head here is my script field to defaults to 8,000 characters that's pretty darn big you will not hit this trust me very large script includes and whatnot use this and they don't run out there it's basically unlimited and I think Steve from Sweden had this question about it what's in between a thousand and a four thousand I think what you get past 1000 it's pretty much unlimited so don't don't quote me on that you might want to do a little of your own experimentation I have I have exceeded the limit on a stream field I think I've got that for another I think it's 32,000 is where you start running into problems but again we'll do a little more experimentation and get back to you on that anyway back to the script field script field created you should know what this looks like if you've been doing any development on ServiceNow for a while I'm going to go to my Lego app there's my labs records and open one of these and it did not put the field on the form so of course we can always do configure for up there it is it just had to draw okay pretty familiar stuff you've you've seen this before right script field line numbers hello world all right we've done this a million times put things in here like GS dot add info message and say table has a script right congratulations you created a script field what good is it what can we do with that before I get into that let me show you the difference between script and script plate this is script plain okay right click show script plain you can change this I don't recommend changing data types often but there are certain ones that are safe to go between type A and type B if I change this to script and update that you will be shocked as I was did I save this I don't remember saving it let's just save that real quick to make sure that we don't lose our wonderful script that we wrote in there and of course that also reloads the form you go what's the difference Chuck the difference is over here notice this little Chevron thing you've probably seen it before if you've ever written a notification and it brings up the little field chooser where the fields well just like condition fields that we covered a few weeks ago you need a dependent table it doesn't necessarily have to be on the phone form it even could be a static field but you need a table name field to get the field pick her in here so if I wanted to get a scrip filled this this doesn't be absolutely no good if I don't have a table it's just a little icon out there and people are gonna go what's that all about okay you and I know that it's about a table field so if I wanted to truly take powerful use of that and say let's create a table field called table name of type table name excuse me and then I need to do the second step again just like condition fields I need to create that dependency to say hey mister script field you are going to have an advanced view gotta go there first you are going to have an advanced view and then under the dependent field tab that one there I click that say use dependent field and select my table field all right making that really big so you people watching on your mobile devices could see this update that put the zoom away and now that little triangular gizmo is going to be attached to the table field again it doesn't necessarily need to be on the form but just for demonstration purposes it needs to have a value if I say incident is my table of choice today it will then populate these with not just the incident fields but you also get some of the common glide record glide element glide system glide aggregate some handy things to put in the script field so if I wanted to do my yes add info message ooh these are interesting I could put G s that debug in there and it would say here is the scope of this the size of this thing okay and I got a drill in there too much but I also get my fields and say let's start with this one it puts in the field names for you you don't have to remember was it called condition was it called filter was it called query what was that thing called you just dump them in this way very similar to what we do in notifications but it's not putting out the label and the curly brace on the dollar sign and all that good stuff so that's the difference between a script and a script plainfield 99.9% of the time I'm pretty sure you want a script playing field so let's go back since we don't really need a field picker chooser unless you want that I don't know but again my gut says most of the time you don't let's unzoom on that we don't need that anymore and put this back to script plane script play just wanted to clear that one up because that can be a bit confusing there are a lot of different data type a lot of different field types that you can choose from and on my table I no longer need the table table update let's get rid of that and go back to where we started very simple very simple form with a very simple script in it okay I am also going to put something in here for let me get my labs records come on put something in here for Beth a little script for her GS dot add info message Beth has a script to my colon copy that because we're going to take that over and do something similar for Fred okay this is Fred script Fred backslash so we don't mess up our quotes close that out an update great so we have some scripts what good eyes what good is that I need to bring up vs code because it has a very easy sample I want you to follow along with hopefully we didn't hit another patch update on this yeah we did that's nice there's always release notes please pay attention to them file open folder let's go get the scripts for today's show Google Drive now community I'm gonna put it in tempo if I actually did move it in here I do have it in the repo I haven't committed it yet so you won't see it until I'm done with the show hahaha okay let's look at a UI action that can actually do something for us this is where glidescope evaluator comes in you instantiate the object new glidescope devaluate ER a lot of times it's just called evaluator you can call me you can call it X you can call it Fred I don't care your variables you are naming structure then I use that to call the method evaluate script and what you do is you pass it a glide record and the field so it knows which script on which record to go run now some of you who've been doing JavaScript for a while may remember eval eval is sort of the great-great great-grandfather of this and it is very insecure do not under any circumstances use eval or you could set yourself up for all kinds of craziness okay glidescope to evaluators a lot smarter about what you can pass in what you can return what you can do with that please if you can't figure out how to run glidescope devalue or do not fall back to eval I'm just going to put that out there as a very strong warning eval is evil Wow you could almost make a t-shirt out of that with one letter change then of course I have my action set to write to stay on the current so this is simply going to run my script let's go create a UI action with that so back to do do create server you action I'm going to call this what do you think run script that ought to be helpful and it's going to be on my lab table that almost sounds like a place you want to work in the science laboratory set up on my lab table don't have an underscore lab I do I should have just typed Lego I don't need an action name I'm not doing anything with the client script don't need to show this on insert but I do want it to be a form button and the condition I'm going to leave it alone well you know what we could put it in a condition that says the script is not empty that would be helpful but for the meantime I'm simply going to instantiate the glidescope evaluator and run the script okay that's all it's going to do submit that bring back by form here's Abel's record there's my run script you know what I could probably even make that a little more alarming you might want to restrict this to like just admins or something and reload that we now have some form styles primary and destructive and yeah so read script pay attention and if I run this it runs Abel had a script this is the script from his record I did not put this into the UI action you saw that and if I go to say the next record and run the one four-bath run script it says Beth has a script too so I can run these different scripts now this is where it gets a little scary because you have to be very security aware of who has access to edit these scripts there should be somebody who knows scripting on the glide platform because all somebody has to do is come in here and with the right permissions and the right croscill privileges they could put in a glide record delete multiple and your data is gone okay but this gives you another way to make your apps data driven the gripped is data at this point you can potentially give and your change control processes and procedures and policies you could modify this in production I don't recommend it you probably want to modify in dev or test and then bring those changes forward because it is script and news changing the behavior of your application but it's available in prod for you to see and let's go one step further make sure I'm following along with my notes and I didn't miss anything okay let's go one strip step further and ask it to pass in an attribute I want to want to get some parametric information cuz that script is all fine and dandy and everything but what if I have that vendor that's sending me some information and I want to parse that information or I want to average up the number of incidents assigned to able phred and whomever so I go back here to my files doo-doo-doo-doo and I get not open the folder cursors let's go to file open and open the folder not just the file that will get me a lot more mileage out of this exercise that I'm going to show you that opens up everything let's go to bethe script and show you something modify that so Beth script number two has tasks count what what is task count it's not identified in this script but what I'm going to do is pass it in you know when you look at a business ruling you've got access to current previous add there are some other things there in flow designer and you've got inputs and outputs there's those parameters that are passed to you that are made available to your script you like where do these come from okay this is kind of how they're done okay with this background here you've got a script I'm going to run it this is how we're going to do this exercise so let's save that it has an assumption and this is part of when you design an application what is that API that you want to use for passing things in and passing things out well since this script is accepting tasks out if I ran this right now it would go I have no idea what that meant and my log would show that it tried to run the script and went what is TAS count again so you didn't tell me so I'm just gonna throw a big old error it's undefined it may even say the word undefined in there if it's not defined in this case it might not gonna work for you today so what I need to do is set up my UI action to pass that in and to do that I am going to update to UI action number to do let's walk through this same instantiation this time I'm going to use a script include that I built that does a task counter let's go look at the script include for that real quick in studio underneath server development script includes I have this lovely bit of artwork called task counter in my utils does a glide aggregate you pass it a user ID Fred Abel Beth whomever you pass it there's this idea that goes hey Fred's got 132 tasks Beth has six abel has 15 whatever it comes back with it's going to return an answer my UI action is going to take the output of that which is going to be user tasks and now look I use a method called put variable where did I get that I got that from the docks or the developer site you go look up glide evaluator and it has evaluate script which you've already looked at takes a glide record and a string field and you can also pass in some variables this way you can also do theirs get variable but I'm down here on put variable put variable string name and value so I'm going to pass in a string name it gives you a little example put X's 100 put Y is 200 very simple assignments I'm going to say tasks count that's how my script is going to know what task count is and user task is the value then this is just exactly the same go run the script on the current record okay so let's take that put variable is my new great addition let's go to studio take my UI action and update this kind of mist but boom done so it's calling my utils getting a test counter update that I don't need to rerun reload the form because everything is server-side on this when I run the script it says yep you got a read operation that's nice that'll come up once that's just your cross go PP eyes and Beth has 318 tasks very cool what if I took notice that it didn't it's also using some information from the current record so it didn't say Beth literally it said Beth from the script very cool let's go try that on Abel where's my lego lab lego lab I think I called this Lego cuz I'm putting pieces together and I was just kind of fun I did this with Abel save that run script now it says Abel has zero tasks makes sense you don't assign tasks to someone without I tell rolled that kind of thing on default out of the box demo data so that's true it's working with this script include to retrieve information now let's take it one step further and pass information from this running script here's where you might want a parser for each vendor another use case where that's the use case I was using it's getting this information in and saying you know what everybody's got their own way of doing things so I'm going to solve that with regular expressions replace split trim cut doing all that good stuff and massage this information into an object that I can handle that the rest of my script is going to expect in this format so I can normalize I can standardize that information to do that is this available in global scope yes yes it is because I know that the docs page that I had said scoped if you also type glide scoped evaluator it's called the same thing there is a global page as well so global glidescope evaluator filter that down to same methods same use case just realize that it's there okay that being said keep in mind if you're trying to run scripts from one scope to another you're gonna have to be concerned about Kruskal privileges if I'm running a global scope and I call my utils you're gonna have to prefix it with X underscore uh dot of my utils all that good stuff about running scripts across scope good question thank you passing parameters from this script on my record back to save my utils or something else let's take a look at how that's done and yes I have another pre-baked script so start out the same way Glide evaluator calculate the user tasks put that in a variable nothing changed there run the script and now there's a get variable it says I expect that script to have a variable called answer or result or average or whatever you put in here you can have multiples in there it's going to as you're running your script you're gonna be calculating or doing something I like to return objects because it gives you a lot more than just one value but you do what you want to do it's going to extract the value of that into this answer and it says look if it didn't work if your script is completely flat-out busted and it doesn't have an answer I'm just gonna throw an error message oh you did it wrong you go check your script otherwise it gonna crash okay if you try to manipulate something that's not defined it's gonna go yeah not fun if it did retrieve an answer so i smart enough to put a little error checking in here and then of course there's a redirect to stay on the forum let's take this and first of course we will leave this script with no answer because we want to check our error checking let's put this into our UI action sorry about the scrolling and paste and save and away we go okay we've got our UI action what happens if we don't have an answer in here and we say run script and answer variable is not defined it still prints out the message from the script because it expected you to do that and gives me another cross goalt message so if I run this again it's going to give me just the pink in the blue going mmm Chuck you didn't do something right well let's have some fun with this and say answer equals ten save go save it run script and it says able a zero test answer is ten so whatever you put in this script for example let's take red here and update Fred's record doesn't necessarily have to be Fred because I think it's dynamic anyway and we do % tasks okay there's another method in here called task percentage and I am going to say here I should have a third version of this UI action which I already showed you yes yes I didn't do another put variable I still have task count and I have that information okay it's going to calculate this and display it and return the answer so let's get that up there run it and it says answer is Fred Lunney has 0.4% of all tasks that is a heck of a number and if you want to change that to a smart-looking number you do to fixed and put two in there save run red has 0.4% of all the tasks so not a bad days work we now have a way of extending the functionality again security is paramount in this make sure you understand who can write to these scripts because again all I've got to do is have the right ACLs and the right cross go privileges to say guess what var gr hickel's new glide record incident and gr dot delete multiple and next time somebody runs this bye-bye old my incidents not cool alright so you you could put a business rule on there maybe to scour your script it's just text you could do hey if there's an index of delete multiple don't save this that's that's one option you could use to prevent yourself from having somebody put in the various code lots of different ways you can you could approach that very powerful let me see that I got everything I wanted to talk about very flexible if you trigger an integration perhaps when I run this script go run this outbound REST API or something like that get the information back here could you do it synchronously that requires another discussion about events and that kind of thing so yes if you use it if you trigger if your script triggers an event or your UI action triggers event that goes and gets this information you could do this but I'm not gonna get into that here that way if you have an integration that runs more than a few seconds you're not holding user hostage while you go and run that script these are meant more for hey I'm going to do this real quick and be done with it or I'm going to use this at some piece of my structure it's going to reference this lab record in this a case go run the code and use my answer for something else that generally happens in the background because some of these things could take a long time again documentation referenced from Docs or from developer dot Service now.com over at developer.samsung.com alright we there it is I had to go get my Bell and again thank you for watching today appreciate it very much if you learned something go check it out and go share it be helpful I will not be doing a show on Thursday July 2nd no I normally do these Mondays and Wednesdays me Mondays and Thursdays at 2:00 p.m. UTC service now has holiday for 2nd of July and 3rd of July so Thursday and Friday are service now holidays and I will me will not be doing this but I will rejoin you next week on the 6th of July for an episode I haven't determined yet I've got a couple of ideas I'm just gonna pull together what we want to cover and I don't want to explain it it might be a lesson learned there's a whole lot that I've been doing in flow designer and I've got these little tidbits that are coming up so that's my lead horse at the moment in this race so possibly flow designer possibly something else but I look forward to seeing you again Monday at 2 p.m. UTC July 6th for the next episode and with that I am going to bid you a fond farewell take care be safe be kind and you will prosper as will others thank you very much take care [Music]
Info
Channel: ServiceNow - Now Community
Views: 1,335
Rating: 4.9354839 out of 5
Keywords:
Id: 9ifArzAI0BU
Channel Id: undefined
Length: 36min 37sec (2197 seconds)
Published: Mon Jun 29 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.