Script Action in ServiceNow | Script Action Practical Implementation | Script Action Use & Benefits
hello friends welcome back to my youtube channel basico servicenow learning so in today's video we are going to talk about script actions in servicenow so let's see what we are going to explore in this video so we are going to explore that what is script action how to call script action for example in business rule or in any other server side script like script include workflow ui action etc what are the benefits of script action and we will also implement a use case scenario to understand the logic behind the script action that how to execute how to call right okay so let's proceed with what is script action so script actions are server side script that execute asynchronously when event is triggered right which says that script actions are server side script right and execute asynchronously so the asynchronously here is that it will not wait for response means when script action script gets execute then it will not wait for any response or it will not create any unwanted latency or bad experience and user can perform any action while the script action script is executing on the background right in other words we can say that when event is processed then script action gets triggered means script actions is not a script that we write and execute in like business rule or script include right here for that what we have to do we have to create an event first and when that event is processed or called in any of server side script like script include like in server side ui action or in any business rule then the script action which is linked to that event will get execute okay so the final thing is that we need a event to trigger script action right each and every word which we have mentioned over here will be clear once we will implement the use case scenario right we can connect each and everything what we are mentioning over here now let's see how to call script action for example business rule or any other server side script that we will cover in a use case okay now the another part is that what are the benefits of script action so the benefits of script actions are many right because it is basically a asynchronous script so we can use it for modifying configuration item we can also use script actions while dealing with large amount of data by integrations or for example let's say we have to make hundreds of child incident uh close right so we can use script action okay because if we will use a synchronous script for that then anyhow user has to face a latency or a delay right so for that we can use the script actions even though we can use in many other scenarios let's say if something gets approved something gets cancelled okay and we have to update some data in other tables there can have lots of examples so the benefit of skip actions as we have mentioned over there that it is basically a asynchronous script or we can say asynchronous server side script which will get executed through event okay and we are also going to implement a use case scenario here so let's see what is the use case scenario which we are going to implement today to understand script action in detail so the use case scenario which we are going to implement today is that when configuration item is attached to an incident record then command field of that configuration item will get updated with the required information as mentioned below so to understand the use case let's navigate to a servicenow instance so friends this is my service now personal developer instance to understand a use case let's open incident record first mostly i give example of incident because so that it can be understandable for everyone either he is a beginner or he is a like a experienced guy right so here we have this form incident form here i have created a custom field that is affected configuration item in case if you don't have that field you can create that field in your form if you want understanding of that i had created configuration field see the field name is you underscore affected configuration item this is a reference field which is referencing to a table that is cmdb underscore ci underscore server okay so let's say if user select any of our configuration item over here let's say this as400 okay then what happened let me open another table to give you understanding that is the table which we are referencing over here okay let me give you example that is cmdb underscore ci underscore server dot list this is a way to open a list view of a table in a new tab so here we are using this as 400 right so let me open this so in this form or in we can say in this ci we have a field called comments right so as per use case when configuration item is attached to an incident record let's say we have attached a configuration item here in incident record and save it then the command field which we have here in this ci will get updated with the information which is mentioned in the use case let's say here let's say we attach and save the incident form then the command which will be get updated over there is that this ci is attached to an incident on this date and the incident state is whatever the state is okay the same scenario if the incident if the ci which is attached to an incident is removed then this particular message will be displayed over there with this information which is marked with the hashtag same as for if the incident is dissolved let me give you a brief of that so that when we will implement that you can able to connect the things so let's say if user put affected configuration item over here fill the information let's say apple tutor let's say uh description right and here must be some state right and he saved the form it means in this incident record he is using this affected configuration item with any issue or something else right so this particular ci will get updated with the command that this incident is using this ci or affected ci right on this date and the state of the incident is new okay if this incident is resolved and the ci is attached to it then the command gets updated that the attach incident or the incident which is using this ci has been resolved on this and this date okay so what will this help is that in case if any incident is using any of the ci at least they see i have understanding that what all incident is using that ci and what is the exact status of that incident okay so whomsoever is open the ci will able to understand the exact status of an incident record okay so this is the functionality or the use case which we are going to implement today in servicenow developer instance through using the topic that is script action we can also implement this functionality through various other things but for the understanding perspective here i am using a script action to give you an example of that that how a script action can be used by modifying a ci record okay here we are modifying the ci record through incident record okay so as i have mentioned in the definition that we need event to trigger script action right so means we need a event first and then we have to write a script in a script action okay so let's see how we can create an event let's navigate to a service of personal developer instance here see in printer navigator if you type event see here at the bottom you will find system policy here we have this registry this registry is basically used to create an event okay so we can give a name over here let's say click on new so whatever event we are creating over here with our name that even will be called through any server side script let's say if we are executing a business rule or we are creating a script include then we can call this event from there and we that event will be linked to a script include script action and that descript action will get executed whatever script is written description will get execute so that gives an example of event name so let's say we are updating a particular table that is cmdb underscore ci underscore server that what we are updating we are updating the comments okay so you can give any name over here but make sure there must there will be no space in between the name and the name if it is in like small letters then it will be fine so here giving a meaningful name will be helpful for you right so i am saying that i am updating a table command section right so i have given update dot cmd underscore say industrial server dot com and if you want to give a table name you can give over there but i am not giving over here here we have other fields the application queue so i am not giving any values over here i have already created a video for event you can navigate to that video and you will know about other things as well so here i am creating a event just to execute a script action right here i'm saying fired by so from where we are going to fire this this is only a text field if you want to give some meaningful information over here you can give over here let's say i'm saying i'm just fired it through uh business rule okay so in this video i'm giving an example where i'm executing us even through a business rule and then script action script will get execute and whatever the functionality we have mentioned in the use case will will be executed so by business rule i can give the business rule number name over here but i have not created a business rule so let's get a business rule also before saving the form because the things which we need over here three things business rule event and script action so here we are creating an event here i am creating a business rule let's say here business rule so let's first create a business rule let's say business rule name can be update affected ci okay select table incident because when we are going to save the incident record with affected ci then this business rule will get execute so i am creating a after business rule over here means after the form gets saved now for the data save into database the script will execute it i'm saying on insertion and updation this will get execute right now when to run so if i'm talking about here means when user is changing the affected configuration item then the business will get execute or when the user is changing the state to resolve any other state but the affected configuration item value is available over here then the business rule will get executed means it will update the data in particular ci right so let's give some conditions over there business rule here we have a business rule let's say i'm saying a state changes my state changes then it will execute or when affected configuration item changes right then it will get execute but the condition is that this will not be blank otherwise where there is a requirement right if this field is blank then we don't need data over here right so here i put that and affected configuration item is not empty right so this is the condition when a state or this will get changes but this particular fill is not empty then the business will get execute then we have an advanced section where we have to write the script okay so let's save it first and then we will write the code so here i have a business rule with some name but i have to create a script for that means i have to call an event from this business rule now in here in this event i can mention the name just for understanding right for here means acquired by this business rule right let's save it so you are i think you are just thinking about that i have not mentioned anything in the event and now i'm going to execute the script action right and how it can be done yes it can be right event is a very basic thing to just populate it's it's basically a small form where we have to give some values okay here the main thing is business rule form where we are just triggering that particular event which we have created over here so from here we have to execute the events so the way to execute the event is gs dot event q okay so here it is mentioned what all things we have to give we have to give the event name we have to give a glide record object then we have to pass some parameters or any other things right so we have already created an even name over here right then we have a glide record object that we can mention as current then we have two parameters when which we can pass from here means while executing a business rule we want the two parameter values will be passed to a script action that script action can use while processing the script okay so parameter one and parameter two okay guys so here this name of event is this so i'm saying execute this event right then record object that is current now we have to give two parameters that is param one and param so here what values we can pass over there to achieve the functionality is that i need this affected ci cis id means okay so that at least i can create a glide record which can navigate to a particular record and update the things in it okay so for that we have two things that we need a previous uh society of that field and the current society so here we have two object that is current and previous so through current whatever is the current value we can fetch and through previous the previous value which we have earlier right so let's say if i am saying previous dot the field name is you underscore affected underscore configuration item another one is current dot the same field right so these are basically two things now i can say variable previous ci right here i can say variable current ci all right so it can be a possibility that while i'm doing this you are not able to connect but believe me when you try by yourself definitely you get uh got to know that what i am doing over here right so here what i am doing here is that this will give me a society of a previous affected ci if there is and this will give me a society of a current ci if there is okay now there can be a possibility that while user is doing changing this state might be he is changing the configuration item right it means we have to also validate that while doing the things is the ci change let's say if this is as400 and while doing something he changed the ci to this car one means he realized that that was not the ci which was affected this is the ci which gets affected right so what we have to do that we have to now update the comment in this car one that this is now attached to this incident and we also have to update the command here that this is the ci from which is removed from the incident right getting my point so we have to write a logic accordingly okay so for that what we are going to hear is that we just create a flag let's say where flag can be like change ci okay this is a variable okay which i'm going to say that if both if the ci change then the flag is true if the ci is not changed in between then the flag is false so here i can say if if previous ci equals to equals to current ci means this is not changed because both are same like the previous one or the current one while saving the form it will validate that whatever the value that particular record previously have have while saving it or updating it again that current value is same as the previous one it means the flag is not changed i am saying that change ci flat equals to let's say false yes flag is not changed because i'm assigning a string value to it okay now else the value is true right true sorry true so here we are just passing two values that is true and false and giving to a flag accordingly so in event queue a while i can say executing event processing event triggering event you can see anything over here so here i have give the event name here i have the uh glide recorder object now i have to pass two parameters so the first parameter which i am going to pass over here is this flag okay let's say copy and this so whatever value accordingly this particular flag has will be passed to a script action getting my point this param1 now the another value which i am going to pass here is the previous ci okay because it is easily we can fetch the current ci in script action but we cannot fetch the previous ci value over there because that is the async server side script and it's a bit difficult for us to fetch the previous value in async business rule or any other async uh server side script so for that i'm just passing two values over here in case if this particular value has a flag it will get passed through or false accordingly and if the previous ci is reared then whatever the society of the previous ci will get passed by script action okay so through this way we can call a event in a business rule what we have mentioned in the slide right the same way we can use in workflows script include or server side ui action to execute event okay so when the event gets execute by default if we have a script action which is using that event will get auto execute okay getting my point or not so here we are done with the business load so let's save the business rule now okay so here we are done with the business rule and here we are done with defining the event okay so now the final thing is script action for which this we have created this video so here please type script action system policy we have a script action click on script action click new over here so as we have mentioned over here that we need even to trigger script action so we have created that event or we have called that event in business rule right and now the script action will get trigger so see how so here we have to give a script action let's give a name over here script action field let's say update update ci comments okay now here i have to select the event name so event name is update cmdbci server comments okay so as we have created the event over here that event is displayed over here now you are able to connect right here we have a business rule where we are calling the event here we have that event right and that event we are using in a script action so anyhow we have we are making a connection okay so when this particular business will fire this event opposes this event then here in script action we have that event okay this event is called now execute the script whatever we have mentioned over here now we have active flag make it true because we want to run the script now we have execution order you can give accordingly right here we have conditional scripts when the condition gets met then only the script will get executed so if you want to give conditions let's say current.state equals to something a current dot particular field is uh equals to something or any condition which you want over here you can give here right i'm not giving condition over here i'm going to write the script directly because whatever the condition i need i have already mentioned over here in business rule when to run right now what we have to do here is that in this business rule we have two parameters that is this chain ci flag and previous ci so what we have to do we have to capture these two values in script action so from how we can do that let's say variable previous ci [Music] because here in the second one see we have param one the previous ci we are capturing the program two and here we are capturing in parallel one okay so here is param so for pattern destiny let's say where ci uh sorry change ci flat equals to even dot param one okay so in this way whatever parameter we are passing through business rule in event queue this parameter and this parameter we are using these parameters over here and storing those values in this variables now okay now for logic perspective we also need incident number so i'm saying ink number how to get instant number current dot because we are see here we are passing the glide record that is current it means we can able to fast the values current values of incident records right so here current dot number okay now this we are done with that we have and the most important is current id right we have this previous id so let's see current ci sys id so that is current dot and the field name this one because we are directly getting the value from the reference field it means it will return us the society of the particular field that is affected ci let copy the name from here this is a big name i don't want to write again and again just here it is mentioned over here so we have defined four variables for better understanding let me put together control here so here these are basically two societies of a configuration item this is basically a flag whether whether ci is changed or not it will tell true or false from the business rule and this is the incident number which get involved while affecting the ci right now let's write the logic now so let's say update ci comment this is a function and i'm just giving two parameters over here the first parameter is change ci flag and another parameter is previous id okay the same function i am going to use over here okay okay now let's begin and define the code so there can be three scenarios which we have to see the first scenario is when the attached configuration item is not changed and the incident record is not resolved let me give you a brief means when this is not changed and the state is not resolved then what will happen right the another scenario is when the attached configuration item is changed and the incident record is not resolved means somebody change the affected configuration item and update the form then what will happen means whatever script we have action what we have to mention in this command section of our ci and the last one is when incident is resolved let's say if any affected c is mentioned over here and incidentally resolved then what information what we have to populate in this ci right okay so let me navigate to this here first condition is when ci is changed and incident the ci is not changed and okay so means if current dot state is not equals to resolve okay not equals to resolve so you can find that resolved value from here show choice list type incent over here you will come to know the result value is six okay so you can say when current state is not equals to six and ci is not change means flag is false right because ci is not change it means both the series are same so in in that case we have mentioned over here if both the sides are same then the flag is false right so here i am saying flag whatever parameter i am putting over here ci is equals to false right this is the first condition now the another one is let me get a frame first okay when ci is changed when ci is changed and incident is not resolved okay then it means if paste over here and say ci is change it means flag is true right and the another thing is when the incident is resolved okay then it means if okay and let's remove this so here we have these much scenarios right where we have to write a script okay so for that what we have to do here is that we have to use a glide record right so let's say that xci okay where we have to trigger this glide record as i told earlier that we have to use in the table that is ci table this table right i told you all right that is cmdb underscore say industrial server right so i'm copying from here you can also check in the incident record that right so i'm saying here cmdb underscore ci server now dot initialize that dot query okay so the query is as we are getting a cis id of a previous one even though the new one we also have a society of the current ci and the previous ci so here what we are seeing not change it means we are using the current ci so i can use the current cis id right so here i can write the query like sis underscore id is this filter the records okay ci dot query right ci dot next right okay then what will happen then it has to update the comment section of a share record right so here type ffci dot name is just a second comments okay co comments equals to with a message which is mentioned over here let me see a message here the message is that the ci is attached to incident number on this state the incident is this so the message plus the incident number see here we can use that incident number right this is attached to this instant number one date right plus gs dot now okay and the message is ordered time the incident state is this so here we can say straight state right now what we have to do see every time if the command gets updated here in this here here so every time it will get override right so what we have to do the new command will become on the top and the previous command will go down so for that just say that we have to backward slash and plus here so what this will do whatever the comment previously it will have but to the slash n it will go down and the new new uh see this text will get on the top right so we are done with this scenario now the same what we have to do for here also so now it is same when ci is changed incident is not resolved means ci is changed it means we have to figure out the previous one and the new one both right so i am saying see this is for the new one it will update the value of the new one and for the previous one i am also again creating this let's say that i dot initialize right means through this let me let me just give you example over here through this we are updating updating the command of existing attach ci okay and through this we are updating the comments so here i am again initializing this particular object which we have created for the glide record and now ci dot add query now here i have to use the previous ci right so you remember that here we are just asking for two parameters flag and the previous cid so we have to use this previous ssid here we have stored the value right for this use to just update the previous ci record right so here i'm saying just underscore id slash comma and here we are using this value right now ci dot query vector ci dot next okay so what we have to do we have to update the commands just copy this comments paste it over here now the comment will be that this ci is removed from this affected ci field on this date and time the incident state time of removal is this okay so this ci is removed from removed from this incident number on this right and the rest part is the incident state is sorry the incident is stayed at the time of removal is the incision state at the time so from in this way we are taking care of the current ci even though the previous ci in this particular scenario right here we have already taken care of the current ci because the ci is not changed and now if the incident is resolved then so now copy this uh affected ci1 copy and paste it over here all right we're done with this so here if current equals to this this this initialize this this this this and i can mention the comments which is mentioned over here that this incident is resolved on the central time so this incident this incident is resolved this incident is dissolved on this data in time just a second here i think let's see here and i have to mention this okay so we are done with the scenario there can be lots of scenarios as well for this particular thing so for the demo perspective i have only mentioned three scenarios the first scenario is here right when ci is not changed and sedentary is not resolved when ci is changed and incident is not resolved and when incident is resolved and what will happen so when the incident is resolved it will only mark the command with whatever the civ have right that the cie is resolved on this this kind of a thing or this incident is resolved on this date and right so we are done with this so in this way we can write a script in script action so friends we are done with this script action we are done with the event we are done with the business role i believe now you have an idea right how we can configure or how we can write a script action and event and business rule and how this particular connection will happen to get the thing executable right so now let's we have a demo first let's see whether this code is running or not so that we can make changes if it is not running so let's open incident over here incident so here we have incident okay let's put ableton let's put some configuration item over here that is as60 then test short description and the state is new let's save it so once it is saved let's see this is we have let's refresh it the changes not done over here might be there is something missing in the code section we have to check it right so let's check the code so here we have this stencil black previous okay we have not mentioned over here that it will update right so we have to say that affected ci dot update that is the blender right we have not write this so how the command will get updated control c uh just a second place it over here and paste it over here and here paste it over here let's save it right we're done with this now so again let's create some incident record let's see and let's see test short description and i'm just putting any here just save it here that's a reload form see now let's get updated see here in this way this incident whatever we have created over here we are using this ci now it is showing us that this ci is attached to this incident on this this date the incident state is one so it is showing one but it is not kind of for like understanding for us so for that we also need to write something right which will display the exact state so for that go over here in the code uh where we have that code here we have so here we are using the incident state right so where we have the state this so we can say here just a second make me a little bit so that you have understanding of it so here we have currently state put this get display value plus and here we don't have that right only two scenarios here we have that here we have that and here we have that right so let's update it i have not mentioned logs over here while doing a coding you can for debugging you can use gs.log right but i have not done here and even i have not commented a lot because it needs time right it will take a time you can do commenting in this way while writing a code that will be a good practice so okay we are done with this now again let's change some cis to something let's say car one save it once i save it let's see what happen reload see updated so here it is saying this ci is removed from this because now what we have done is that we have put that into car one and the previous one we have removed now it is saying this ci is removed from incident days you can change that language okay so this ci is removed the incident is straight from the removal is new now see the text is displaying over here let's open that car one that that previous one or the new one get updated or not see in car one we have a state the ci this number is attached see here we have attached the ci right and the date and the timing and the state is displayed over here and this one is the previous one right here it is saying that removed okay so this is the basically scenario which we have covered right that when the attached configuration item is not changed and the incident record is not resolved the first one when the attach configuration item is changed and the incident record is not resolved the second one and now the last one is when incident is resolved we have to execute it right okay so who is working i believe third will also working what you have to do you have to try by yourself the script action and you have to let me know through the comments that you are able to execute the third one or not okay and if any of the scenario is missed over here you also have to tell me through your comments okay this can also be a possible scenarios which we have to cover while updating the ci through the incident record using script action so let me give a final brief of that what we have to do is that first we have to get a business rule event right first we have to create a business rule event other than business we can also create a script include or any server side script but the way is same which we have to execute in this way okay and for that we need a event which we have created over here then we have to create a script action where we have to mention the event name we have to write the script and this script will get executed asynchronously right and it's a server side script i believe now you have a better idea of script action right in case if you have any question related to script action related to this script feel free to comment below in comment section i will also post all this code which i have written over here in script action is business rule and event even though event it does not have any code right but in business will i have that code i will post that in my website you all know that basic or servicenow learning i have a website over there i will post this code over there i will also share that link in my description of this video you can get that link you can go and navigate the code in case if you find something new in it if you find something like you can write some other code or which is beneficial or some short code let me know okay friends so if you found that this video is helpful for you then please do subscribe my youtube channel please like and share this video to others thank you friends have a nice day bye bye take care
https://www.youtube.com/watch?v=M3PXXMYv2JQ