logo

NJP

Inside the Barrel Pints - Script Include Namespace Pattern in ServiceNow

Import · Mar 02, 2022 · video

welcome to another episode of inside the ferkin where we take a shorter look at certain components of the day in the life of servicenow developers i'm joined with dorian so let's get right into it dorian what do we got for today yeah so today we got um building on top of our script includes so we're going to do a different pattern last time we talked about the function pattern and some of the pros and cons of it being a little hard to organize and you just have a lot of single function uses but so we're going to talk about how to potentially organize some of those functions using the namespace pattern so really taking it to the to the next step okay so let me share this screen and remember which one i screen i was on [Laughter] don't share the wrong thing no yeah we're good all right so so essentially following the similar um pattern you know we're following what this call is called the name space pattern um and really the structure is in the function pattern it was just some function and then it does some work in this case we're essentially naming or organizing our our our functions to do similar related things um and so it'll look something like this where you have this you know script include pattern it has some functions underneath it and then those functions just get called very very similarly now notice here some things to call out is there's no um like a parens in the beginning so that's something to to keep in mind and so going back to you know what we had some pros for it is it allows you to organize um your function patterns essentially so this is like a really good use case for like utility functions so if you did want to create a util like this is a good example and servicenow provides some examples out of the box that you can go and explore in order to to read about it some cons that i've noticed there is no syntax highlighting when you uh when you use this api so just keep that in mind that like when you do want to call it and i'll show it like it's not going to highlight that it's like it actually exists in the system and then you know continuing on the path of you know your properties and your functions aren't truly private okay i was gonna ask that because i saw you had one down there and i was like are they truly private or just private to you yeah not yet and and so so that this should look very similar i didn't do any other you know this code change is still the same that we had in our other functions um and similarly i created an atf for us to test this so again nothing nothing different here we have you know your first name and email and locked out for params we have an example of a a user that is locked out and a user that isn't locked out and then we have our three basic test steps where we create this user we then run our our script include and then we we do some validation in the script include but also validation after um after the script is done so if i click into this you know script again so similarly you know we're calling some variables um from the previous step uh before we run our test we then you know instantiate this object so notice here there is no uh parents here in order to instantiate this and then we do a little check to say if the user is considered locked out we then expect them to the private function to lock out is to return true and then we also want to validate that when we do set this locked out it didn't actually update the user record so that's why it says this should be equal to three um and then similarly we we have that other if case if they aren't locked out um then it should lock them out and and that's why this is here the reason i did just a simple not equal to three is because actually in our code after it it's going to validate that they are locked out so don't have to worry there so let's uh let's add some debug statements here so we can uh walk through this or sorry break points so one breakpoint two break points debug test pause before roll back and we'll run this test still favorite functions of san diego and let's go to our sys user table so that we can see that this user just just ran so it paused for our breakout we can see that the first one is is already locked out um we step over which should then run our actual function and so nothing changed here there's no updates and just to make sure the system is smart it didn't update and now our test should essentially pass and so now we're essentially checking is there anything else we want to explore here before which we don't so we're now finished let it roll back that user test passed and so now we go here and it should be a new user which is not locked out and the update is zero we step over so then our function gets called and you see that they are now locked out and there is an update that has happened on it so we step up uh oh we got a failed test oh boy man um [Music] expected true so so here's an example where um our test i guess is a good example where a test failed for this person um or maybe my test is is incorrect but the idea here is what's nice is like you can you know kind of get some you know information related to your test um so yeah that shouldn't have failed so let's see what my test is doing and uh [Music] it's saying that this should have been not equal to three um but let's see what other ones they've all been failing oh wait my last one succeeded weird let's uh i just want to run this again i'm very curious sometimes like oh no all right there's something wrong with their code sometimes there is some service now to blame but not in this case so one thing i want to check is how is our code different from our other script includes so if we go to our functions pattern this looks pretty much identical oh maybe they're these aren't identical no that yeah that last function is a bit different so is locked out user because you're returning true or false three yeah interesting yeah i guess i guess i did a terrible job of copying pasting so let's well because that [Music] that is the set lockout well so this is the this one which this looks the same right so if it's the set lockout um this looks identical to this and then the this one looks identical to this one okay i don't know good question but i mean we could we could spend a little bit more time debugging but i think overall i wanted to show that like that the the actual script is working and you know because we want to keep these short i won't go into to the full step debugging here to find it out but at least we're showing an example of like you know this is what your unit test would look like um if they do pass or fail and you can see that like what is expected um but it it it returned false right so you can you can you know go through here and and figure out um like what what actually happened right yeah so um but yeah thoughts thoughts on that from a like architectural standpoint of the the name pattern versus the function pattern um so to me i don't know i i'm so old school i just i i still love the original pattern you know the one that that everyone uses um this one uh the name space is close closely related to that or it feels like it's closely related because it works almost the same you just don't have the parens and it in it i don't see any like outside of that syntax highlighting which you know for someone who may be using vs code to write the scripts anyways probably isn't a big matter yeah but uh like this this syntax you're talking about right yeah um without the prince which i i don't know it seems like six and one half a dozen the other right if you're used to writing the the original um pattern and then changing over to this would be the easiest but i i don't see why you would you know i don't see a a reason to to move off the other one um and maybe it's just because i haven't had a chance to use any other you know like i haven't come across a use case where i've needed yeah to change the pattern yeah and it it's interesting too because like you know this person says the same thing right like the class pattern but it just doesn't have the class create and the new right so it it really isn't uh adding more value than just using the the servicenow class one that i can see of like i don't even know maybe there is some memory management that it does differently but i wouldn't be able to uh to determine that easily sure i mean even then are we talking you know millisecond processing differences or is there something more noticeable right um so yeah i don't know i like i said i'm old school and i still fall back to just doing the old original pattern just because that's what i'm used to and it works for you know almost all mine use cases yeah and i wonder if the reason there are these different patterns out there are is the same you know is is due to different um uh groups building things right so you have group a that is used to doing one way in group b that's wanting to do it you know they're used to doing something different you can see a lot of that like when i used to work on sharepoint you'd see a lot of that because there'd be these disparate teams that really just seemed like they never communicated with anyone else and so they built their own stuff in a silo and and you get stuff like that yeah and i think that's the importance of the overall thing here right is we're trying to find the right architecture to use for your script includes and even scripting so that you can easily unit test them so that's kind of like like choose a pattern and stick to it if it solves it and then if there are use cases where you need to use a different one use a different one so yeah yeah i i agree with that i do want to before we go at least lastly share the um where you can find some of these [Music] snippets so like let's say you wanted to to use this so what i have here is this so what we've done is from the last one we included like an example here so all you have to do is you know copy it into a script include um and so that you can find this at the cascanex you know inside the barrel code snippets and here's an example of the script include inside of the atf so so most of the things that you would you would want to need and then we just took a an example picture of you know what are all the pieces of uh the atf in order for you to build it so very nice very handy so we'll upload another one for for this pattern as well so that people can utilize it as an example perfect wonderful all right anything else before we go john how many patterns are left no so we still have the global include one which is which is actually a really cool pattern because it enables you to import like javascript from other places um so we'll do some some cool things with the global include and then we have uh one final pattern after that so we have two more patterns okay perfect well uh i guess with that um we'll say ideos thanks for joining us subscribe and uh have a good day

View original source

https://www.youtube.com/watch?v=7Z5ukm3Wf8s