ServiceNow GlideAjax Usecase : Unlock the Power of GlideAjax in ServiceNow for Date Field Validation
foreign [Music] so in today's tutorial we'll be focusing on one of the use case okay which I I was working on couple of days back so you must have seen a scenario or any organization that you get annoyed when a people when when a client create changes on the weekend right so today's tutorial we will be working on the same so we will restrict the user from creating any change on the weekends because we don't want to work on weekends you can consider so in today's tutorial we can see uh how we can restrict it out okay how we can throw some error message okay and um avoid users from creating the change request on the weekends okay so it's pretty simple use case and you can just extend the scope of this use case okay in your day-to-day activities okay based on your project requirements started so for this we'll be going to change this okay and what we we are planning to do is like we don't want user to select plan start date on weekend we don't want them to plan anything on weekends okay so what I will be doing over here let's open normal change okay this one for instance which is a new state so whenever users try to go to schedule huh cool ready to go to schedule and try to select the plan start date okay if it select Vehicles then we want to give them some error okay so for this let's get started so what we'll be doing first of all we will create a scripting tool where we will validate these things out okay so I will go to scripting field and in system definition I will open it in new tab this is a script include and we'll create a new one okay as we will be doing this validation on the on the server side on sorry on the client side so let's let's add something out okay let's make it client qualify so let's give it a name first so let's give it a name give a week day validation okay read the validation we'll make it a client callable and uh here we'll Define the function so let's pick a function um let's consider function um is so uh let's give the function name as is it a weekday is it a week the okay so that it will get 30 what exactly is doing today my keyboard got screwed and with your function and as it's a as we will be using this function in the Glide digest right as it's a client callable script include so we will not be getting the parameters via as an input parameter to the function but we'll be just grabbing it for from the get parameters kind of thing okay so all what will be a variable and started flash started equal to this dot get parameter so this will be the parameter which will be passing from the front and side okay let's give the parameter and what we will be passing will be passing a date so let's make it generic we will give it as this param let's put it okay so we got a date for the function now what we'll be doing will convert into live date time objects so what will be the variable date equal to new right date time and we'll pass that selected data over here okay so it will be planned studies okay now the next step is like we got the Glide date time object now we have to see if this black date time is on a v day or weekend okay so as we know we have a function called get day of week local time okay so we'll be using that function out so if date dot get circuit day of week of week local time because I want to consider all the scenarios with respect to my local time okay and will give so how exactly because in service now instance when you use this function if it's a Monday it will be considered as one if it's a Sunday it's considered as seven so if Tuesday will be 2 minus there will be three and so on so what we can do we want to exclude saturation and that means we want to exclude six and seven so we can give condition if the weekdays greater than 5 okay that means if it's weekend then what we have to do we will return screw okay else always give else okay so else we'll give we will return false not false false okay done so this is all we have to do on the script include side okay so I will just save it out and then let's go back to a change form and now we will be creating a on change client script okay because whenever the changing it the date get changed we want to uh we want to perform this validations right so I'll just go to configure and configure will go to the client script and I'll leave and in client script we'll create a new client script and we'll give it a name as well as debate started it makes sense will give a type as on change which particular field so we want to perform it with respect to plan started right because we don't want them to schedule change request on the plan on the weekends it can and now we'll start writing our code so first and the foremost thing we'll be writing Cloud HX over here so what we'll be doing variable Glide attacks change equal to new slide as X and here we have to pass the scripting code name which we'll be calling so the scripting group name is weak to your validation so I'll copy it out and we'll paste it over here okay now the next step is to pass which function will be calling because a script can have multiple functions out right so the next step is like GA like agree on glider exchange Dot add param and what the second attribute will be the function which function we want to count and it gets signified with the name called this bottom underscore name and which function we are calling over here I don't remember so we are calling this particular function which is is it a weekday so we will give it the function over here is it a weekday now the Third thing what we have to do is to pass the value of the start date right then only the validation will happen right so for that we can again go with ga J Dot add param at that program will be uh what this time what attribute we have passed over there so we'll get this one param date go here paste it out and now this particular value is in the new value right because we have written on change kind of thing so we can simply use new value no use no need to use G form and all those things out done now we can get the response now it will go to the server side with this attributes okay and it will get the response so we let's get the response and how we can get the response to a variable response equal to change Dot get XML answer and in this we have to give the name of the Callback function so we can give us call back and I will just copy this out now we'll be writing the definition for this function so it will be function pass callback and it will have input as answer because this is the function where the server side answer will get pulled okay and over here we can give if the answer is true that means if it's falling on v k so we can give if answer equal to equal to true if it falls and we can then we just want to give an error message how exactly we can give the error message through which the unders could form Dot form Dot show field message because I want to show it on the field okay then now it asks for which particular field right so I also don't remember what is the name of the field so what we can do and go over here go to history change request let's open this out tap tap start underscore date you can go tab and we can give start Congress could be it this is a field where you want to display the message what should be the message okay so we'll say he we are on the these plan on weekday okay and just to make it interesting just to make it as a like warning okay we can give it his time as error hmm true so we are done with the logic over here we'll simply Simply Save it out now so I'll save and now as you know whenever you write a client-side thing you should refresh your record so that it will start reflecting it out and if I reload the form okay so if I select some validate okay some weak that's it let's see what happened so I will select 25th okay it's fine everything is fine right now now if I go and select 28th he he we are only believe this plan on weekdays you can also clear this value if you want okay but this is how I have done at this particular point of time okay because we just want to give the warning message the warning message is different in our case okay but just to make it some interest ing thing okay I just twisted my Logics out okay so this is all for today's tutorial hope you like it out in today's tutorial we have seen how we can use Glide Ajax to validate reader name field okay and we have just explored one uh Cloud daytime APA okay and in that also we have explored a function called get day of week local time okay so local time or UTC you can use both okay so this is all for today's tutorial hope you like it out if if that's turned out to be true okay then please subscribe to my channel so you will be getting updates on my new videos till then stay safe stay happy and have a nice day thank you
https://www.youtube.com/watch?v=wpq0nku78a0