logo

NJP

What is a Script Action in ServiceNow? | ServiceNow Script Action basics

Import · Feb 27, 2023 · video

in this video we're going to be taking a look at what is a script action in service now how would you call it using an event but more importantly why you might actually use them [Music] hello and welcome to the channel where in today's video we're going to talk about script action before we head on over and do all the good stuff I've got a bone to pick with some of you because you've been a bit more it's over at YouTube have told me if you're watching this there's an 80 chance that you're not even subscribed that's not good enough so make sure you head on over and do subscribe before you continue watching the video smash the Bell icon do all the good stuff but most importantly make sure you share the love with all your servers now buddy okay so let's talk a little bit about what script action actually is before so essential recording service now where you're going to write some server-side script because it's server side script we've obviously got access to all the Glide apis that you would on things like business rules and script includes now to call a script action it's going to require an event so you're going to need to create an event there's two things that can be triggered by events one of which is notifications that's a little bit dull I've done videos on that go and check them out the other thing is a script action now that may or may not be a question that comes up with the admin exam so perhaps keep that up there so the purpose of this video right because I think script actions are massively underused perhaps people find other ways of doing stuff but this game is all about having these tools in Your Arsenal that you can you can pull out and you've got them at the back of your head that you can use when you're thinking about 10 . the other thing about script actions is we've got access to two objects in them one is current which is a Glide record which is declared within the event when we trigger it over it's typically the record that you trigger the event from right the other one is event object which is going to contain two parameters it's got some other parameters but mainly palm1 and palm 2 which again we can send with the event that we can access from the script action and do start with okay enough waffle let's go and have a look okay so let's head on over to the tool and uh I'll put my glasses on so we know he's getting serious if we go to script action um using the application navigator on the left-hand side terminal argue there um I think instead of me creating everything from scratch in this demo I think we're just going to pick on something that's already there there's a load of stuff that's already there and hopefully that that will work just as well so in order to utilize script actions we've got the three main components three yeah three three main components the first is um we need to create an event okay so you can there's other videos out there I think I've done one with notifications I'll show you how to create an event we're going to need to trigger that event from something so typically what you'll see on Doc sites and other YouTube videos is that events are triggered by server-side scripts so things like script includes business rules uh UI actions or the server-side bit of a UI action um things like workflows or Legacy workloads right that is correct but I also want to mention don't um discount or don't forget about flows as well we can also trigger events from flows so we can use actions within flow scripty stuff to trigger them but you can also create an event record from a flow and I've done a video on that as well so whereas you might hear it say yes you can trigger them by scripts don't don't forget about flows as well okay so if you're debugging plugging something always check there as well those are things that are going to trigger the event that you need to create then we need to create this script action thing which is what this video is about you know based on title um so let's head into script actions now at the Box this is a Tokyo instance and we've got 287 so it's quite a few to go and look at and see how they work right couldn't get a u to our instance I'm on the waiting list service now if you're listening um please give us more we all want to go and have a look and have a play anyway so let's pick on um a script action that I think will work I'm gonna go for well I don't know what I was writing there communicate work around okay so we'll go for that one let me just explain this in a little bit of context so this kind of business use case it's all about um you have an incident you've decided you don't want the root cause is therefore you create a problem and Link the incident to that problem whilst you're investigating what the actual issue is what the root cause is of the problem you decide you've got to work around that may be reboot a lot laptop right so you write that work around on the problem and you want to tell your users about it you don't necessarily want to go into all the 20 incidents that are linked to that one problem and write in the workaround so you would write in the problem you would click a UI action in this case which would then send the workaround out to users incident okay let's go and have a look so I mentioned you would click the UI action so let's go and have a look at that this is the communicate work around you action uh let me just move you're on that right this is a communicate workaround UI action which is on the problem table okay this here is the event that we are triggering okay so that event is called communicate work around let's just go and have a look at that event we head over to events here not event management it's events registry another one so we don't remove look at the event and here we can see the event when you create the event you're going to need to put on a table in this case it's on the problem table when I mentioned before that we've got access to two objects we've got current and event we'll cover in this case is going to be a problem record because this event is on the problem table right um one point to note here is fired by in description please please please please please please please when you're creating events from scratch please put these in um I've lost count the amount of times where I've come to an event that someone's created and there's nothing in here and then you have to go digging to go and find it but please do that your future self will love you for that this is the event that gets triggered as we can see here let's just pick the bottom one so IU action is triggering that event that event is now triggering the script action right so event name here and this here we've got quite I would say that is quite a simple bit of code right the one thing I want to point out here is on line three as you as I've said before a couple of times now we've got access to current the current object so current dot get unique value in this case would be the sys ID of the problem that it came from but we've also got event part one so you've got event Dot palm1 now you've also got I'll just write at the bottom here so we've got event one event too there are other attributes of events as well that's another video I think but the main ones that you see in user event dot palm1 event.com two in fact actually if you wanted to see and what other attributes you could just simply put um you can have a look through the other script actions are there and probably get an idea that way if you'd like to anyway so we've got um event part one event Palm two so what are they right what does that mean if we go back to the script action we can see the event um and there's different parameters so in event queue method there's different parameters that you will pass it the first one being the event itself the second one being the Glide record um in this case current and the third and fourth will be event part one of them Palm two so in this case we've got workaround current dot work around which is like a text field where you type the workaround that is event Palm one let's whiz back over here work notes it equals then Palm one so that's work around notes if we chose to put in another parameter I already put this one in called hello just to illustrate a point we could put in another parameter this would be event part two that we could call event Palm 2. if we were to do this so the code would be will it be um because I've just put hello it would be really awful the work note the the result of this worknote would be the workaround plus hello okay pretty pointless so in this instance what will happen when this UI action is clicked the event is triggered we've seen that from that the script action will be called and then um it'll take the the value of event Palm one put in the work notes and update um any relevant incidents so let's just talk about so that's quite a simple scenario now what we could do is we could call a script included if we really wanted to from the from the script in the script action um so you've got access to all the different server side apis as I've already mentioned let's just address why would you use a script action well script actions we've already said are asynchronous so if there's ever a case that you want to update multiple records and you don't want to do something in a business rule perhaps you could use a script action because the event is going to queue it's gonna kind of I don't wanna say drop the connection but the event is going to queue and it's gonna wait for the next slot in that queue in order to be processed so I guess you'd use a script action when you're not overly bothered by the time it's going to take um to action the script that you wrote right so in this case we know the work date word notes are going to be updated but it doesn't need to be in incident it doesn't need to be instant we don't need to click the UI action and wait for a response to come back right we don't need to hit that communicate work around UI action and wait for it to say yes I've updated all the 50 000 incidents we're okay in this instance to put it in a queue and let the queue pick it up and do it whenever it's available to uh to action okay so I hope you found that video useful I know it's been a short one it wasn't actually me writing the code and maybe that's a good thing but hopefully I've explained it enough that you understand the next time you're thinking about a solution just think about a script action it might just help you out until next time make sure you're subscribed give it a thumbs up share it with your service now buddies smash the Bell icon so you get notified every time I upload a new video just like this till then I've been service nerd thanks for watching foreign

View original source

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