#4 OnChange Client Script in ServiceNow | Client Scripts in ServiceNow
[Music] please subscribe to my channel and click on the bell icon to get the regular updates of my channel and do not forget to like comment and share hello everyone welcome back to sas word service now this is part of short series of learning client scripts in service now in this video you will learn about on change client scripts in service now what is on change client script on change client script runs when a particular field value changes on the form it is used when you want to perform some action on the client when field value changes you need to select type as on change to create on change client script and you also need to select the field on change of which you want some action to perform on the form once you will select the on change type you will see function on change automatically created in the script field in which you can write your code you can write your script to perform some action on change of any particular field which is also selected in the field name field now there is a difference in the script as well so if i talk about onload client script and on submit client script now the function which gets generated with onload and on submit they don't have any kind of parameters but if you will see on change client script you will see some parameters with this function you can provide these parameters and use them in your script as per the requirement and use case you have from your customers and clients and these parameters are control which stores dhtml value of the changed field then we have old value which stores the old value of the field when form was opened new value which stores the new value of the field which was changed by the user then is loading which basically confirms if the change was done as part of form load and then we have is template that's the last parameter for on change client script that is confirm if change was done as part of template applied on the form so maybe if you want to take some action that if change was done as part of the template then you might take some action as well so it's just to differentiate different types of actions so you can use these parameters as per the requirement you get major use cases of onchange client script showing an alert when priority changes to p1 on incident populating assignment group when ci configuration item of the incident form gets changed then you populate the assignment group maybe a new assignment group making assignment group field mandatory when category is hardware now making field mandatory you can achieve it with the help of ui policy as well that is the recommended approach but you can also check or make fields mandatory read-only with the help of client script as well and in that case we can select on change client script and make one of the field as mandatory let me show you some practical examples of on change client script in my personal developer instance scenario one when the state of the incident changes to on hold then work note field become mandatory now this can be achieved with the help of ui policy as well but as we are learning client script i'm just showing you the example that how you can write on change client script as well and make some of the fields as mandatory as well so let me go to my personal developer instance to achieve this requirement this is my personal developer instance so i will go to incident list just to see the list of client scripts i have so i will go to configure i will click on client scripts now this was the one which we created earlier so i will just deactivate this i will make it false so it is deactivated and i will click on new now so this is the new form and i will just mention work notes or maybe i will do make work notes mandatory monday three and i will select now here i will select type as on change so you can see here we got these parameters i got this function at the same time i also got this is loading parameter automatically so if you want to run something if you want to maybe check something on that loading of the form then you can use it over here but why exactly we do that because on change when you when you are basically creating an unchanged client script if you don't want to run anything on load then this particular uh code should be available in your own change client script so what i will do now here i will select the field as state because that's our use case so in this scenario we have state so if state will be changed to on hold then i have to make work notes as mandatory so what i will do now i will just do if so i also need to check the value the backend value of on hold choice so for that i will open maybe any racket in a different tab and i will just go to the state and i will do configure dictionary because i have to check that value backend value so that i can use that in my code and here we have multiple choices but that's fine i will do hold so here we have hold and the value is 3 and i will go over here now what i will do i will do if if i will use this parameter new value so i will do new let me make it full screen and zoom as well so here i will write if new value so i don't have to use anything like g underscore form dot get value because that value so whatever value is getting changed that value is already stored in this particular parameter and that's basically i would say important feature of on change client script in service now that the value which user is changing and the new value will be stored in this particular parameter so i can use if new value equal to equal to if it's three if the value is three that means it is getting changed and it's if it's on hold that is three then i will do g underscore form dot set mandatory i will use this method and i will put basically the field and that is work underscore notes and here i will make it true that's a boolean value because yes i want to make it mandatory and that's it you're done so if i click on save so this client script is created and now i will do a testing so i will close this one now and i will just directly go to maybe incident list in a new tab and here i will maybe let's open this racket i can open existing racket as well and you can see the state as new and work notes we have over here it's not mandatory so if i just select on hold so it's showing me on hold reason but at the same time if i come over here you can see work notes is also mandatory because it ran my client script so whatever client script i wrote it automatically run that when i changed this field that is state and if condition is also being matched that means it is on hold and that's what we wrote in my script and that's the reason this work notes becomes mandatory as we are done with first example of on change client script let me show you what all values are stored in the change function parameters so if i go to my script if i come over here you can see you can we have this on change function and we have these parameters now when we run the client script this client script when we change the value of a field what kind of values are basically stored in these parameters you can see that as well and that's how you can decide which parameter you have to use in your script as per the use case you get from your customers so what i will do i will quickly mention alert because i will try to print values in alert so that you can see and try to understand uh why we have or why we we can use these different parameters in what kind of use cases so i will start with control now control is basically for if you want to change the length of the field size of the field in that case you can use it because it gives you i think get access of dhtml content off of that particular field uh which you are changing uh so i will do one thing i will quickly basically copy this and put it five times because we have five parameters so i will copy this one as well this one this one i'll paste it over here is loading oh copy is loading and the last one is is template i will put here as well and i will just quickly save this and i will do a test and i will i think let's see if it is saved so it is saved and i will reload the form uh yes i can leave the page i am reloading the form now this client script will run when we will change the state and if i make it on hold or any state i want so i can just maybe select on hold so now you can see it says object html select element overall it doesn't give you basically value because it is used to change access the element html element of that particular field so that you can change the size and and length of the field now if i will click on close it will give me the value for second parameter that is old value if i click on close now this is the value of new because we had new and if i click on close again now this will show me three that's the new value which you have selected if i'll click on close again this will show me false the reason behind it because this change basically is not being done on load of the form so that's the reason i'm not i'm not making any change on load of the form so in that case you could just made it form is not getting loaded it's just changing the field so it is false as well and if i close that is also false and what is exactly this is this is is template because we are not creating or we are not performing this change with the template and if i click on close that's it so we were trying to change the state from new to hold so new back-end value is 1 and 3 is the back-end value for on-hold that means it was able to print it was able to show you all those values in alert box and that's how you can understand that what kind of values those parameters store so that you can utilize these parameters in your script uh to get some maybe to achieve achieve some different kind of requirement from your customers scenario two when category is changed then there should be a message about change of category on incident form that's a use case we have so i will go to my instance and i will go to list of incidents again and i will go to client script here we have configure client scripts and i have made other client skips false and i will click on new this is the new form and i will give alert on change of category that's a name i'm giving and then we have this on change and the field name would be category because that's a use case that's a requirement then when category is getting changed then users should see that message so i will come over here at the bottom i will make it full screen zoom this and here i will write the script now when this value is getting changed i have to show the alert so i will just do alert you are changing category from i can do plus here i will put old value because old value is captured in this parameter and then i will put this i will add another string value 2 from this 2 plus and here i will put another parameter that is new value because new value is getting stored in this parameter so i'm using these variables now these parameters and i will just that's i think nothing else because this time we are able to use the parameters as well and when user is changing the category i should just see this particular alert that's it so i will click on change so client is client script is created and i will go over here and i will directly go to list of incident and i will just open any existing incident so let's say this one and here we have this category so the requirement is that if i change the category then i should see that alert and if i will do that if i will change it to maybe software you will see that you are changing category from inquiry to software i just did not add space over here that's fine but i'm getting it because when i'm changing the value of the category field because this field is already selected in this client script and then it is populating this alert and i can see the same message which i mentioned in the alert so that's how you can create on change client scripts as per the different requirement you get from your customers and clients i have also published a lot of videos around examples of client scripts i will put into the description so you can see those examples that how you create different client scripts in order to achieve different kind of functionalities so thanks for watching this video have a great day
https://www.youtube.com/watch?v=iAmrt-75sBA