#3 OnSubmit 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 servicenow in this video you will learn about on submit client scripts in servicenow what is on submit client script onsubmit client script runs when a form is submitted by the user it is usually used to validate things on the form before submission of the form by the end user you need to select type as on submit to create the on submit client script so you have to select on submit option in the type field on client script form once you will select on submit option you will see the function on submit automatically created in the script field in which you can write your script to perform some action on submit submission of the form major use cases of on submit client script so showing an alert before creation of p1 incident that is one of the use case where you will use onsubmit client script stop user for creating the incident if assignment group is not correct as per the selected category and the third one is validate for mandatory fields on different forms so if user is submitting the form and you want to validate mandatory fields in that case you can use on submit client script so similarly you have different other use cases where if you want to perform some action while user is submitting the racket submitting the form in that case you can write on submit client script let me show you some practical examples of on submit client script scenario one when p1 incident is created then it should ask user to confirm if p1 incident should be created so if i am selecting the priority as critical in that case if i'm going to click on submit button i'm going to save that record then system service now should ask user that hey it seems like you are going to create a p1 incident do you really want to create it if user clicks on yes then it should create the racket else not so let's see how you can achieve this requirement in my personal developer instance overall this is a confirmation which you have to ask while user is submitting while user is going to create the racket in that case you will definitely create on submit client script this is my personal developer instance now what i will do i will go to incident list in order to create client script so i will go to client scripts directly from here we have client scripts i will click on new i got the new form and here i will provide confirm for p1 that's the confirmation box we have to provide so i will select type as on submit now one thing to note you can see this script field is empty but as soon as i will select this type it will automatically populate that particular function so we have function on submit now under this function we will write our code now what kind of box we have to populate to the user so we have to show a confirmation box that hey are you sure you want to create a p1 incident before submitting the incident so in that case what i will do i will start with capturing the value of the priority field that means it has to be p1 on the basis of that then only i will show confirmation box so what i will do i will make it first maybe full screen and make it a little bit zoom and then i will start with the scripting so i will create the variable that will be p r i equal to g underscore form dot get value and i will put the priority field that's our field and now i will put if condition so if priority will be p1 then only i should see that confirmation box so for that i will do pri equal to equal to one and i can just do like this and here i can put one let me check if okay and then now in this condition pri equal to equal to one in this condition i have to populate i have to show that confirmation box but the thing is on that confirmation box you also need some input from the user but the user can click on ok or user can click on cancel and basically the input we get on confirmation box is basically boonian values boolean values we get so if user clicks on ok that value becomes true if user clicks on cancel that value becomes false so then that case i will capture those values in another variable so in that case i will write u input and here i will do confirm semicolon and i will write are you sure to create p1 incident so that's the confirmation i'm asking from the user and now i will put another if because i have to confirm whether if user clicking on ok then i have to take that action that means i i now i can submit the racket if user is click on cancel then i don't have to take any action so in that case i will do u input equal to equal to true if that's true then i can do return true that means yes create the record else if i'm getting false let's say user clicked on cancel then i can do return false that's it so we have pri equal to this we are getting the value then and if i i think if i talk about the best practices of the scripting so you should always write comments so let's say for this getting priority value priority value and here i'm checking if priority is one or not and here i am basically showing that confirmation box storing the value basically the input from the user in this particular variable and then i'm checking if user has clicked on ok that means it is true then it should submit the record if it is false then it should not submit the racket so that case i'm going to click on save because i'm done with my scripting part and my client script is created now we will do testing so whatever script i have written i will do a quick test now so i will go to incident click on create new and here i will just select caller apple tutor and here i will write test for on submit client script and i will select impact and urgency as high so that it can populate priority as p1 that is critical and if i will click on submit so now it will run the client script and our client script started running the reason behind it because it got the same condition so we got priority as one so it's asking are you sure to create p1 incident if i will click on cancel the answer will become false and if i'm clicking on ok that means it becomes true so if i click on ok you can see it is now submitting the racket it is creating the incident so our incident got created you can see but that means this we we have been able to submit the racket say this is this incident is a p1 instant that's fine it's our old uh old client script where we were showing our message our pop-up so that's fine we will disable it later now what i will do i will just try to create another incident without p1 so i will just cancel this tab and i will click on new and this time i will not create p1 because i want to check whether it is taking same action on non-priority incidents or not not p1 incidents or not so i will do test negative test negative test for on submit client script and we have impact low and urgency low and priority is not p1 for sure and if i click on submit what do you think this will not show that pop-up if i click on this you can see it created automatically without that confirmation box and this incident is also created let's say if i click on cancel so i want to show you third testing as well because that's how you have to do all types of testing so that you can confirm that your functionality is working without any issue so i'm clicking on new button again i will try to create p1 incident test for on submit cs so this time i will select urgency and impact hi we have priority critical and if i click on submit i will get that confirmation box and if i will click on cancel this will not take any action if i click on action cancel you can see that it is not taking any action i'm not able to create the incidents every time if i will click on submit it will always show me that confirmation box if i click again you can see it is keep on coming so it will not allow user directly to create incident priority one incident it will ask to confirm do you really want to create it then only it will show that scenario 2 when network assignment group is selected and category is hardware then users should not be able to create that incident so this time again you have to stop the user but with the condition with another type of condition in previous example we were just confirming with the user but here we will not allow user at all if he will select network assignment group and the category will be hardware the user will not be able to create incident because maybe category should be different for network assignment group or maybe for category hardware network assignment group should not be selected it's a vice versa so in that case what i will do i will go to my instance i will go to incident again so we will go to incident list and i will go directly to client scripts so i will click here go to client scripts so we have configure client scripts and i will create a new client script now all other examples client script which we created i have disabled them so that they don't conflict with my current use case so i'm clicking on new and creating a new client script i will provide a name and this time i will maybe abort submission for network just a name i have given and here i will select the type as submit here this is on submit and what i have to do i have to basically fetch the value user will be selecting in a group field in a simon group field and also in category field so in group field it's a basically reference field that means i have to check the cis id so what i will do maybe i will check the society we have for network group so i will directly go to sysgroup dot list enter so i will just check the society of network group okay i think we have selected the wrong table that says user group that's right name i think i was thinking about cis underscore user table and that's the reason i just use says underscore group now here we have and let's search for network so i will do network i think it's wrong name network so here we have network so i will just copy so i will just copy the society because this is id i have to use in my client script so i will come over here now i have to fetch the values so let's say i will do where and i will also make it full screen so here i will put var group equal to g underscore form dot get value and the field is assignment underscore group and here i can do where and i also need to capture the value of category so for that i will just do where category equal to g underscore g underscore form dot get value and here i will put category now i will use the if condition because that's what i have to check so if group equal to equal to if that's the same sys id that's a society of the network group and and category equal to equal to and let's check the value of category field so what i will do i will just quickly go to the main form like main view of my instance and here i will just go to incident and i will maybe i can just click on new because i have to check the values i have in category field and here i can select category and i will just click on configure dictionary i have to check the backend value so here we have choices and the choice we want to select as part of the condition is hardware so that's hardware i will just copy this one come to my script and i will mentioned in the value over here so you can see we have this hardware option and now i will put the curly brackets and i will put that if condition matches like this that means if grip is the same network group and categories hardware the user should not be able to create submit this incident so what i will do i will quickly do false return false else and if user has selected any other category for that particular group then i can just do return true that's it and if i come over here i think you are done you don't have to do anything else because we are just selecting we are just checking both the conditions together with the help of this end and operators and i can just click on save so this will create my client script and now i can do testing so i will go to maybe incident and i will click on create new and here i will just select the apple tutor caller and i will select hardware and i will just do test and here i will select assignment group as network so i have network now if i will click on submit this won't allow me to do that if i click on submit you can see it is not taking any action because my client script is running when i am clicking this on submit button this if i am clicking this submit button it is basically running that on submit client script and it is stopping me as a user to create this incident and if i change the category let's say i'm keeping it as software now i have changed the category this time i will be able to create this incident submit this incident if i click on submit you can see now i can create the incident without any issue previously when i had this category as hardware at that point of time system was running that client script and i was not able to create i was not able to submit the form so similarly if you have to stop user or perform some validation while user is clicking on submitting and before submitting you want to perform some validation some kind of checks you want to do some kind of validations you want to do in that case you can create on submit client script in service now to achieve different requirements of your customers and clients
https://www.youtube.com/watch?v=lla0rWXuInA