logo

NJP

ServiceNow SDK: Using third-party libraries in applications. Installing and using a package

incident․do · Mar 11, 2024 · video

in this video we're going to be looking at using third-party libraries and applications in the service now SDK uh in a previous video we installed the SDK following the instructions here I'm in the docs site and I'll I'll put the link in the uh description so we installed the surface now SDK uh and then we created a project um using the link here create or convert an application so we followed this uh using the command line uh interface we created a project called um the heck was that called example app example app and then we uh right using this uh well first we set up our authorization our credentials uh then we use this command to create the app um and then we used I think we went down to here and we built the app using uh now- SDK build and then we deployed the app using now- SDK deploy so that put the app onto the server here uh onto the instance rather and if we look at this application in studio uh you recall that there was it's a simple app with one table a to-do table and one business rule um as well as it installed a couple of ecma script modules in the cisor modules table um which is the example app um actually this is the package Json and then the my function. Js so uh let's take a look quickly just to recap this is the app on the on on the local development environment so uh we have the folder structure here and in the source folder there's the my function. Js which comes out of the box when you create the app using the CLI uh so it gives us this function and all this function does is uh adds an info message for whatever the um old status was and then the new status another info message um and there aren't any uh well there are some uh third party uh there are some libraries that are being used here some packages um but there's nothing that we installed after these are just stuff that comes with the project when you open it up uh when you create it with the with the SDK CLI so the result of it is I'm in a record here and that table here so I'm in one of the records and if I update the status here we get these two info messages right and those are the info messages that we saw specified in here um and then again I'm still just recapping from the last video you can go back to that video to kind of see the all the details of this um the way these info messages get called is through business rule on that table right so I'm in a business rule it's on the to-do table and check this up a little bit okay and we have this uh require statement which Goes To Source my function .js and then we're doing show status update which is a function inside of this um my function. Js and it we're giving it two parameters here we're giving the the current value of the status field and the previous Val value of the status field so this maps to the module here right uh that's this module here my function and here is uh we can see it here but we can't modify it here this is exact same thing as we're seeing in the uh vs code okay so that's where we are so now what we're going to do today and let me change the let me get my scope lined up here to be in the right scope what we're going to do today is add a package to our application um and off sort of off the-shelf uh package if you want and we're going to reuse the code inside of that package so this is really the the power of the service now SDK is all those packages that have been created out there throughout history are all available to us well within certain limits they all have to be compatible uh with uh with the uh they basically have to be vanilla JavaScript uh and they can't be like specific for nodejs or there's some other limitations but uh still that's a whole lot of packages that are available to us so what we're going to do today is install the uppercase package which um you would probably I can't really think of a way of a of a use case for this in reality but it's just a simple package that uh takes a string input or has a function that takes a string input and makes it uppercase right JavaScript has this sort of uh natively so not sure why you'd ever need this uh but uh I'm I'm sure it probably has a use that I'm not aware of uh so anyway we're going to install that and we are going to put it in here and then we're going to create another business rule to call that so let's head back to the documentation and let's go to this one use thirdparty libraries and applications so we'll go down to the procedure here so nvs code open your scoped application directory so I have that fired up already uh right my example app I have that and we looked at this uh structure here so now it says uh from the application directory open an integrated terminal window okay we'll do that in a second and then install the uh npm package for the library so using this command mpm space install uh so let's fire this up here and I have my terminal window already fired up you can do that vs code uh over here with new terminal and so we're going to go npm uh space um upper case and actually before we even do that and I think we need some quotes there go find the package you want to install and they'll give you all a lot of useful instructions here so uh here's my package and uh I'm on uh npmjs.com and so here's my pack here uppercase and here's the installation instructions so we can actually just copy PA out it here it's safer than having to type stuff so let's delete this do this uh they have this D- save but you actually don't need that anymore we just leave it in there see if it complains so this is going to go ahead and install that package into the uh node modules uh directory over here that takes a few seconds to run uh now if you remember in the last video when we did the install of the service now SDK uh we use the global flag right so that installed it uh in general for the entire node uh installation here we're just installing it for this uh particular application so you can see we're in this Michael Local example- app so it's just going to install it in uh in this directory so we do get these complaints about um this service now and we saw this in the last video as well when we installed I can't remember what it was building it or something like that we see these complaints I'm not sure what's going on with that uh I think maybe because 16 is required and we're using a later version so uh but it it doesn't uh it doesn't hurt anything everything runs so we get this added one package um and now we're back here so now if we go into the node modules directory we should see uppercase uh here it is uppercase perfect okay so our next let's go back to our instructions here so we installed those and we looked at where that went uh okay now it's just telling us about the libraryies packages are added to your application in the uh right in the node modules directory we just saw that and the library is added as a dependency in the package Json file so we should have probably looked at that before we ran this thing just to uh prove that the npm actually modified it uh but we'll have to just uh you'll have to believe me on this one so if we go down to the package package.json file we can see dependencies uppercases in there now along with the the version number okay go back here so now in a JavaScript module import the library using an import statement uh in this example the module includes a name space for importing the low Dash module okay so in there here they're importing the whole darn thing uh and then they give you another example where they're importing a camel case from low Dash um okay so this can be a little bit confusing because they talk about in a JavaScript module and you might not know what what the heck is that so module is basically just a file um what we'll do uh and actually So the instructions would have been in here when we do create and use JavaScript modules in this previous uh section right this is where we uh in the source directory create a JavaScript or typescript file that contains the module code you want to reuse okay so that that's basically we're following this so let's go to that uh let's go back to visual studio we'll go to the source directory and we'll do new file and we'll call this one my function uh 2js okay let's go back to our right here and so three optional import the uh Global Glide apis to call them from a module in this case we don't really need that uh so we won't we won't use the we won't use this basically this allows you to use um I think all of the uh everything you get in the with the Glide apis uh server side uh so here they're using GS uh this was to do the add info messages remember this was from the example this was in the regular my function right they're using GS because they're doing add info message uh we won't be doing that in here we're going to do that in the business rule instead so we won't uh need that but uh if you want to do anything with uh Glide with glide classes you can import this in your function just for Simplicity sake we're not going to do that okay um but what we do want to do is import the thirdparty library at this point so let's go back to here to the usage and we're going to import um these two uh methods or functions uppercase and local uppercase from uppercase okay so again this is just the package documentation so we're back to our new module here my function 2. JS and I'll stick my import statement in there okay now you notice visual studio uh or vs code uh Grays it out a bit because it's not once you reference this then it will uh right see All Imports in this import declaration are unused once you reference it it'll sort of brighten up and be the regular color um okay so let's go back here and so now the next step is to export the code you want to reuse right so in the module identify the code to export with export statements and um okay so what I do here to keep it simple is I just copy something that's already existing so I see this is the out of the boox function so I just take this import uh block here go over to my function here I will paste this um and let's just modify it a bit so we'll change the name of the constant we'll change this to let's just call it uh test upper okay and we will just we won't feed it two parameters we'll just feed it one parameter and we'll call this uh input string okay that's the name of the parameter maybe that's not a great uh name maybe we should just call this uh uh string for transform I don't know probably not a great name either uh anyway this is the what will feed this is whatever string we want to make uppercase uh we'll get rid of these and let's go back to our documentation for how do you use the darn thing so you use it by just doing uppercase and then in parenthesis whatever string you want to uh make uppercase so we'll copy that go back here and paste that in here uppercase string so this is going to Output like it says in the documentation string right we don't want that but what we do want is for it to uppercase whatever we give it over here so whatever is in this uh input parameter so string for transform and let's see do we want to do a return there I think we do yeah we wanted to return this and then we're going to do the info message business uh inside the business rule instead of it here so this is just going to return uppercase of whatever we give it all right we're going to save that uh let's go back to our instructions here so we've created the module and entry points I think we don't need to do this because it's um I think that is uh automatic or this might be for right okay yeah uh for external modules and scripts right we're not doing that right now everything is uh inside of this application so let's go back here now that we have our module sorted out we'll go back here and so we were here in the JavaScript module import the library right we did that um and then call the uh call code imported from the library in your module to reuse it right so that's we we did that as well over here so basically we called this third party Library uppercase here and then we reuse it uh down here with an export right so we basically make this functionality available uh to the platform as we'll see to a business rule in the platform we make that available by using this export piece all right so what do we do next is build and deploy your changes to the instance easy enough so we can go over here and and uh so first thing they ask you to do is to do a fetch so this is important because if you have made some this will give to the latest changes if you've made them to the instance so if we were in here and um you know let's say I added a business rule or say I made some changes in here uh it will grab the latest changes from the instance that's why we do the fetch so let's let's go ahead and do that um in fact before we do that let's just throw something in here to show uh how it fetches so let's say yeah create application file we'll just do a say we do a business rule and we'll just call this new BR and this will be on the to-do table all right it's not happy with that let's call it by name for there it is okay oh it's two dash2 that's why okay uh when to run we'll say we'll run this on insert doesn't matter this it's not really uh and we'll just say I don't know set the status to DD okay and we'll submit that now if we look at the before picture so I believe um no not Target it is let's see where is this thing metadata uh update yeah so I think it is this and so right now so these are the objects that are or the artifacts that are part of the existing application right so we have this cscript right this is the existing business rule this is the uh log item change business rule that's uh this guy here and then we have a table and some forms and some list layout and I think those are more or less in here right the documentation this is like the labels uh this is the yeah here's the status column here's the task column um I think this is the table right uh this is the business rule this is the list this is the section the list view the section Etc so basically all the artifacts are in here uh in this update folder and these are the same artifacts that if we go to the application and we go to the application record and we see these application files um here maybe sort by uh it's these files here so there's 17 of them now this won't match up exactly uh because the uh ecma script modules are not downloaded somewhere it tells you that uh in a few places these are not downloaded uh only the other stuff so like the dictionary entries the field labels Etc so all the rest of that stuff um okay so we see there's only one business rule right now C script uh let's do a fetch so it is uh now- SDK fetch and so now it's going to go up to the s to the instance and grab everything off of there for this uh application and I think it gives you a little print out of what it what it did or maybe that's when you do the deploy actually I'm not sure it does that let's see okay um so yeah here we go right list of downloaded metadata files uh this dictionary this package inventory CIS app and then this guy here so here we go update SLS script right ending in uh C3 over here so if we go here and I think we got to refresh this uh we can now see C3 right this is the additional business rule that we created on the server just now uh what do we call that thing yeah see new BR the very descriptive new BR so that piece is in here now okay so that was the fetch that was the first piece so it's going to grab the latest um artifacts or metadata now uh step three is build your application using the build command and we did the same thing in the last video when we did the outof thebox uh uh project so we do now- SDK build go over here back to our terminal window now SDK build and you always just make sure you're in the uh directory for the application example- app and not in any of the child directories so this does take a few seconds to build and if you have any problems at this point it will send you some warnings um so for example say you uh say you import uh from a module that is not in the modules directory uh it's going to complain right here with One of These Warnings uh right so here's one of the warnings I was talking about earlier in this video about if you use third party npm modules ensure that they use only scripting features supported by the now platform so there's a list of things that won't work uh with the now platform uh right so a couple they give you some additional warnings on that don't use asynchronous function or disallowed nodejs or browser apis right right so this basically has to be more or less vanilla JavaScript and then yeah for more information now documentation okay we're still building okay so it gives you some information about uh where it bundled where it put all the artifacts [Music] um okay right building it generating the mod XML into the output directory right and the output I think is just a is just a um zip file I think all right I got to stop myself from exploring stuff while we're let's stay let's stay on track here so okay build is done and there were no um here here we go go single artifact admitted uh admitted as right all this stuff Target example there you go so it goes into the target uh directory which yeah right here right so it it zips the whole thing up into one uh okay great so now let's go back to our instructions again and now we deploy the app pretty simple um so they talk about using the oth and then the alias um since the one that we created is our default uh we don't need this piece we can just do now SDK deploy so paste that in there do the deploy so now it's going to take this file basically that we just built and um upload it into the server great that was pretty quick and it gives us a uh print printing most recent installation stats right so updated one uh and it gives you this is like the um what do they call that thing well if you're committing an update set and it uh talks about some of the conflicts or better yet this is actually closer to uh when you go through the when you update an application and then you have to go through the um uh I think it's skips or I forgot what it's called but uh you you go through the uh upgrade history table to figure out out uh what it might have uh skipped over so all right everything is good here and deploying okay so now let's go take a look on the instance uh at what it did here so right now we have 17 files I'm going to refresh this and I'm in the uh application record here for example app we're going to just reload that okay so now we have 20 files right uh what do we have 17 before I should have remembered that so the files that are new and maybe that's in the updated uh column that must be these 2033 yes so it added three of these modules right and if we Mouse over here we look at source my function 2 right that's correct that's what we created the uppercase module that's the package that we downloaded that uh my function 2 um references and then a package.json for the uh uppercase um because remember that a package will have other dependencies uh right so uppercase is probably dependent on some other uh file which I'm I'm tempted to click in there but I got to stay on track here uh so basically it's going to go and it's going to make sure that we have all of the dependencies for uh for the for the for the uh module um okay so now that's in there so our next step is we want to call this new module uh from and we said we're going to do this in a new business rule so we'll make this simple and and we'll go to our existing business rule the one that calls the other out of the box uh function and we'll just do an insert in stay okay and we're going to call this one um uh what test upper I think we called it okay when to run we'll keep this the same we'll run after every up update and in here instead of um show status update we're going to get this from and this is where I have to remember let's go back here and remember what we called it so we call this test upper test upper and we get this from Source my function 2J as and then we're going to call test upper and remember test upper I believe it just takes one value right uh which is the string for transform yeah and it then returns that value so let's give it um the current value of status got too many parentheses here okay we'll give it the current value of status and so let's do VAR um status upper okay and then we'll do the gs. add info message here test upper uh no sorry uh status upper right so I'm going to to send the current value of status over to my function uh that's going and my function is going to call uppercase the uppercase package transform it to uppercase and send it back we're putting it into the status upper variable and then we're just going to show that variable in a info message um so this is already running too long but I will just add this comment um though we saw earlier how you can do a lot of Glide record functionality or Glide class functionality in here using this uh package service now Glide right like they do info message directly in here um I think it's better to do as much of this as possible uh in the I lost my thing here in the platform uh and the reason for that is you can modify this in the platform right if it's in a business rule and I say I don't like info message I want it to be an error message instead right it's as easy as going in here and modifying add info to add error right but if this piece is in the actual module uh then I have to go into Visual Studio I have to fetch the latest I have to make the modification I have to build it I have to deploy it uh to get it back into the into the instance so there's a lot more steps involved um it seems to me it's better to do this kind of stuff uh on this side as much as possible um that's that statement is subject to change uh but that's my instinct in looking at this uh and the way it's built okay so this is what we're doing uh let's save this and let's see if we can let's give it a run and see what happens here so we should be getting both this uh existing info these info messages from the my function and then we should get another uh message from the my function uh two so here we'll just make a change here I'll save it and there we are you see so it transforms whatever I have in the status to all uppercase so all lower case yeah this field is like I don't know how they did that you can only put like 10 characters or something Save Right There we go again so it's uh it's doing that so this is really using a third party Library uppercase um to perform a transform and so I'm sure you can see by now uh I mean the power of this thing right I mean all those uh or many of those I should say uh packages uh that are available could be avail able to do lots of stuff in here really exciting really powerful and I hope to make some future videos on doing some using some actual packages to or I should say using some packages to do some actually useful stuff rather than just an uppercase uh to start doing some interesting stuff thanks for watching

View original source

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