logo

NJP

Inside the Barrel Pints - Script Include Module Pattern in ServiceNow

Import · Mar 02, 2022 · video

welcome to another uh episode of inside the barrel where me and dorian talk about uh what we do for work consulting in servicenow sphere these are our short videos we are coming up with a new name i'm not sure that i enjoy inside the furkin dorian [Laughter] i agree yeah yeah it's definitely confusing and our seo isn't working for us yeah ferkin's just not one of those words that roll off the tongue is it anyways um so we're doing another episode on uh the scripting patterns uh so shout out to travis olsen on his blog that started this whole thing um i believe we're on the uh we're on the last one which is supposed to be the best one is that right it is supposed to be the best one and so uh we'll get right into it um so yeah let's get to it yeah so um this is what travis would call the module pattern um and it pretty much the goal of it is to take all the best things from all of the other ones whether that was the function pattern whether that's the class pattern whether that's the namespace pattern and really put it all all together and so i pretty much the pros is it allows you to you know organize your functions under a single name um it's self-executing so you'll notice this like function here so it will just execute when called it does support private functions and private variables as seen here and although it's not actually shown here that it's private i'm just uh i'll we'll show where it will become private and i i put this other pro that you normally can't test the private easily right so after it's final you can't but it's relatively easy to to expose it for testing purposes and then hide it later and so we'll we'll show how you could do that so just some of the cons it's not the servicenow standard obviously and there isn't syntax highlighting so you may you may see that relatively uh uh quickly so um what is nice is there are examples of this though utilized in service now so if you ever get stuck or want to know how an advanced thing works in it you can look up some of these examples for it all right so diving into what we've done and how how it's different so notice here it looks very similar to the namespace right it's this var equals the difference is is we're now self-executing so it's just a function that executes directly and then within it you you know utilize it as if it was the namespace or just a set of functions that you that you have that you want to be called so for example in our set lockout you can even call other functions within here just by referencing it so that's it's not like an object um so it's there's no this in this case right um and at the end so after you define all your functions what you want to expose very similar to like the javascript export module is done by the return here so you're returning essentially an object that defines the like what is what is meant to actually be returned so in this case works we're saying that when you call this you have access to this function um which is you know defined here so but not your private function if you if you don't define it correct correct yeah and so it'll be relatively easy to expose this for testing purposes you just you know throw it in this return object as well and you can then go and test it so uh what does that look like in etfs so we have our similar atf pattern nothing has changed here first name last name whether they're locked out an example of whether someone is locked out or not and we have our test script and so inside here what you'll notice this looks very similar to the namespace one notice that it's instantiated without the parentheses so there won't be syntax highlighting um but the the biggest thing that we changed so that our unit tests passed this time is that since it is um since we don't have access to this private function it's going to equal undefined right so when you try to call this it's going to be equal to undefined but if you you know call our normal function that we do expose that will that will correctly return the right result right right um so let's go ahead and run this now and we're going to skip over break points but um first one pass second one past i like all the green it's good yeah so you know shout out to travis for the fun exercise and walking through on this you know he has the example uh and he even goes into why it's his favorite pattern in this case but yeah yeah i i don't know i i'm split here i still it's probably because i use the old original pattern the most you know so it's it's very common to me or very you know easy to to write because i'm so used to it i could i could possibly see myself moving to that that final one the module one but unless i have you know really good use case for why then i i just don't see the outlay of um what is gonna you know the the roi on using it right yeah i really and that's not it's not a good one i mean i'm sure it's got its uses i just don't i don't know my day and day day-to-day usage of script includes really i don't think requires it i guess so so my my my main thinking behind like what's the true like would always want to use it use case here is potentially if you're building a custom app for like the store um and there's probably more benefits that you would get out of that especially like if you don't want people to use certain private functions of yours like i think that's kind of where i would see this but okay yeah like your normal consulting or customer instance i i think you would just stick to the class pattern just you get all the syntax highlighting you get the ability to to jump into a definition you yeah you're it's a customer's instance so you like you have all of the acl already accessed for it and yeah well and you have to balance who you're giving your work to right you know if you're giving it to uh an admin group i i would be concerned about giving them something that they're not going to be used to either how can they continue to support what we've given them if we've written it in different ways that they're not going to be accustomed to you know so there's there's that aspect of it also i mean it's not like it's a huge change but you know i don't want to add to their workload so they're going to have to go through and try to figure out how we wrote stuff yeah so i think the the biggest takeaway for me on this is one like put everything in script includes i think that's kind of why we started this right someone keeps calling me out on that yeah yeah and uh and two you have a pattern for atf to test it right so like you don't need to use a background script to test every time and this way you know when people are changing your your scripts you just run your atf's again and you can determine whether or not it's it's possible to deploy or even plug in some automation there yeah i agree i like that well good you know this was super fun i enjoyed it uh i'm glad we had uh five five different uh patterns to talk about yeah yeah and i just wanna give us you know a little little clap [Applause] and uh yeah i had fun and you know if if the community has other things that they want us to explore or walk through or figure out you know feel free to comment or let us know and we're we're happy to to have people on and even you know jump into some of these things so yeah if you'd like to be a guest drop us a line [Laughter] awesome well thanks everyone

View original source

https://www.youtube.com/watch?v=3v4mlQIUmvI