Creator Toolbox - Flow Designer Scripting
[Music] do [Music] hello and welcome to creator toolbox uh this is the show where we explore servicenow tools that developers can use to make development easier for you before we get into anything let's start with some introductions chuck hey my name is chuck tomasi senior developer advocate at servicenow been here since 2010 was a customer for a couple of years before that and a lot of history across a lot of topics in it going back into the early 80s it is my pleasure to be here my specialty is in creating custom applications and integrations and just general platform topics so i love this show to bring you the tools you need to be an effective developer brad who are you all right thanks chuck my name is brad tilton i'm a developer advocate with a developer program here at servicenow i have been developing on the platform since 2008 and my specialty is also doing custom app development uh as well as kind of front-end development across a few of our tools over the years um and uh excited about today's show where we're gonna get into some flow designer uh and uh some inline scripting and flow designer uh so without any further ado i'll turn it over to chuck and tell us about what we're doing today yeah you bet you mentioned some scripting and flow design and a lot of us think of flow designer as a no code solution but there are places in there where you can inject some code for for those specific needs or as we'll show you you can create your own actions to make repurposable routines if you like to to run that over and over it's just i i'll tell you i have really gravitated towards flow designer and then it as a as a way to build those subroutines as actions as subflows as flows this is a really creative way a really fast way to do this and debug it i had to go into some code that i wrote a year or so ago this weekend you know rather deep javascript and it was it was tough to remind myself of what the logic was why did i do this what are the apis what are the function names what parameters do they take scripting is just going to be a little harder and if you if you have the choice anytime between doing something in script maybe it's just looking up records or doing it in flow or any other no code solution always always look at the no code or the low code solution first you'll thank me later i i especially all you old crusty people like me that have been doing code for decades it's like oh yeah but i could just crank out a script to do that no trust me look at the low code stuff because you'll hate yourself later when you go what was i thinking here and and then and then they shuffled the deck and go no glide query will do this easier than glide record now you're now your scripting brain just goes into this [Laughter] we don't want to mess with you too much uh what i've got today is a simple parts management parts order system so i've got an order table and line items i'll show you that in just a second on the screen share but we're going to do some automation of calculating some values very typical thing what you do price times quantity equals amount on each line item rather than having somebody sum that up yes you could do this in a business rule yes you could do this as a calculated field but i want to show you these capabilities in flow designer and i think you'll you'll enjoy them they look they practically write themselves i did this on build with chuck during creator con there was one point where i had to inject a little bit of calculation into a field and i mean it was like hit the dot hit the tab hit the dot hit the tab at the dot hit the tab okay i just wrote script well i'll show you that in a second it's real fun so why you know instead of promising let's deliver let me do a quick screen share and all right find my screen i'll give you a count in when i'm ready to hit that screen share button range share in three two uno and hit the share there we are so as i mentioned we've got our simple order i've got a couple of outstanding orders here in my parts management i've got orders and each order as i mentioned has line items so i've got 325 12 centimeter led panels for a buck 25 but there's no amount and you can also see that this amount field is editable probably not something you want to do you want that to leave that as calculated and then i've got a total order amount up here now yes i can use the list controls on a lot of these lists if you haven't looked at this before i encourage you to configure a list and go to list control and oh i'm sorry list calculation totally messed that up so under the amount i say configure list calculations and up comes this nice little modal that says what do you want you want the total value minimum maximum or average and you just check these off and what it does is puts a nice little sum at the bottom of that amount field and you could do this for any of the fields that have some sort of numeric value with them uh you could i don't know if you can sum up like a reference field what happens there but we're not going to go there today so i've got my line items and this is one of the interesting situations where i put the line item number at the end don't ask me why it just didn't work when doing an embedded list just from a user experience standpoint you want to enter quantity description price very fast boom boom boom next boom boom boom next uh having the number in that first field is just not helpful to the data entry piece so talk to your users about how they use the system blah blah blah that kind of stuff let's get into what we were going to get first thing i want to do is sum these up so let's first make the amount field read-only i don't want humans touching that it's a calculated value deal with it so to do that i'm going to create a flow and i'm going to do that of course with flow designer so let's go to flow designer by typing flow designer up here and i have a shortcut and under process automation is one also up comes flow designer let me remember to zoom out on that so i remember later and i will create a new flow and we'll give this a name like line amount changed application list is loading loading loading derek says the uh the sum of a reference field is a list field i like that that's true could be we might have something there okay first time in flow designer i'm going to skip the tour and don't bother with me again this is my pdi i purposely set that for another demo to say look flow designer is very useful if you've never been in here you can take a tour of it and learn how it runs i'm not going to give you complete overview we have other videos on that the first thing i want to do is add my trigger i'm going to say whenever a line item record is created or updated because i want new records and changed records to reflect this let's get this out of the line table of course there are a million things that say line in them so i'm going to cheat and look by scope first uh 66238 parts line and parts order there's my line item table and you know i don't need every change if somebody changes the description i don't need to recalculate the amount so let's add a filter on here very easy to do if the quantity changes or the price changes that's the only time i really want to update that so change that and then it does are those fields uh mandatory they should we add another empty check all right they are if i remember right let me drill into one of those yes quantity and price are mandatory what happens if you enter something here and you don't and you add a new row without one nothing here i've never done this on an embedded list so will it save that oh ouch mandatory that's dictionary level mandatory now okay so embedded list may not be the best thing here all right i'm not going to get into that right now we're here to fix a different problem but yeah you're right we probably should do a not empty check so but it's multiplication rather than division if you were doing division you don't want to divide by zero and also important run the trigger for each unique change or for every update i haven't figured out what the difference between these two is yet do you know brad that's a great question i don't does unique change assume that a field changed maybe i i don't know or if you change two fields is that two unique changes and it runs the flow twice i don't know we're gonna need somebody to give us an answer on that it just bad description in my opinion so i've usually taken for each update and no problems okay that's our trigger i'm going to save save really save often there's two types of people in this world those who have lost data and those who are about to next we just want to update the record so if the price or quantity changes let's update the record easy action under core update record thing which record are we updating we're going to update that same line item record that we just got we're just going across each of those line items and i'm going to set the amount field and here's where i got fooled the first couple of times as i said could i set that to the amount field here and recently i think it was orlando paris maybe medman orlando we got this data pill functions and if you click that i'm zoomed in a little bit so let me back up you can see there are some mathematical functions but none of these really works for me the sum takes an array of numbers i don't have an array of numbers i'm not sure how i would generate an array of numbers out of those data pills so unfortunately we're not there yet get rid of the data pill what i'm after is this little box right here that says toggle scripting on amount okay and it brings up this nice little template for me of the script and what i want is almost exactly what's in this description only i'm going to call it a local variable called a mount and i access the data pills through this fd data object it says right there flow action data using ft data object so fd data dot well there's only one thing trigger i could type it or i could hit tab this is where i said it practically writes itself dot i want the current there's a few other things that i could get at but i really don't care when the run start time was current dot and i want the price the quantity times is done with an asterisk if you need a little javascript help we have a whole javascript series on that and then multiply that by fdata dot trigger dot current dot oops i spelled current wrong that's why i usually let it type for me times price and price so quantity times price is amount i think we all learned that back in well whatever grade you were in when you learned that and i must return the amount don't return it it says right here script must return a value otherwise what good is it you're doing these happy calculations and away it goes now also very important is the temptation is to hit the script box don't do that or you will get no value this will do exactly what it says in the amount field put nothing i open that back up hit the collapse and then it says scripted if it doesn't say scripted you're not getting the value from the script i have made that mistake more than once it's kind of like doing a glide record look up without a query you you're just mad at yourself at that point yeah so so many times that i hit that button i know and you think it's there but you don't check to see that says scripted let's go get a line item record to practice on uh 1001 looks good now let's do a test i forgot to save but that's okay we'll get there in a second test on 1001 run the test i'm not sure what this little hop over is the tooltip hint thing is kind of interesting your test is finished let's go look at the results i opened up let's make sure it's the right record 325 times a buck 25 or a pound 25 or whatever currency you want to imagine that is is good oh other thing that i found out data types on those fields right now is floating point i have tried this with decimal and for whatever reason flow designer does not like decimal so i haven't figured out why or what the difference is but floating point there's my tip of the day let's go into here and it says amount which was calculated 406.25 got saved in there the record was updated and the output ta-da the amount is in there you know what i didn't do i didn't make that field read-only we should do that real quick because we don't want you to do that so many ways you can make a field read-only with acls with the dictionary with data policies ui policies you decide i am going to go into the dictionary for this example and simply click the read only field click no not delete column that'd be the end of that demo okay the other one i want to make read-only is this order amount up here no human no touchy no touchy read only and good okay so we've got this running let's i'm fairly confident that's going to work let's go ahead and cancel save and don't forget to activate i forgot to do that a few times too activates as put this in production not not production production but make it run it can actually trigger when it's supposed to trigger watch for that trigger it's especially annoying on those scheduled ones where you go two days later why isn't my flow running oh i never right and you can tell real easy if it's activated or not from this list on the home it will say published or draft if it's in draft you haven't published now the trickier ones are the ones that you come back and re-edit it and you will have both of those lit up it'll say deactivate and activate like and publish wait where am i in this so always look over here at this little pill if you're curious as to what state is my flow in right now it's not like the old workflow where you checked it out you worked on a copy and you checked it back in okay that's done i think now this isn't real time update so if i change this from 54 to 50 and i do a save it's not going to work in real time this is just something you're gonna have to deal with for this example you can make this run synchronously and wait or other methods if you really need that but remember embedded lists are not exactly like related lists the data i enter in here think of it as like a multi-role variable set it's not saved until i save this record which is probably why that mandatory field didn't work but the read-only field works can't touch that oh suddenly i want to play m.c hammer okay so andrew answered our uh every update versus unique value yeah in the chat uh which of course andrew's watching uh can't really leave us nobody scheduled a tuesday morning meeting for him but uh it makes sense so every update runs just every time the conditions are matched the unique value uh runs anytime the form has a unique set of values so if you change it to something and then you change it back to the previous thing then it doesn't run for unique values because it's already run against that so for our purposes here we want it to run on every update uh just in case you know you change the quantity to four and then it changes back to one and then it changes back to four you would want it to run every time no matter whether that value combination has happened before so it keeps a history of that stuff somewhere hmm i guess so interesting okay interesting which you know might make sense for i don't know redoing an approval or something you know have i have i sent this person an announcement or an email or okay i could see that being useful okay next step we've got our line item calculations done and all i would have to do is change either one of these to get this or you go and write a flow you manually upd or to go through and update them once and say hey recalculate all the amounts i trust that that flow is working let's go on to the next one i want to roll these all up actually you know what i want some actual amounts because i want to take this sum at the bottom i remember showing you the list calculation before let's do this one hey look the price on ls2 clamps or l52 clamps has been reduced they are now a low low price of 12.50 and i don't think prices do that i think they always go up right okay well the sticky stuff is going to go up to there i don't know i don't know what the sticky stuff is my other order has a 25 snacks on it demo data we've all had to make it and i run out of imagination fast okay so my grand total for all of this is 19 12 75. i want that up here because often when you're reporting you just want to get at the field on the order like hey show me my open orders and how much stuff is in the pipeline and you know businessy stuff that most businessy people want so i'm going to write not a flow but a custom action that i will put on the flow so anytime one of these changes it will trigger that record to be updated so let's go back to flow designer and i can either do new action here or if i had another tab i could open it up with the little plussy thing let's call this calculate line item total you mean the parts application i don't need a category i think totalism is spelled but i kind of like i kind of like that's more oh i must have been spelling name my cat or something and i am going to take one input and that will be a reference to the order since this is specific to the order parts order library thing and let's call this order and it's going to be a reference click through the arrow find your table line darn it i have to use the long list and line there it is and that should be mandatory okay actions are repurposable components we used an action in the last flow called update record all you do is plug in fields so i want my users when they get this to say give me an order i'll figure out the rest you say you need the total recalculated let's do that next thing i'm going to script this ooh don't be freaked out come on i click the plus oh there we go now somehow i accidentally clicked on wait for condition what happens when you click the wrong thing you click the x and make it go away i must have just had some crazy lag in there so i want down here utilities script i have to pass this action input item into the script so it knows what to do with it it does not have instant access to all of your action inputs so i'm going to call this order rec and the value is going to come from that data pill i could also use this handy dandy data pill picker value here but it was right at the top level so now i mentioned glide query earlier what do you say we take our chances with that ooh i like it hey new global not glide query don't forget to put the global in there on the x 6238 part arts order no parts line i want to find all of the line item records that are related to the order i passed in okay so i need dot where order is the same as inputs dot order rack that's this id unfortunately does not give you the ssid by default so we're gonna have to do dot walking that could get turned up as a bad practice somewhere i want to sum the amount all of the amounts of the line items and i haven't used this one too much yet or else zero this is right out of the docks by the way i pretty much copy and paste it and put in my own where and so outputs.amount equals whatever came out of that glide query i'm trying to teach myself glide query whenever an opportunity comes up yes i could have done that with a glide aggregate and i'll call this one amount because that aligns with outputs amount so this is my outputs call it amount it's going to be a loading point number that is the script step output calculate order amount you can rename your actions in case you have a whole stack of these especially when you're picking data pills you hit a limit or is that a display okay that's a display thing and now this is the step i always forget i already created an output yeah i've i've missed this one a lot too don't forget there's a there's a scope to these variables one is the scripts excuse me the script steps input and output then i need to pass that script output to the action because each one of these steps can have its own little private set of inputs and outputs much like a flow the actions themselves have inputs and outputs well and oftentimes i think andrew taught me this and i didn't do it today is create your action inputs and outputs first then you're less likely to forget them i don't know when i do that then i forget to do i forget to do the output on the action itself and i just try to write to the uh you know the the or on the step itself i always try to write to the actions output so let's call this sum the name will be sum and this is going to be a floating point also i want everything to match and then i say exit edit mode i'm done defining this in a dictionary of outputs and i need to put the values in there and the value i want is from the script step amount drag drop done we take another look at your script somebody in the chat thinks we may have misspelled something out outputs that is right uh it needs another t thank you there we go give ed a plus one thank you yes those are the kinds of things that scripting will get you in trouble that's why scripting takes longer ask me about my memorial day weekend okay i saved it but i did not publish it yet let's test it i have order number 1001 and i should get 1912 out of this thing when i test it so let's go test mandatory order record why is it asking for line item numbers did i point to the wrong table uh i did i pointed reference to line this should be reference to order that's not right oh reference that again reference order i asked for an order but i accepted line items that's why testing is really helpful really really helpful okay save that again hope it didn't mess up any data pills or anything later there we are number field prefixes are your friend press the order let's go see what it did output value of that looks right well action was 19 12 75 and you can go into the steps as well to see what those inputs and outputs were ours was pretty simple step inputs and outputs were exactly the same as the action inputs and output because it only only did one thing but i wrote effectively what was that one line of code two if you count the output statement this is this is all one statement and that's two so i'm still classifying this as low code it's good publish let's put that on our massive magic flow do that and go back and open our flow i don't know why i closed it low line amount change so when the line amount change if somebody updates a price or quantity we need to not only calculate that line item but we also need to redo our order record total so underneath action i should see parts in here somewhere woohoo parts calculate line item total probably the calculate order total but that's okay there's my input pick a record well oh oh i have a line record but i don't have an order record you just dot walk to it right you can just walk to it right because there's a reference in here now you could have gone two ways on that you could have accepted the line item record but my action would then have to get all the sibling records so it would have used i would have dot walked in the script that's the only difference so which way do you want to slice this do you want to make the person making the flow dot walk to the order or do you want to make the i kind of like this because it's a little more generic you could go and create a calculation on a totally different record yeah you can yeah you can you could trigger this from other places too so if this was a sub flow uh you could call it from when the line item changed or if you want to have a button on the order record that just recalculate yeah recalculate things so did i test it i didn't test it let's test it i should save it first save is lit up it's a good indicator if it's a lit up you better save and we will test this again with uh now we're not actually changing a price or quantity but when you test a flow it skips the trigger it assumes you made that change so it's going to pretend we made a change to 1001 we then get the action's completed let's take a look at our results output of this action was 1912.75 oh i can hardly wait you have to do a refresh reload form oh guess what i didn't do we didn't use it that happens all the time too i'm glad we do this live so next thing we do now that we've calculated this useful number let's actually put it to use one more action and i like to go here whenever i can too most recent update record i i like using most recent and i like doing the copy action too there's a duplicate action that is just sometimes i find myself not using or go home why are you doing this the hard way stupid okay the record i need to update this time let's go this way trigger line order that's fun and the field is going to be the amount field it was called order amount right let's look at the order record order amount yes good i think in one of the earlier incantations i had this is called amount um i don't know if i'm on the right table or not so name your fields accordingly obviously you wouldn't want every field on your order record say order description order amount order customer and what do i want out of there i want the calculated sum done save test let's do a test you're seeing it exactly how we do this every time i got it i forgot that again i'm glad we're making some mistakes this is not a well-polished demo we're somewhere between tech now professionalism and live coding happy hour improv test those results this time did we get the right amount in 1812-75 did we update the order record with 19 1275. order amount 19 12.75 we should be able to go over here and see the little heartbeat icon that says chuck modified this and there's our order amount hey this is pretty cool i say both of these are ready to publish oh i already published this one so action is published flow is now updated and ready to activate and we have done what we set out to do any comments from the chat yeah we did have a question about uh ordering uh which i am about to paste in a link to a good doc's article that shows where uh that is a great question what happens first business rules with an order or less than 999 or a that's flow way to find out it now flows if i remember right if you look in the properties it will run they run they run synchronously or asynchronously i thought there was a thing in here that says running background or running foreground in the property i think it can do both right oh that's my action anyway no that's the flow property by default though run if you run it through the api you can choose but if you run it through the trigger which way they run i mean if you just let it run is it async used to have a property in the flow i think the flows so if you look at that article uh it's actually that article is really great because it it goes through just about anything that can run on uh something happening in the database and uh the flows run after in the trigger less than a thousand and then after uh business rules greater than a thousand run after the flight here it is by default it's gonna run in the background thank you andrew does he have a job yet is he just here to epic on us he's being helpful yeah he's proving we can't do without him [Laughter] we're doomed brad we're doomed oh it's called getting old too i can't remember where these options moved in the trigger so we could run you know for any user we can make it we can run as in the properties we could run as the system where we could run as the user who initiates it so consider that around your acls too i'm getting into a flow designer lesson again but there it is when to run so it's going to run in the background which you got to consider if you've got a before business rule or an after business rule those are going to run before an async flow and if you are running a flow just like running business rules and somebody hit save you better know which business rules and which flows are being triggered synchronously because the user will have to wait for all of that logic processing to happen which is kind of why we default these two happen in the background as i mentioned you can put it in a script api so now that we've got this done we can say copy my code snippet copy flow copy connections where'd it go you only get that on a subflow oh no there it is code snippet you have to save it maybe it's got an unsaved save not sure what i did save i just opened up the trigger copy code snippet is not available for me oh activate i don't know if i ever activated after our last activate i probably just injected a new bug because i was clicking and not paying attention once it's activated then you can copy the code snippet same with flows same with actions so copy code snippet and then in here you will see it says if you want to start asynchronously you run this part and the key differentiator is scroll with me bud there it is in background is called or in foreground as part of this string of api stuff so that's really the major difference between those two and away we go so we could like you said put that on a ui action if we wanted to refresh the total just click it it'll happen that's all i've got all right any more questions oh andrew's still on vacation and this is this is what he watches instead of netflix that's what that's what real developers do [Laughter] so we showed you how you can put a little bit of low code into a record definition or a field definition it says look get the amount to calculate price times quantity very common thing you might want to do for each of those line items and of course you could sum up the line items with a custom action you could conceivably i haven't tried this yet but you could do a lookup records and then use a flow variable to contain a title i haven't figured out if that's working or not yet it wasn't in one of the early access releases of flow variables so it's time to go back and check we're up to quebec patch two is in the wild it may be three maybe three early january early june of 2021 if you're watching this later so thank you everybody who joined us thank you for all the wonderful comments great discussion and that is a little bit about how you can do scripting in flow do we have any announcements brad yeah so we took the month of uh may off from this show and i think we only did the one knowledge live coding happy hour because of knowledge in creatorcon we're back weekly with creator toolbox uh that'll be the same time every tuesday morning for us uh afternoon for for others around the world uh and then uh a change for this summer uh or or the months of uh june july there you go our andrew would have hung you by the toenail we're going to be doing live coding happy hour on thursday afternoons an hour later than we normally do them on fridays for us so uh we will be doing that tuesday mornings and thursday afternoons uh for uh for ams and and adjust accordingly to your time zones uh but we'll be publishing uh you know those schedules uh just stay tuned for uh what we published on the youtube channel uh that's what we'll socialize that too typically i'll socialize a day before so tomorrow you should see a live coding happy hour announcement go out do we have a topic for this week just as a teaser what's the live code topic this week we do we are going to have one of our developer mvps on and we're going to talk about instant scan and doing i think we're going to maybe create a spoke and instant scan spoke to do some additional things that the current spokes don't do so that should be good oh interesting okay would that uh mvp be mark i believe it is mark yeah he's the first name that comes to mind when it comes to instant scan so the man is working on his id brand identity hopefully he remembers uh from what we talked about last week i think he's on we'll be in touch as well working on a topic for next week that's gonna be fun we're going to go back to service portal if everything works out here as i was working on something this weekend and i went hey i want to do this but i have no idea how so we're calling in some specialists now if you want to be part of either of these shows creator toolbox or live code let us know reach out looking for your help on topics if you've implemented something and said that's interesting that's worth sharing run it by us we'll let you know if there's a slot and if things work out you can find yourself doing a show that'd be fun absolutely all right thank you very much brad for taking care of the technical details and we will talk to you again real soon if not sooner oh gotta play the music there it is see you later you
https://www.youtube.com/watch?v=qV_sNRoNz4Y