logo

NJP

Inside the Barrel Pints - Script Include Global Include Pattern in ServiceNow

Import · Mar 02, 2022 · video

welcome to another episode of inside the ferkin where we take a quick look at a day in the life of uh consulting this time we're on to another episode of travis tolson's blog posts about uh script include patterns um so take it away dorian awesome yeah yeah so again like shout out to to travis for all the work that he's put into that uh article and for those that are just uh filming in like we're we're describing different architectural patterns you could use to set up your your script includes um we started our first slide session on the class uh pattern then we moved on to the functional pattern then we moved on to the name space pattern and now we're into the global include pattern so we'll kind of jump right in on our on our screen here we essentially see um you know i named it global include pattern so some pros about this one is your it helps you uh organ oh i actually this is copy and pasted [Laughter] i was like that that okay that's interesting um maybe i didn't even yeah i guess no no this is right okay so yeah i'm going crazy the only thing here is this this pro that one yeah so um some of the pros is it allows monkey patching and so essentially what monkey patching is is like a term when you want to change existing objects or classes because essentially what you're doing is i lost john but essentially what you're doing is you're changing an existing object because you're loading in uh the entire object as is and it also allows you to include external libraries so this is a really really good pattern for if you wanted to to utilize like moment.js which you've seen a few in a few different places um or if you just want to include an uh another script and then modify essentially it's its functions so some of the cons again like functions aren't truly private in here and it does have to be in global scope so that's definitely something that you want to you want to be careful of it will yell at you if you're not and so kind of so there aren't really any good examples in the system that that do this but i think the most common one is you have some external library you want to you want to utilize that is supported for like javascript 2015 [Music] if you do try to import something that uses like cons or lead or any of the emacs 6 stuff then you're you're going to run into some some issues compile issues yeah um but uh as for what this pattern looks like to utilize it so that's not where my atf is so we're gonna open up an atf and we have another atf for this one and pretty much the the same thing for the parameter definitions the data set looks the same whether someone's locked locked out or not they're really the only major difference here is inside this server side script so essentially here the difference is you include so you call gs.include uh of that and that essentially makes it available right like as if it was a function that exists um in your in your normal code so it essentially loads it here and then you have access to those uh to those those pieces and so like as you can see here i you know if the locked out user you know call this that private function then call the the other function that i had in here and then do some you know expect some results out of it so um pretty pretty similar process uh that we that we were following from before and so what we'll do is run this test we have some breakpoints oh but i i just granted i didn't actually click debug so and our test failed probably because it has the same bug that i didn't fix last time um but that's okay right again these are these are meant to more show you the pattern and and how you can utilize atf's but you know the the code itself like notice here right it looks like just a bunch of functions right so you can organize it um all your functions in one place you then use the the gs.include um i'll go back to that so you you essentially create your global include pattern you load up all of the functions that you want in here that you want it to be called so there is some organization that you can do with your functions and then you call gs.include and that that loads it into this this script uh for you to call uh in the future so you can just call any of the specific functions out of that script include after you've included it right you don't have to correct do anything else it's as if they are in that context available yep so definitely useful for external libraries right if you wanted to use momentum.js for example is the most common one that i've seen and um and yeah it it you i don't see it as common to like call your other functions to do a gs include elsewhere um but as i said like you could use it for monkey patching so what you could do is like you know add objects to like since this instantiates a function you can call that function and then you know potentially modify some objects in there and then you know continue down the route of utilizing it um but yeah a very simple one not very commonly used but yeah definitely useful to know um and you said you were you were bound by global there wasn't a way to there's not a way to use it within a scope app you would you have to have it in global yeah so just the the gs function dot gs.include isn't in scoped at least from the servicenow's documentation so it will yell at you if you try to just include uh uh something that isn't in the scope in the global scope oh yes yeah cool no i mean it makes sense with with moment i mean i hate dealing with dates and moment does make it so much easier and so yeah that that's the one thing i can see working but the the problem there is if you're working outside of global and you need to do it yeah so that's why it's like the global include pattern right it's not it's probably not as useful as the like some of the other scoped ones um sure but yeah that's i mean that's you could in theory with some work take some of the moment js stuff throw it in a script include pattern that we've talked about before and then call it um so not as straightforward but totally a a plausible uh a route you could take sure sure well good all right is that uh how many of those are left is that was that the last one so that was the the the last one before what travis would call the perfect one [Laughter] so we have one more on uh two days from now and uh we will uh we'll go over that one and that should be one of the the most utilized ones if you're not using the class one in my in my opinion okay so all right so i i had a question though this is kind of outside of what we were just doing but it came to me as you were talking at the beginning um when you mentioned that you know it's got to be uh workable in emacs what is it five 2015 yeah what what do you think servicenow is going to do um in the future to get us on a newer version or or do you think it's good like because i foresee it needing to happen at some point they've got to get us off this old emacs version but i i worry i mean is is uh is it backwards compatible as you go forward so um so if you remember what was that website i sent you like the battle yeah the babel 6 or something like that i think that's the route service now it's going to take is they're going to you know build another translator like a translator or compiler that's the only way they could do it without it like breaking a bunch of stuff um yeah um and i think that you've seen that in the the compatibility switch on on the test scripts that exist today there there is a way to even get to a further compatibility a further older one than uh and then the 2015 one so okay well i'm excited once they do move up i i would like more of the features yeah i think the world is ready for it that's so true well you know babel if i remember the last time i looked at it they were like oh we're going to stop allowing you to choose 2015. like you can't even go back that far to convert it anymore yeah it's it's i mean at some point they're gonna need to to cut it and and hopefully provide some mechanism to convert code for people yeah yeah because i yeah or even their their own internal code they have tons of scripts that they would need to make sure it's compatible so so true so true it's a big deal all right uh do we have anything else no no i think that's it so we'll see you guys in two days for the the final one of the series all right and if you like it subscribe i don't know where the button is it's somewhere yeah yeah thanks everyone thank you

View original source

https://www.youtube.com/watch?v=n4L7a-6cFFM