#2 OnLoad 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 with service now this is part of short series of learning client scripts in servicenow in this video you will learn about onload client scripts in service now so what is onload client script onload client script runs when user opens the form and before user can enter any data on the form it is usually used so set default values on the form or showing a message or pop-up while form is opened so overall if you want to take some action on the form when form is being loaded then you can use onload client script and if you have to create onload client script then you have to select type as onload on the client script form once you will select the onload type you will see a function on load which is automatically created or populated in the script field and in this function you can write your own code as per the action you want to take you want to perform as per the requirement you have from your customers and clients so whatever action you want to perform on the form while form is being loaded then you can write that script under this function major use cases of onload client script so showing p1 message alert if the priority of the current incident is p1 setting logged in user in the caller field on incident form so let's say i'm clicking on new button and it is loading the form now while form is being loaded i want to populate the logged in user on that field that is caller field and the next use case is showing message on the top of the form if user is vip user the system should check if the user is vip the user in the caller field is vip or not if he or she is a vip user then there should be a message on the top of the form let me show you some practical examples of onload client scripts and i will be showing you few scenarios and the first scenario we have is when p1 incident is opened then there should be an alert which says this incident is p1 incident that means when we have existing incident which is a p1 incident and if any user is opening that racket that means it will open the form and when that form will be loaded then users should see this alert that this incident is p1 incident now in order to achieve this requirement we have to create onload client script so let's go to my personal developer instance and we will directly go to incident maybe i will go to list of incidents and here i will go to client scripts i have client scripts over here so these are all client scripts for incident table i will click on new i will provide a name over here let's say populate alert for p1 i will keep desktop as ui type i will select onload type now here we have this function you can see it is automatically populated now here under this function i will write all of my script so whatever code i want to execute i have to write under this function so as per the use case if the priority of the incident is p1 then i should see that alert message so before i show that alert message i have to definitely check the condition condition of that particular record so in that case what i will do i will first get the value whatever value we have in priority field so for that i will just create a variable var and i will make it full screen i will make it big so that you can see the whole code properly so i will do priority maybe pri and then i will do gnscore form dot get value and here i will put the name of the field so i will do priority and then now i have to validate it so i have to check the condition so if pri equal to equal to if it is one if the priority is one that's a backend value then that means this is the condition we are using so if the priority of the incident is one then i should see a pop-up and that pop-up is so i will do alert semicolon and i will do this is or this incident is a p1 incident that's it so we have got the value and we are just populating that pop-up basically alert which will tell this incident is a p1 incident but it will only show if the incident priority will be p1 so in that case i will click on save so i'm going to create this client script so client script is created i will minimize it and now i will go to incident list and i will open any racket maybe the p11 where we have this critical so let's open this one first will i see any alert so you can see i'm not getting this is a browser alert i think this is something which is different i think this is a test i created so this is not the same alert so maybe i will disable this i think that is better so that we understand that why exactly i'm getting this alert so let me just disable that quickly so i will go to client scripts so i have this test you can see maybe i will make it inactive so i will make it false to deactivate this i will make it false and if i open the list of rackets again and open any racket incident bracket so now you will see that it is not populating any alert because we don't have p1 incident here so it's not critical and what i will do now i will go to this list again let's open this one that's our condition if i will open this incident it should definitely show that alert so if i click on this one you can see it says this incident is a p1 incident automatically and it is showing this alert when form is basically loading so that's how you use onload client script let's see another use case the second scenario for onload client script is when new incident form is opened then caller should be automatically populated with current logged in user so whomsoever will login to the system and we'll click on new button that means the new incident form then we have the caller field that that caller field should automatically show the name of the logged in user so if i go to my personal developer instance and if i click on create new as of now out of the box you will see that it is not showing any user in this particular field but that is the requirement that i have to show so if user is clicking on new button then when form is getting loaded then user's name should be populated over here automatically now in order to do that i again have to write client script because this requirement can be achieved with the help of client script so i will directly go to client script then i will click on new button because i have to create a new client script i will provide the name populate color field populate color i think this is okay populate color i will run it on desktop here i will select onload same like we did before and now i have to write the script now i have to set a value now servicenow provides an api where you can set a value on the form on the client that means at client level you can set that value how can you do that you can do it with the help of g underscore form apis and its methods but if i have to set a value how exactly i will come to know about the current logged in user now for that i have to use another apis so let's do one thing let's first get the user so maybe i will i will get users says id and then i will also get basically display name of the user's society the whatever user record i have i will just try to get the display name as well because that's what you use in uh when you when you set the value on the form so what i will do i will do u sys id equal to i can do g underscore user dot and here we have this user id now this will give me the society of the user see if i will do tab semicolon and then i do you maybe value and i will do g underscore user dot now this time i want to get display value and i think username should be good so i will do username idly i think the display value is full name but i think this should also work if i'm not wrong maybe we can put full name let's use that get full name so we will get the full name and now what i will do i will do g underscore form dot i will do set and that's what you use for client scripts if you have to set a value on the client you use gender score form dot set value i will make it big screen so that you can see the code properly now when you use the set value method you have to provide some parameters the first parameter is the field and that field is caller underscore id next is you can put the society so because it's a reference field so what i will do i will uh just put you maybe i will just copy this one and put it over here and then i will put the display value that is the full name so i will just do semicolon now is this code complete the answer is no and the reason behind it because if you will save this record save this client script then it will run for all situation but if i talk about the requirement the requirement is this functionality should only run for new racket because for existing racket it should not run it should not set any other value otherwise your form will be broken so let's say if you will write this script and you already have existing records where you already have caller feed filled and if somebody's else is opening that racket then his name or her name will be populated in the caller id field and that's totally wrong so in that case you will put a condition what condition we will come over here and that condition is so we will again use g underscore form api and we will use the method dot is new racket now this is something checks if the current racket the form which is being opened is it the form of new racket then only it will run so what i will do i will do is it a new form if the answer is true then it should run else it should not run so what i will do now i will just click on save so i'm going to create this client script that is populate caller and that is also on incident table now this time if i let me just make it small screen a little bit and i just go to incident maybe and if i click on create new let's see now you can see over here initially we will not getting our name that means because i have logged in with administrator so i was not getting this system administrator before before we wrote this client script but now you can see this is automatically populating even if i will uh maybe impersonate with any other user you will definitely say this but if i open this let's say i will open it for existing uh incident racket maybe let's let's yeah let's check it first with this id so maybe i will click on open and i will open any existing racket you will see we already have survey user basically this is not being replaced with my id and the reason behind it because you use that condition that if g underscore form is new racket and it is and if i was clicking on create news that was the new record but this is not new record this is definitely the existing record of the system let me impersonate with any other user so that you will get better idea that it is working for other users as well not just for admins so if i click on if i'm just impersonating that idle user and i just directly go to incident and i click on create new you will see that magic and you can see that my name is again automatically populated in caller field and i have impersonated you can say i'm logged in as well but as of now i have impersonated with this user that is itel user and my name is populated over here this is how you can create different types of onload client scripts as per the different use cases different requirements you will get from your customers and clients
https://www.youtube.com/watch?v=7QF0C6D9Cr0