NOWCommunity Live Stream - API Adventures - ArrayUtil()

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Music] [Music] good morning good afternoon good evening wherever you are and whenever you are welcome to the community live stream my name is chuck tomasi from servicenow senior developer evangelist been you for about 10 years and happy to be here once again this is monday july 13 2020 and our topic today is the arrayutil script include there's some interesting stuff in here that i recently discovered and wanted to go over i thought i knew this 12 years ago really but there's always a surprise never hurts to go back and look at this good morning to everyone who's checking in good morning hitesh swapnil mark jazmeet great to see everybody joining i saw you cued up in the beginning it's like wow we've got a good group waiting at the door well the doors are flung wide open and let's share the knowledge of array util today if you don't remember we do this live on youtube at 2 pm utc on mondays and thursdays this is monday the 13th and oh quick note if you've got something deeper about a ray util or you've got something other than a ray util please feel free to go over and share that on the community at community.servicenow.com that's our topic today i know sometimes people try and get a question in the chat that's a little off topic about training or certifications or something great questions i love them all really we want to stay focused on this topic so let's let's keep it on this topic and take those questions over to the community that you see behind me because that's the inspiration for a lot of these episodes now most of these episodes are your emails and that kind of thing oh wow everybody's checking in thank you for joining today uh back to youtube you know what to do share like subscribe notifications that whole thing you can go back into the archive on the servicenow community channel and find all of the previous episodes that we've done on this the topical ones start about mid-april 2020 and before that it was sort of a scatter of let me go through the community see what i can find and give you my thoughts on those questions so the idea of this show is to dig deep give you the thought process uh inspired by the community so that you can take the skills of engineering and thinking in the whole platform in general and apply those to your job to be a more effective developer and administrator so with that in mind let's go on you can also find this on twitch over at twitch.tv now community very happy to be over there as well if you haven't done so already please go over and check out the developer portal at developer.servicenow.com you got to give the bell in there and uh free personal developers we are only a couple of weeks away from the paris release early access on the paris release comes to the developers first so go over and get your free developer instance and you can check out the paris release before well kind of get that exclusive thing when you're done with that you turn around and register well you can register now if you want for tech now episode 77 over at tn77 reg bitly tn77 reg where i will cover with craig and jeremy all of the platform features it's not going to get into csm or hr itsm or itbm or any of the other acronyms we've got floating around there it's all about the platform what's new with apis what's new with tables what's new with security what's new with atf those core platform things that you and i use on a daily basis that's what you can get into over there register for that and i will share all of that information with you in a couple of tuesdays it's coming up quick i'm going to rehearse one more time go through those notes this afternoon the other thing we want you to take a look at let's bring that back and this back there we are is meetups we have developer meetups happening virtually all over the place let's see what's on the agenda for this week these aren't in any order i don't know why eat up doesn't sort these chronologically kind of confusing july 30th we've got minneapolis august 4th is madison july 20th coming up soon is boston i gotta look into these the 16th in just a few days is milano so there isn't one near you there's our virtual so you can go to them anyway sign up for those meetups over at meetup.com and the url for that was do i have it i do meetup.com pro servicenow dev program we'd love to see you there i'm trying to attend as many as possible time zone permitting of course so good morning wow there's so many names up there steve is here and alan and james wow thank you good audience today appreciate it that's what gets me up in the morning that and learning about a new script include let's continue on i am going to quickly mention that there is some javascript in here very quick short little examples if you want to know more about javascript or some of this doesn't make sense like what's an object what's he doing with that array i'm not familiar with arrays why would we want to use arrays this is the series for you it has now been live for one year and one day it was published july 12th to 2019 and thank you for everybody who is watching and more so to those that are recommending that you know when you get a a a reference from a peer that's that's gold to me that's this blows my mind and thank you very much i've heard a number of you at meetups and and on your blog saying hey go watch this javascript series so thank you very much i appreciate it i'm happy to be able to put this together i've got more ideas on similar content coming up in the future so stick around i'm not done yet the purple shirt of course means we're doing an api adventure today i forgot to mention that because i just noticed the green shirt up there that's thursday that's for a technical deep dive anyway the scripts that i do have for you today can be found on the bitly link you see here bitly slash sn-cls for those code snippets take them run them in scripts background or explore or wherever you want and experiment learn that's how we learn right you take a small simple example and you tweak it a little bit and go what happens if i do this what happens if i pass a third argument to this function what happens if i try adding more stuff in what if i put invalid data in what happens that's the fun part right that's where we learn oh let's continue on back to my developer instance which i got from developer.servicenow.com okay gotta keep doing that and i want to make sure i've got my notes up here where's my cursor there it is notes so i get these in order for you the way that i had intended so i have the document page from docs.servicenow.com up here for arrayutil you can see this is global and if i look in script includes which is under system definition i am flipping around to too many cursors and screens and whatnot with my hands and i look for array util this is a standard baseline some people will say out of the box i'm trying to get myself off of that term because there is no box it's cloud software come on and it says this is global and it's open to all application scopes this was not open to all application scopes at one time it was only available to global apps so that's changed i don't recall when it was suppose we could find out if there's been any recent updates by doing a show xml whew there's the script in here and if we look at sys updated on 2016. okay so it was probably shortly after we came out with scoped apps arrayutil has a number of functions in here let me open that up to the full screen mode make it just a little bit bigger for you so i can do that and we can even collapse this so we can see some of this code we have contains and i'll go through these one at a time index of ensure array concat convert array diff and intersect and i believe the last one is union oh and unique i use unique all the time okay i'll go through these one at a time it's not that long these are very handy if you've used some strings before and you've done some manipulations like index of some of this will seem natural tell me which list this element is in and i was just thinking i had an exercise this weekend i should have been using index of but it became a time issue because i had lots and lots of records to search through so i'm not going to use it let's take a look at the in the array util page it has some snippets on it okay and it also has some that do not like convert array doesn't have a code sample it just says convert an object to array not the most useful thing in my opinion so that's why i wanted to dig in a little deeper and experiment with this and explain to you what i found or what i didn't find in some cases diff code example ensure array no code example i'm working with them okay doc's team is getting here i usually extend and add sort oh sort of well sort is built into javascript sort is is a native array utility okay you often want to declare your own function for the uh sorting algorithm do you want ascending you want descending do you want to sort by a certain key that if you've got objects you're going to need to do that so you're welcome to extend it i really haven't found much of a need for it myself but i'm open to use cases to understand let's go back to my instance i am going to go to scripps background and i have some pre-written scripts here to help demonstrate what these are all about let's start with concat okay i declare the array util i instantiate the object with new very common how do you know when to use new and when you don't well if the script includes starts with prototype up here then you need a new if it doesn't if it's just a bunch of functions already labeled out if it says array util dot something or retail.something you can just call those you don't need to instantiate in fact it'll break if you do and if you forget the new keyword it will break i also put global on here because i want to forcibly say this is a global global script include because my scope may be something other than global and i will prove that down here in the uh in the cell in the picker in the scope picker on that it doesn't matter if it already is global so quick background we've got scoped applications this is a way to protect applications from other applications global was the original architecture until we came up with scoped applications about 2015 all applications could interact could write records delete records from any other table and we started coming out with apps like hr and said oh ooh no no hr needs to be protected we also needed a mechanism to be able to allow isps or partners to create applications on the store that were safe to install that didn't conflict with other applications that were already there so came up with this scope you think of it as a namespace in a certain sense it's not exactly a name space so i have declared a new object called a u short for array util i've got two arrays here a b c d and c d e it's simply going to glue those two together that's what concat does let's go prove that that works that's what my gs info says show me the output of a plus a1 plus a2 is what it basically says paste that in there and run it and i get abc cde see that not too difficult that's exactly what i expected abc plus cd it didn't do any uniqueness we've got another function for that didn't do any sorting if i screw these up all it's doing is gluing these together so you can see how having unique little utilities is very handy to be able to stitch these things together later on so looking for questions do you play games with your cams api uh make video you you have to pay for the meter on the store can't tell you about the store really good question for the community i'm not a store specialist maybe uh pretty can answer that one let's go on to the next one the second example of concat so this is a very simple array of strings i wanted to know will it work on objects and i created a three which is two objects name and age one for chuck one for tom and then i've got another array down here one for linda one for amit and let's see if this will work i'm still doing my concat down here but i've surrounded it by json stringify because if i just said hey show me what it is because this is an object yeah nice that doesn't tell me anything i want to introspect what's in that object so i put it into json stringify and put that in the gs.info so i've got a couple of levels going on here but the end result is let's put that in paste and run and the answer is yes it does concat arrays of objects not just strings not just numbers it will do objects i haven't tested that for all these methods i invite you to try it if you like so i've got chuck tom linda amit everybody's got their properties everybody's in the happy array it's a good time not too tough to figure out what concat does let's go on to the next one these are not in alphabetical order i did go to a phoenix developer meetup one time and the guy presenting said it helps if you put these in alphabetical order in your script include i don't generally follow that i probably should but i have more mine more in a logical order like my more complex logic is at the top and the simpler ones are at the bottom or they're grouped together so that these three that work together are near each other so that i'm not jumping around your style guide your thing if you find alphabetic is easier for documentation purposes good on you let's continue on with the next example which these are alphabetic contains again instantiate the object here's an array a b c i want to know if a you if a1 contains a b and if a one contains x simply saying does this thing exist in the array very easy to figure out it probably is going to say true for b and false for x and of course we do does a1 contain b yes it does we're humans we can see that you want to try you want to see if it works with objects again i'm curious i don't know the answer to this i didn't try it let's create an array in fact let's do it over here let's do over here easier to see we'll take this script yeah we'll take this script it'll be a better starting point paste it there create a new array of objects let's create some objects real quick object one in fact we could probably copy it from here copy and paste to the rescue our old friend let's get rid of that paste that in and now what would you say b dot name i'm not sure what your ass what you would ask for hmm maybe it would help if we look at the script include and see if contains it simply says if a1 equals equals element i think you'd have to pass in an entire object let's find out if it contains i don't know i don't know let's see if we can pass in the entire object for tom and 54 and say does it contain that we're going to find out if we can make this work if you pass it an entire object will it say contains it it says false okay so clearly that equals equals isn't doing it for us the element is tries to convert it to an array the array is fine yep um i suspect it's it's probably not object savvy at this point all right that didn't work but again experiment try it out if you have questions i'm gonna close that one without saving it let's go on to it contains convert array this one threw me for a little bit because i wasn't quite sure the difference between that and where's my docs docs docs there we are this is also alphabetical so you can see why i'm doing it this way convert array convert an object to an array and i thought well if i pass it this array with name and age and whatnot it'll convert it to some kind of array no that's not what it does that's that's what i thought and my experiments did not work it converts a java array to a javascript array and i went well what's the difference between this and the j2js script include that we have hmm okay don't know i don't have a good answer for you for that common exposure to java arrays is on the table utils script include if you want to say tell me all the tables that are extended from task or tell me all the tables that are in what's the baseline of this there's a lot of utilities we'll do table utils another time i think but we could say get all the table extensions from table utils then i declare a u again and convert it to an array so you're saying well g chuck what is going to happen if i just do this and a gs info to get this and it's very deceptive i don't like the way this is represented if i said gs info ext you go well it's an array right if table you get all extensions is returning an array it will look like an array it goes um oh table utils is private to global probably because it involves java objects and it prints out this array but notice this is not the same notation as a javascript array the clue that this is a java array is those brackets and no quotes if i did another u2 another array like var array equals let's put in chuck commit drove okay with just some names and say gs.info array watch the difference in the way these are displayed this is just comma separated values this has a square brackets around it and spaces in it it's different representation if you see that alerts should be going off because you're not going to be able to run your standard for loop around this thing it is going to require a convert array convert that java array or java object to a javascript array then i could use something like index of sort of putting two exercises together to say where is incident because without that i can't do it let me take a look at this paste that in there just to show you what it's going to do when it runs it says 10. if we count on our other one we should have left our gs info up there let's do this gs.info ext run it zero one two three that's a long one four five six seven eight nine yep there's incident out of 10. remember it starts at 0. so it's the 11th element that's where the index is and index is obviously very handy but if i tried using index of on this thing natively it's a gs.info a u dot index of ext incident what do you think is going to happen it's going to say undefined because it's not an array you can't do that here just keep your eye out for those square bracket things if things are a little wonky with arrays make sure you've got the right type and one of the most common operators i use when things are just not what i expect is type of and it will say this is an array this is an integer a number let's do type of on both of these just to give you an example this is an object okay and it will say object for arrays a lot of times too if i say ext down here that's an array and if i say conversion is gs dot info type of conv note that it says object also so this isn't a conclusive way to determine is this a string is it a java array is it but if you do the gs info on the elements of that and it comes back with square brackets you know you've got something so just one of those idiosyncrasies about us having a java platform and exposing data up to javascript you may get caught by that and that's where convert array comes in handy test of the variable is an array before or after i just show that oh even better let's do this instance of is another way to do this okay you could say is it an instance of glide record and it will say yeah this is a glide record or not because when you instantiate it when you have that that piece of it i could say is it an array is it an array watch what happens if i say ext getting that back from table utils and say are you an array it says no i'm not an array it's not a javascript array but that's another way better than typeof instance of will also tell you information returns a true or false i use this a lot to determine if something coming into a script include like if i pass uh something into uh here table utils task for example when i instantiate an object it runs that um where is it sorry to flip back and forth so much i lost it passes that to the initialize function that's what gets run once when you do an instantiation and i have instance of it there to go are you sending me a string then it's probably a society are you sending me the instance of a glide record then i don't need the society i've already got the record and i can make do with what type of information it makes the scripting code a little more flexible to say sometimes i need to pass a society sometimes i need to pass a glide record and you figure out the rest that's easy that's easy so that's a little insight into java arrays versus javascript arrays let's take a look at our next example we're cruising through these diff one of the most common ones that i've seen i think on the community and use it a lot you can take the difference between two arrays very handy i would recommend sorting your arrays first it's not not required but it helps in the readability output so let's do this all in one step i'm not going to instantiate it first and then called if oops i'm not going to do that either but i can chain these two together and say instantiate it and run diff on array1 and a2 and what it will do is got a b c i've got c d e the difference between those is a and b why is it a and b because and not e it's saying let's read the docs just so i get this right i don't want to misstate it find the difference between two or more arrays any number of arrays can be provided as parameters it says returns an array of items from array a that were not found in either b or c so it's not taking the union it's not taking the intersection it's not taking it's taking the difference it says here's a tell me about what's not in these other ones what's unique about a that's not in these other ones because e is not in a so it really doesn't care it only cares about b and c in my example you can see or a and b excuse me says yep a is not in array 2 b is not in array 2. c is so that's a false d is not in a and e is not id so handy a lot of times i i think i was thinking about gs.diff not a arrayutil.def pardon me array diffs aren't that common but handy to know and note that it can take more than one parameter two or more arrays i could put out an array c array d and say all right i've got these data elements think about importing data from another system and you want to find out what's unique about this first one that hasn't already been seen in these other ones you might want to do that a lot of times i deal with arrays it's on uh data import types of issues i just seem to be modifying a lot more data that way let's go on to the next one in index of we already took a quick look at this but here is a b c to e f g i declare my array util and i say where is c and where is z it's going to say c is 0 1 2 z is ah what is z well if you were to do an index of on a string and to look for the position of something that wasn't found index of from the standard javascript library will return a minus 1. it says it's not found can't return to 0 because that's the start of the string so it has to return something that's outside the range of zero to n that would be minus one and if i look at this second parameter there is a third argument that comes into index of and that is a start index where would you like to start looking you may have let's say again we're importing data and they all have a prefix of like inf for example and you really don't care you're looking for maybe an f somewhere past that you could say start counting from position three zero one two three and look for capital f at that point so let's do that as an example first we'll do the simple one to say what is the position i went right past my browser somehow go here look for the position what did i say two and minus one two and minus one good we got what we expect let's throw in the other examples back to the editor not the finder come on stop it finder finder just wants to be special today remember me don't forget finder's here hey let's do it again i'm going to learn to skip over that let's just fix that one more time there got it this says start at 2 0 1 2 and find a d and then find a d at six zero one two three four six will it find a d after six probably not unless i put one in and it says first one is at three which makes sense this is from the beginning not from your start index that way you don't have to do any special arithmetic to go well it said it was 2 and i started a 4 so does that make it really 6 your array is your array is your array that didn't change because i found the first one at 3 zero one two three and six said minus one i said well if i start counting yet zero one two three four five six there's nothing after that so unless i put another d out here that second one is going to say minus one okay if i make that an r for an r and run it it will go back to minus one there's no d's out here so again handy stuff if you want to find the index of something haven't tested on objects don't suspect it'll work uh perfect use cases for new members in a watch list or create events absolutely check those societies see if the this idea is found in there anywhere and if it comes back as minus one then you can add it love the use case as a list field like a watch list is just a comma separated set of sys ids it references the records in the table pointed to by the list field what i'm talking about is on my instance let's go back here incident is a good place to look at a watch list thank you very much brian or byron excuse me i know people you probably get that all the time and i want to be apologetic they were when i was growing up there were twins in high school named byron and brian i could tell the difference so the watch list field is you're probably already aware is for some reason not on here okay oh there it is watch list is down here and right click show it says i am like a reference field but it can hold multiple cis ids so it's a glide list that points to cis user and the values in there are going to be comma separated values for our able tutor beth england chuck tomasi system administrator you get the id it'll just be ch and you can say do i see this person in here if it comes back with minus one then add them to it okay very easy to do and you can add people through script to the watch list nice use case thank you that was very helpful next one intersect got two arrays again a b c d e f g h and c h u c k i've threw in a couple duplicates in there what is the intersection of a1 and a2 what do these two have in common if i look at the docs page intersect oh i missed insure array there's no script for insure array because it also says returns an array from an object did i have a script for that i thought i did i do not unique unique unique object the object for which to create it i can't find any use for this it's not referenced anywhere in the docs which is frustrating i mean it's not referenced anywhere in the baseline script so i'm not sure what insurer array is supposed to do that convert array or js j to js don't already do the description is similar to convert array no out of box examples that i can find don't know if anybody's got a use case or understands what this is for please let me know maybe you've got some plugins turned on that i don't so let's go to the next one we did index of index of with multiple elements intersect says what do these have in common i made a little bigger example than the one that's available there these are very similar to the examples that are on the docs page but hey if you use my repo you don't have to type them in right it says what they have in common is c and h and that would be entirely true notice that it only lists it one it's because there is if you look closely let's go back to that script include i went why doesn't it say c twice hmm if you look at intersect which i think is way down here it has at the end it calls its own unique function so instead of saying c and h and c it goes why am i telling you what they have in common twice let's make this unique which jumps a little bit ahead but that's what it does it takes out duplicates it deduplicates your array that's what unique does so that makes total sense cnh or intersect where are we on the script list oh we're almost done union puts them together says what do they all have if i want to take multiple arrays and say all right keep finding out what the this is i want the whole data set sum is in this record sum is in that record some is in that regard but overall what does the whole data set look like we can do that i feel like i'm back in like fifth grade math where we were learning about venn diagrams and unions and intersections same concept there's a reason computer science and mathematics departments are usually linked together this says abcdefghuck okay not sorted it is unique right because i have this list and this list and it says well i already saw c and i already saw h but it went a b c a b i don't know how i came up with that order it's probably just sticking them into this list at some point so intersect very useful ensure array looks like it basically guarantees that the variable is an array i.e null and undefined are turned into empty array oh shall we have some fun with that let's try so var a1 equals nothing far let's do our au equals new or global dot array util easy enough gs.info a u dot insure array a1 see what it says nothing okay ensure array a2 a2 is not defined so i can't use it which it technically is if i say undefined right wouldn't it say undefined well maybe if i went a2 this is undefined it has no a nothing let's do a type of here now let's save the answer r my a2 equals au dot ensure array a2 curious where you found that information too or if you did some experimentation find out a2 and let's say gs.info for earlier uh instance of a2 it's got to do this right a2 instance of array a bed statement i'm missing a semicolon somewhere do i have a colon instead line seven this is where i wish i had line numbers two three four five six seven oh too many brackets too many parentheses undefined and false okay so type of is undefined and this is false oh but i should have my a2 out here where find out what my a2 is we already know a2 is undefined and it's not an array and now it's an object and true it is an array cool i'm not sure when you would use that but somebody's got an imagination for a use case to ensure that some thing coming back maybe so what would be the difference of that and let's try convert array convert array undefined in false okay interesting we need to improve the docs really bad on that page let's go back thank you very much byron we all learn together that's why this is the community live stream it works sometimes oh and look at this we even have communities spelled out in here final one is unique you can see i've got the letter c in here once twice three i've got m in here a couple of times unique is simply going to isolate those duplications i believe it does this in place where we get h k c o m u n t y feels like it's doing it in reverse order but i'm not sure uh out for operations not necessarily unique well yeah it is it's got a nested loop in here so it's going through both arrays if you have lots of data or lots of arrays or you're doing this thousands and thousands of times this can get cpu intensive i discovered this last weekend i was putting together a small example of retrieving data from a rest api and i said hey this works pretty well with 25 records let's extend it out to 800 records and it timed out my flow stopped it was too long i said where's it spending all its time it's not in retrieving the records it's not even in creating a new glide record it was in that processing that it had to do on each record because if i took that out and said just retrieve a record and go retrieve through the rest api and for each one of those go create a glide record of what you saw not a log statement but just a separate glide record and a separate table and it would do about 20 of those records a second when i started throwing in this interstitial processing between each record that slowed down to one record every two or three seconds that's a 10x difference because i'm going through one of these index of operations is exactly what i was doing i was saying does this string appear does this appear in this array somewhere and just went it just kind of blew up so be careful when you start seeking in this this interstitial processing it can really really hamper your operations okay and now i just thought of a different way that i could approach that problem sometimes not thinking about it is a good way to think about it and that takes care of it i think we're that's the end of our examples i will have the code posted up to make sure there's nothing else out there nope nope we've got we got through concat i've got my concat objects contains diff index of both operations that's a ray util lots of useful stuff in there if you're dealing with arrays which are very very powerful things if you haven't already done that you can pass a whole data set similar data type whether it's objects integers strings you name it you can make a list of these things and pass them and say here function here's a whole bunch of things you can work on involve arrayutils when you can and say where is this element in there but again be careful because looping and nested loops in these things can slow it down depending make sure your operation is scalable that takes care of it i hope you find a good use for ray utils i am going to do that wish you a fun week join me this thursday again 2 p.m utc whatever that happens to be in your neighborhood it's currently 6 00 7 a.m pacific time in the u.s 10 a.m eastern time in the u.s probably 7 30 p.m ist you got the idea 2 p.m utc this thursday i will be doing another inspired question from a community member saying i want to put some reminders on approval so we're going to do approval reminders on thursday hope you can join me for that very popular subject comes up all the time on the community we've had a number of i'll just say evolutionary answers as we've gone through the history i used to do this with a scheduled job there's better ways to do it now please don't use scheduled jobs for this stuff anymore it's not the right answer for you know create a creativity and maintainability and speed join me for that until then i will see you again real soon take care bye [Music] you
Info
Channel: ServiceNow - Now Community
Views: 1,591
Rating: 4.9310346 out of 5
Keywords:
Id: EuV4IjbK3xo
Channel Id: undefined
Length: 42min 30sec (2550 seconds)
Published: Mon Jul 13 2020
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.