logo

NJP

NOWCommunity Live Stream - Code Decoded - Condition Fields

Import · May 28, 2020 · video

[Music] good morning good afternoon good evening wherever you are and whenever you are welcome to the community live stream for may 28th 2012. let me mute that forgot to take care of a little audio on my end it's always something i miss always something my name is chuck tomasi i said this is may 28 2020 and happy thursday to you wherever you are and whenever you are thank you for watching i am a senior developer evangelist here at servicenow and looks like my prayers have been answered youtube is working and thank you for joining me it was looking a little sketchy when i had the broadcast software up it was saying i'm having a problem connecting but that stuff is likely to happen good morning sri swapnil dhruv dave everybody checking in it is wonderful to see you hope everybody is enjoying the content for knowledge 2020 digital experience i will get to that in just a minute today's topic i'm wearing orange it's going to be a code decoded we're going to be doing some script and that means roll up the sleeves and have some fun okay you can find this information on youtube you're probably watching this on youtube the people i called out definitely are they know to go and hit that subscribe and notifications and like and comment you know the usual youtube stuff you've been doing this for a while it's only been around since what 2004 2003 i know youtube is pretty old and so am i so let's you know what to do there's the url you see right there so take a look write it down screenshot head over there if you haven't already you may have just randomly come up with this on your phone thank you for finding it if you're looking for something on servicenow condition fields this is the place we also offer this on twitch oh forgot to mention if you do get this on on your mobile device you subscribe and whatnot you will get an alert that looks much like that and says hey there's the one that just came in from twitch while we were talking so they know we're out there and this pre-roll just kind of gives you a minute or two to join while i talk about all the cool stuff that's happening out there so youtube twitch it happens i mentioned knowledge 2020 digital experience it's still going on this is week four it's and there's six weeks there's still two more weeks to go we've got our keynote with cj and uh cj desai is our chief what does he he's a chief product officer and uh bill mcdermott our ceo will be giving his keynote this week was the customer success keynote uh wonderful stuff lot of great customer input it's fun seeing some of your faces on here on the on the keynotes and the sessions and the workshops thank you this this wouldn't be a success without you and i hope you're enjoying it as much as i am i know that i don't have time to sit down and watch everything live as it happens as it rolls but you can always go back to the scheduler there's an on-demand section if you want to see the the the servicenow karaoke app look for hack snack i believe it's 48.22 just in the search bar type in hack snack h-a-c-k-s-n-a-c-k and uh it's one of those it'll say service now karaoke or something maybe you could just search for karaoke i don't know there's probably only one karaoke session and the whole scheduler a little video i put together about the app that i built and i'm still building i've been working on a lot of things and finding some new content for this show so i came across something about objects yesterday that i learned and discovered something new in service portal so it's a wonderful experience to be able to share this with you i i think the content is getting in the pipeline faster than i can spit it out so twice a week mondays and thursdays i will be here sharing that information with you whether it's about coding or a specific topic i want to put tools in your toolbox give you the thought process the engineering that to make you a more effective servicenow developer and administrator good morning to john and steve steve you made it i made it all the way to stockholm on tuesday not really but it was a virtual developer meetup which reminds me if you are interested head over to developer.servicenow.com and under connect we have events and meetups and meetup.com organizes all that stuff lots of great in fact let's put the developer notice up there first developer portal is looks like that before you log in developer.servicenow.com and you can find more about the meetups over at meetup.com pro servicenow dev program i know that's a long url you might want to screenshot that one so take a look get in there join up with people you don't even have to be in the local area because everything is virtual these days thanks to the recent situation you know it's been going on for the last few months sad as it is it's giving us some new opportunities like me going and listening to swedish for a couple hours on github great slides couldn't understand hardly any words except servicenow and source control and branch but there it is we've got the the pictures made up for the thousand words that i missed so thank you very much that has been fun let's get back to the community i think oh we will be doing some scripting today so if you want to know more about scripting just a quick reminder that i do have a scripting course available totally free it's available on the community developer channel but if you follow that bitly link there sn-learn js you can go all the way from hey i don't know anything about anything to comfortable maybe proficient might be too strong of a word but i it's a great setup for taking the scripting course online virtually maybe some day back in instructor lead check it out go look it's also a great reference if you go oh what was that thing on uh objects or passing functions you know this will help understand some of what we're talking about today i will try to slow it down so that we don't go too fast hashtag now 20. thanks dhruv love it the other thing is the code that i share today will be available on github that you see over there at least sn-cls stands for community live stream like a lot of things in servicenow i'm not very original at naming things community live streams seem pretty self-explanatory it's live it's happening now if you're watching this live 1 p.m utc on mondays and thursdays as i mentioned all right that i believe is it for the setup let's get into the meat of this thing for today's situation i am going to pop that up and go to my developer instance last episode we looked at flow designer decision tables and decision tables and how they tied in with the make a decision action under the logic go back and watch that one it's turning out to be hugely popular thank you very much and this is this is sort of a precursor this is what we did before we had flow designer before we had decision tables it's still around and i still use it quite a bit it gives you some additional flexibility granted it comes with a little bit of scripting complexity and i'll get into that so i i want to just quickly cover we've got condition fields which allow you to put condition fields you've seen them before you've seen them in codeless business rules you've seen them in ui policies you've seen them in every list filter where you start to build out active is true date is at or after today and and you can build out these conditions you can put those into your own tables they're very powerful very flexible you can either associate them with a table field or you can make it statically linked to a specific table and i'll get into that in a little bit if it sounds a lot like hey i'm making some decision records that something is going to go and evaluate sounds a lot like the decision tables yes it is and now that we have the decision table api in orlando they're getting even closer because you can access those decision tables from a script well this is what we did beforehand so why would you use one over the other i'm still looking for a use case to say this is better for this and this is better for that but obviously flow needs a decision table you can't use condition fields on any old random table in flow so if you have to use flow you have to use decision tables if you have to use script this may be an option now what i'm going to show you does not require a decision table excuse me an answer table like a decision table would when you set up a decision table you have to have an answer now it could be an existing table could be something you create but every decision in that decision record has to point to an answer whether it's an incident whether it's a template whether it's a knowledge base article whether it's something you created in an answer table of your own like i did on the flow table uh on the last video check that out and and there's so there's differences that you want to be aware of and some of this may drive you to one or the other so let's get into what i am going to build today i'm going to start in studio which just happens to be here i'm going to refresh the screen just one more time make sure i haven't been talking too long that i got logged out that happens usually on my personal developer instance i set the glide.ui.session underscore timeout to 1440 that's the number of minutes in a day so if i'm not using this for a day it will log me out 30 minutes is the default don't do that on your production instance unless you have special permission okay a lot of confusion in none in choice type uh secar i'm i may be looking at that in just a little bit but let's continue on with what i plan to do and then maybe i can come back to that answer in a little bit so let's go this is studio and i am picking on my favorite community live stream application simply called cls 323 which tells me it was built on march 23rd so i don't run into any scoping conflicts and i've got my tables i have an employee table i have a band last time we used the employee and made an answer table called employee band we're going to take a little different approach this time and i'm going to make a new table of rules okay this is kind of my own decision table so let's start out by going data model table and i'm just going to click it since i can't see the create button and don't want to make my font smaller good old table creator not extended from anything i am for lack of imagination going to call it rule okay and it calls it 66 not rules remember don't make plural tables you'll hate yourself later and it's already going to make it plural anyway so let's add it to the menu cls just so it lives somewhere for whatever reason this app was created in guided app creator it's making a new role it's making some new acls and it's making a new menu for every table i don't need a new menu for every table okay a couple of things i want to add to this first let's give this rule a name you need something friendly that people can see especially if it's in a reference field so let's make that display true i'm going to just quickly flesh out this table let's make it active since just about about every new table i have has an active field for reference qualifiers for filters for deprecating data later that you don't need the idea behind decision tables the idea behind this rule table behind this filtering making condition fields is to make your application i should say this right up front so listen closely to make your application more data driven reduce maintenance for you when business rules change i mean i say business rules in a generic sense not in the technical sense of the business rules but when business logic when requirements change and the people up above you who are or the out in the business are making these requirements and say we no longer want red we want green we no longer want a hundred thousand we want 120 000. they're changing these rules you don't have to come back to some flow you don't have to come back to some script and change that logic that would be painful look for those observations in your requirements to say this may be a good place to move the rules into something we can maintain quickly and easily without script without going back to dev delegated to somebody else who has access to this rules table that you see behind me let's hand this out make your job easier aren't we all developers and lazy at heart i mean efficient and right you want to you want your future life to be better than your previous life which is let's make this easy to maintain okay let's keep going we've got an active field that defaults to true order we want to evaluate these in some sort of order usually doesn't have to be it could come up random if you like but i am going to order these and let's start with a default value of 100 just for fun you don't have to have a default value now here's the two key fields that make this whole thing go table again like business rules like ui actions you pick that table and then you see the condition field it populates full of fields it's really cool so this is a field type called table name available to you i did nothing to modify this it's available here at the picker don't know if it's available in guided app creator out of the box i think they filter those fields down to be a little more user friendly no code friendly whatever you want to call it line of business the second one is condition okay sometimes it's called filter please don't call it query you can run into problems if you call your field query because you will now have a field query and if you reference the table query it will get confused with the field query notice there's a method called query when you're doing a glide record query okay when you have a field called query you have to use a method called underscore query so i just try and stay away from keywords when i'm naming fields i ran into this when i called a field event one time event next query just if you're aware of the basic methods on glider record or the objects in the system like action current previous event mail don't call your fields that okay now some may argue this is a good time to put you underscoring here you may do that you can choose to do that if that's your standard that's fine i've gotten accustomed to scoped apps where you don't have to and then things like the add active query method works fine on a field called active but i'll get into that in a little bit the field type on this is conditions okay that's where it's going to give you the words that spell out be active is true if you use a condition string that's what shows up above a ui action i don't think i have a ui action in this application i do okay so let's take a look at that where would you see a condition string it is like a one line script it has to evaluate to true it doesn't have a return it doesn't have an answer but this is a conditioned string in fact if i look at this and say show condition this is a condition string field but if i look at i don't know if i have any ui policies no i have a client script maybe i have a business rule no don't have one that works on there but i could create one real fast the condition field is going to be very obvious when you see it so make sure you pick conditions not condition string and that is our table okay very simple i can add other things if i want but this is what's going to drive the logic i'm making rules let's go take a quick refresh on our app and it said refuse to connect that's nice did it refresh this at least it did cls rules got lucky out of that one and i have name active condition order now let's take a look at the new create this uh actually the layout on this isn't too bad but i'm going to just it around a little bit and i don't need the order to be in a wide field format so let's put that let's put that under there table maybe you know what that would actually look better as a shorter field so there nice and balanced condition can be long because they do get long close that fix that reload that i like form designer the more i use form designer the more i like it so let's call this one active users i'm going to save it first just to show you something i have a list of tables and if i look for the cis user table all of our good tricks work here too so i can do that square bracket thing to find this user fast but notice the condition field did not jump up and say here's the user fields because condition needs to depend on table you got to do one more thing in the dictionary here let's go back to studio and in my condition field after i create my table i go to the dictionary entry for condition and click advanced right down here you know i had a thing there advanced advanced try that one more time advanced and it shows a few more fields like attributes we'll get to attributes in a minute one thing at a time first let's do the dependent field click dependent and it says which table or excuse me which field in this table is this dependent on you've seen dependent fields before if you've ever used assigned to an assignment group assign two is dependent on assignment group uh what was another one there was like subcategory is dependent on category that kind of thing so let's make it dependent on the table and just by making that connection we are going to improve the functionality of this by one thousand percent reload this and now it says oh that's this user table let me go fetch the fields for that and i can say active is true that is a rule that is a condition field on there let's do another one for example p1 active incidence you can make these up till your face turns blue okay now this is widely dynamic with any table any condition so let's go incident is the table that incident brings up the fields just like you this is this is like one of those wow this is cool i can do what what's what comes out of the box right you kind of feel like you broke through a little secret wall here and i can say active is true and priority you get the idea you've made filters before right lots of times nothing magical here for whatever reason i always think of the old itunes smart playlist when i do this let's make this one 200 we want to evaluate it in a different order you can make some of these fields mandatory you could obviously i didn't do any of that i didn't put any acls on any of this yet so you get the idea now notice that the condition in here looks like an encoded query string you may want this i don't okay there's also a couple other little tricks i want to show you about the condition field remember i said about the attributes there are some attributes you may have noticed hey sometimes when i look at a condition field it shows the number of records that match this condition yes we can do that sometimes there's there's lots of cool things that you can do with this two that i do almost immediately are go back to our dictionary entry down here in the attributes i can click new and the attribute i want is show record count come on it's not going to do it for me this is this is one of the things that will bug me i pull this up and look for show show condition count sorry got to get the name right show condition count and make that true the other one that is very extremely mostly useful that's that for a sentence is readable readable is what makes it show up on a list in readable and i i have been trying to get our developers to adopt that practice because so many times especially in the mobile world i see these conditions on a list like oh you know i'm i'm okay trying to interpret a encoded query string but i don't think every average person or or non-veteran of 12 years is good at that so let's do that now if i reload the form watch what happens it goes out and says aha 613 records nice thing about that link and you know it you click it it brings up those 613 and if you change the condition you can hit the refresh that is the oldest saddest looking 16x16 or 32x32 icon i've ever seen there's still a few of them that hang around you wanna see another old legacy page this is fun uh where was it was it export was export pdf no it was one of these in here now i can't remember it was something in here that you could print maybe it was export pdf and it had the old big green arrow no that wasn't it i'll have to find it it was oh it was it was something like print show printable version or something of this page somebody's going to point it out to me anyway sidetrack okay so we've got a little more readability on there and if you look at the list of rules now it says active equals true active equals true and priority equals one much easier to read okay than an encoded query string so think about those two attributes if you're doing this the third one and i'm pulling these right out of the docs you don't have to be a good memory person the docs for dictionary attributes down here if you want to find them they are readable readable true false any condition field causes it to be human readable very nice there's another one down here that i want to talk about i don't know if i'll have time to demonstrate it but it is static dependent there it is name of table so in fact let me demonstrate this real quick right now if you look at any of these records i pick a table then i pick fields you may have situations where your rules are always going to pick on one table let's say for example active users wasn't my table i'm making something around incident management or change management and my change management is pulling from a table of templates that i made whatever you make up your own condition but if it's always on the same table let's say this rule table is dedicated for incident management rules it's always going to point to the incident table i don't want to burden the person or cause a possible mistake by them picking the wrong table okay yeah you could probably say default value equals incident and hide the field and that's messy there's a better way okay if i didn't have a table field and i'm going to assume i don't for this moment i will hide it for you just for the sake of demonstration to show that i'm not picking it i'm not doing anything special save that now without a table i can't do this so how do i fix that how do i attach that directly to the incident table well that's another attribute on my condition field called static dependent static okay that reference picker is really annoying and this may be a good demonstration for something else let's contains dependent nope okay guess what not all the attribute choices are in this related list pick list so boo hoo what do you do in that case well look up here this is kept in sync with the attributes related list kind of cool which means hey i just read this attribute called static dependent equals some table name and when i save that record watch what happens it's it's not that magic really it puts static dependent down here if i couldn't choose it from the table i could always type it in myself in the attributes field i personally i think that's why the attributes field still exists that it's not related list only because all the options already in the related list so that's just my thought steve says he prefers the encoded query god bless you my son go forth and have fun the code of queries are nice because you can just copy and paste them into other places in your script but it doesn't have to be that way if you have humans involved consider readable okay now that we have that let's go back to this table and notice that i now get incident fields and if i create a new record i get incident fields i have no choice it's a static dependent as opposed to the dictionary dependent that attribute just overrode my dependent dictionary definition didn't realize that was going to happen generally you don't want both okay typically you wouldn't even have a table field if you knew your condition field was always going to point at the static table you don't need a table field unless you're using it for something else so don't get these too confused i should have detached them made it non-dependent okay so that is that let's continue going along with our exercise let's do active p2 incidence and make a couple new conditions active is true and priority and yes you can dot lock on all of this stuff if you wish submit let's create one more that says um software active software incidents and we will count these up so active is true and category is software you make up your own if i do that it says congratulations eight records match i get some good stuff out of here let's take one of these i'm going to use it as a sample it's an active i'm going to copy this society out of here which may actually be the same society now it's time to get down to the script how is it if you if you look at this okay this says i'm going to write a condition that gets all of the records out of this table we're doing the reverse we have a record and we want to see which one of these rules it matches okay that's different why would you want to do that because if one of these rules matches you could then trigger some action maybe it's a flow maybe it's a workflow maybe it's some script that you write whatever you got you want to see does it hit one or more of these maybe another field that i didn't think about before doing this is stop execution if these are happening in an order do you want to hit all rules or do you want to hit the first rule and stop okay kind of like inbound email actions kind of like a lot of other things where you could go hey i've got multiple matches do you want to do all the multiple matches it might be active software but it might be active p1 ooh think about that what do you want do you want all the rules because different rules may trigger different actions different rules may bring in different records from a template rule think about where you might use these and how you want it to behave and whether or not you want that i'm not going to put that in but i will show you roughly where to put it in let's go to scripps background and i'm going to do the cooking show thing and i have a script already built here so i'm doing the old faux current idea where i have an object called current because it could this script could later be put into a ui action or a business rule i will oops try that again highlight just that undo that it ended with 6a7bc i think that's the same record i'm going to just pick the same one i did okay lucky me like winning the lottery so i set up a current object so that i can do this part that looks like a ui action or a business rule or whatever to see which rules match how do we do this we go through the rule table i am looking for active rules this should look pretty straightforward to anybody who's done a glide record query before this is our nosql sql stuff i am checking for any condition that's not empty if it's empty it's not a condition in my opinion why would you want a condition that matches all records you can just go get those yourself okay because i have a table field i don't need to look and say here's an incident does it match us this user rule why just look for the ones that are on the incident table so in this case i'm going to look for rules that are on the incident table and the table is whatever my current record is so if it's on the incident table i'm going to look for all the incident rules remember i had one that was sysuser it'll skip over that one i'm going to put them in an order and i'm just going to get all the rules that apply here i might have 700 rules but only 500 of them are active and only 20 of them are on the incident table you get the idea i'm not getting all 700 of those and then doing if if if in my while loop that's the slow way let the database do a lot of the work for you okay for every record that i find i'm going to dump out the condition okay that will be an encoded query string just to show here's what i'm looking for normally i wouldn't have the gs infos all over the place this is for your benefit here is where the comparison checks this is the secret sauce that makes this all happen glide filter it has one method it's called check record this is all this does and who knows this might be what's going on behind the scenes on the decision tables i don't know never asked but it takes a glide record which is going to be our current and the string filter the condition from the rule record and it says does this record match this rule in some way shape or form is the priority one is the is it active true and if it does it returns a boolean match true or false so if it matched i would do something important here and i will print out my magic current display value and which rule matched it the name of the rule let's try it out let's go to scripps background there it is i think i can make that a little bigger for you maybe even a little bigger sure that'll work paste that in run that script and it says hey guess what here's the condition i'm looking for active equals true priority equals one it did in fact match this incident which matched the rule p1 active incidents now i thought i thought i had one called software category let's find out active software incidents active is true category software we probably should have matched that to like record 300 so that happens in some other order and eight records match wasn't 9005 one of them it was okay so why didn't that rule catch i'm scratching my head right now wondering why try this again it's getting all the rules it's true it's not negative oh i know why right there when i made a static dependent i said i don't need the table field therefore it's not set on this record it says that rule doesn't match the incident table so that's my fault let's go back and put that in let's get rid of our static dependent on the dictionary so somewhere in studio found it i have my condition field let's get rid of the static dependence so that my table field comes back and i can set that update that wait for it go back in here refresh that i know there's a better way to refresh it but this is more fun and i gotta put the table field back on here configure form design we'll do with form design this time i alternated table can go right back where table was so that we can set the table value so that the condition matches that was a good exercise you learn things when things don't work and then i make videos out of them okay it did not match because it wasn't true it was that that wasn't the right table so if i set this to incident and it even remembered the condition that never went away it just couldn't render it because i didn't have a table now that this is a dependent a true dependent save that and if i run my rule now it should match two rules haha look it got the first rule called p1 active incidents it also got another rule called active software incidents you may be triggering mail conditions you may be whatever you want rather than just this gs info from here okay check it out see what happens it did not match was it there was a third one in there for incident maybe there wasn't let's go see i have two incident tables it's not really proving anything at this point oh active p2 incidents also didn't have a table let's go and look i have three incident rules now let's make this order 5 500 whatever and run our script one more time now that we have three rules on the incident table for sure we will get here's our first condition is it a p1 active yes okay you matched is it a p active category yes okay you matched is it an active p2 well obviously it can't be a p1 and a p2 at the same time so this is true it is false so what can you do with that a whole lot as i mentioned you can set up rules either the way i did with a dynamic table field that you can then create any rule on any table and you may want that for your use case or you can set up with a static dependent field where everybody's pointing their rules at one table okay if you do a static dependent then you want to modify this script to take out this line in fact i'll help you and say don't use the table field here if you use static dependent that was not going to get a whole lot of matches because the table field won't exist and probably won't be set well if it doesn't exist it won't be set so we'll go with that i hope that makes sense this is really powerful stuff for making your applications data driven now somebody comes back later and says oh i want all the active p1 incidents that were open the last week great here's p1 incidence add your condition and you go home it's a nice easy evening for you and opened so you can type better than i can at or after last seven days or last week or whatever you want you know how to do this okay maybe it's january 1st of this year there's my new rule none of the script changes now the logic changes but the results people get will change very important i'm just thinking if you could tie this in with a scripted table so that your results end up oh hey that would be fun i wonder if you could put these two together remember scripted table remote tables that i did a couple episodes back see if you can think of a use case to put those two together so you don't need to store the data but you could make these dynamic filters on records that come out the scripted table would be a lot easier it wouldn't have as much logic in the script it would just go through does it match this rule i'm going to build a table for that does it match this rule build a table for that and then display it as a regular oh i'm not sure how i would implement that the filter on that but that would be really wicked fun okay this is one of my favorite favorite features when i discovered this it was a game changer in 2011 2012 whatever it was doing an implementation for a large pharmaceutical they wanted change management and they said we want some templates so if it's a server upgrade or if it's a software upgrade a server install they had these different packages of tasks that they just wanted to define or redefine and then glue them on copy them over to the change record so when you picked a template and said go it would say it wasn't so much a reference field but it was rules to say which ones of these do you need to choose and then go find that you know is is this change record in the uh hardware category is this change record assigned to this group is this change record whatever rules they wanted would then define which template they went and got and attached and these were pretty complex conditions too once that was implemented with this glide filter thing and a rule table never looked back they have a rule they have a set of rules they had some template change records they had some template change tasks so they said go find the change record that matches this condition in the templates and get its tasks now they can maintain whatever template tasks whatever conditions they want eight years later still running never had to do any maintenance on it because it's that flexible so think about that it it lightens the burden of maintenance for you later and conditions are easy for somebody else to decide and build so you can delegate those the rules table will have its own acls so you can do what you want there that's pretty much it for today i am going to jump back up here hope you enjoyed it again like if you find something useful in here and share it with other people i appreciate all those people that are going out on linkedin and saying hey chuck's got a new video thank you i will due diligence and share with that as well when you at mention me kind of thing so until next time i will be doing something on monday which i believe is the first of june wow where did may go and it's still going to be knowledge 2020 time i don't have a topic yet for monday i will be falling in line this one did change because the topic i said on the last episode actually did a few weeks earlier so i don't want to repeat anything that was done in that context and actually this was a great follow-on for flow designer decision trees and make a decision and decision tables so these two went together really well monday i will decide a topic next thursday i'm mentioning this ahead of time because this one's already planned out next thursday june 4th i will be reconstructing the loaner request application that i built in 20 what was it 2009 2010 whatever it was it's it's the app that got me the innovation of the year award at knowledge 2020 still lives on but i haven't touched it since 2016. it's still available on share as a scoped app but i haven't taken advantage of mobile i haven't taken advantage of flow designer and there are some customers out there with very strict rules that say thou shalt not download and install anything from share how did you build that so i'm going to be presenting how it was built in a slightly different light of how i would rebuild it today so i'm not going to do it end to end that won't happen in an hour but i will be showing you how i would do a lot of those pieces i'll do a couple of flows a couple of reports a couple of i'll get into some mobile so you'll see some of the new mobile studio things lots of stuff in here it will give you good ideas of where we can take this so lots of stuff working on that one there's still a few uh things i need to smooth out before i deliver that but look for looking very much looking forward to that one on june 4th rebuilding the loaner request app you'll get a lot of information out of it i'm sure if not well share it with somebody who will till then take care have a great weekend and be good to each other be safe [Music] bye [Music] you

View original source

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