logo

NJP

#7 Create Client Script in ServiceNow | Learn Application Development in ServiceNow | LMS

Import · Nov 10, 2020 · video

[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 the next feature of this application is calculate and set duration as per half day now you have set the value of duration when you select start date and end date what about half day field because you have to mention 0.5 as well because that's what you have to calculate that's how you deduct and you calculate the leaves so it's very important that you also basically do the calculation for half day selection as well how will you do that let's go to our instance so in order to basically fulfill this requirement so basically what we have to do if i will check this check box and i have selected this start date it should just populate point five over here that's it this is the requirement okay that's a half day we have to mention and that's how we will calculate the leaves the crude taken balance that's how we will uh calculate the whole leaves we have for any particular employee but how exactly i will populate this 0.5 do i need to use glide ajax again do i need to use script include so the answer is no because we don't have to do any kind of date calculation over here we don't have to calculate any kind of field subscription subtraction we don't have to do that so in that case what i can do i just need to create a small client script that's it you will be able to achieve this so i will go over here and i will close this together and i will create another client script i will double click this and i will select let's say this one set duration for half day and here i can select the field as leave request and ui type i can uh put all now why i'm putting this ui type as all because i really want to i really want to make it working for portal as well so in that case i will select like this and then i will make it on change now with what change of uh with which like which field we have to change and that field will be half day so whenever i will select the half day this client script should run so i will just mention over here so i will go to my client script now and i will just start creating the script so i don't have to write in loading i have to write over here what exactly i have to check basically you also have also need to check if half day because half day can also become false then you don't have to basically uh basically uh calculate the duration so in that case what i can do i can uh capture the value of half day so maybe i will do h a l of d so it just i will store uh the value in this half d and i will do g underscore form dot get value so that's what we will get the value of half day field and that is half underscore day semicolon and i will do if and i will do [Music] half d equal to equal to true if it is true and if we are changing the field then only it should calculate the duration so in that case i will mention over here g underscore form dot set value but that's what we have to set the value and that setting the value is for duration field and here i can just do like this point five point five that's it you are done so we will set this 0.5 value if half d that means the value of this half day is true now you will also think that it's very important that start date should also be filled so should i mention that condition over here idly should not the reason behind it because uh you you you will already uh basically you you i think start date will always be mandatory so people definitely have to fill the start date so in that case i will click on save because we are done with the creation of this client script and if i go to my form i reload it so i have this casual leave over here i select uh this date it's just a start date i'm selecting and now am just selecting half day if i select it you can see i got 0.5 automatically that means it is telling me that hey you have selected the half day that's the reason the duration is just 0.5 it's not the complete one duration if i just select the one day so for example if i select 5 let's see what comes over here if i reload it and i select like this maybe i'll select five if i select five over here great you can see because it's zero so i am i am getting this one day over here that's good and that's the reason if you remember we were adding those plus one that's the reason i'm minus one that's the reason i'm getting otherwise i think uh i think it's it's calculating correctly but if i select for example six okay yes we are getting two so yeah it's it's calculating perfectly and if i select half day it will calculate 0.5 that's it and i can submit the request but this is how you create another type of client skate for different kind of requirement i think it's it was kind of a very small script uh where we had just had to uh basically achieve the requirement on the client and we didn't have to use any kind of server side code as well so we didn't have to use glide ajax or maybe script include as well

View original source

https://www.youtube.com/watch?v=LPX5LjdEquo