logo

NJP

#9 Data Population from Server on Client in ServiceNow | Learn Application Development | 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 we have to add is populate leave details so if i go to the form over here you can see we have these details like accrued we have taken we have balance so the thing is that as per the requirement so for example i'm i'm an employee and i'm just trying to request for the leave i select uh maybe start date and date but when this loads the form what exactly should happen i should be able to see my leaves that means whatever leaves i have in my bucket like whatever count i just accrued how many of leaves i have taken and now how much is the balance that is something i hack i can see over here in these three fields because that's very important because whenever i am requesting for a new leave uh then i should be able to see those details as well just to uh so that your employee has those details okay these are the leaves i have in my bucket uh and it should change as per the types as well so for example if i change here sick leave then this should automatically change so if whatever type i select over here it should automatically change the number of leaves for that particular leave type so how can you populate it now as you know we are storing the leaves the the number of leaves for each employee in leave bucket that means i have to pull the data from leave bucket that means from this table and i have to populate it on that particular form that means i have to populate those values on the form to those fields that means if i come over here click on new so from from leave bucket i should get those numbers in this particular fields so that is the requirement now how can i achieve this requirement now in order to achieve this requirement because you have to fetch the data from server side and then you have to populate it on the client side so now you have to write a second glide ajax again and we have to use script includes that's a client callable and we have to write client script as well so we will use the same script include so let's start so we will go to studio over here and i will just keep selecting this one and i will go to i think i don't have to create a new script include what you can do and that's i think i would say utilization of script include if you have multiple functions that's something you can declare in a script include and you can use it in different scripts so i will open this script include which we created that is lms ajax over here as of now i have this function but now what i will do i will create a function specifically so that i can fetch the data for leave bucket so whatever leave i have i can fetch it on the client so in that case i will move to next line over here and i will create a function and i will give it a name as maybe get leave bucket so this is the function name i am giving i will do colon and here i will do function maybe i will make it full screen and make it a little bit bigger and then i can just do like this and then i will put parenthesis and i will do curly brackets then i will do just comma over here so my function is ready so now i will write the script in this particular function so what i have to do basically i have to pull uh the rackets from the bucket that means for this particular employee who is requesting uh the leave that is what i have to fetch so how exactly i will fetch it so maybe i will just come over here i will do where gr equal to new i will do glide record and here i will i will do like this okay it says so i i just did syntax mistake and here i have to provide the table name that's for leave bucket so i will come over here i will click on leave bucket and i will just copy the name so i have this name i will copy this one and i will go to script include it is already client callable so i don't have to change anything i just need to put the function and just uh or just mention the server side script that's it so here i will put this so i'm glad basically doing the glide record on this leave bucket table and i will do gr dot add query now we have to match the employee so in this table we have employee field now where exactly we have to match we have to get it from the client that is the requested by so in that case what i can do i can do this dot get para parameter i think it's correct parameter and then i will do here i will put the parameter because i have to get the user so i will do cis underscore cis underscore perm i think it should not be perm okay we it will says sperm and then i will do underscore and then i will do user because we have to fetch the user from the client so we will send this parameter um as the ssid of the user and then i will do semicolon and now i will select gr dot add query now we have to select the type as well because that's what i have to show the leaves so it has to be user and it has to be what type so in that case i will select over here leave underscore type and i will also get this value so we will get this value from the client so in that case i will do this dot para me ter and i will do like this i will do sis pum underscore so spam underscore and here maybe we can just do leave type that's basically another uh parameter we have to send from the client so it's leave type and i will do semicolon and i can just do gr dot query it is done and i will do if so if i will find the racket if the same type of leaf and for the same employee then i will do gr dot next so it will look for the next record and if it will find that means if this racket will be found then i have to send the details that means for that particular record all the accrued uh balance taken that is something i have to send back to the client how will i do that because this time we don't have to send just one value if you remember if i if i talk about over here here we are just sending the one racket just one value that's it and whatever value we are storing over here we are just returning that but in this case we have to populate it for three different fields how can i do that so i cannot just just directly do the return and just put one uh variable value what exactly you have to do basically you have to put those values in the object you have to create that object like a json and that you have to send back to the client that's how you will do that so what we will do we will maybe we will mention um we will do leave details we will create a variable and i will create an empty object so i'm creating an empty object for this particular variable and what i will do i will do leave details dot accrued i will just mention that name now what exactly i will do i will basically uh club the data and i will make this object and then send it back to the client that is what i have to do okay it will become a json object so in that case i will do i have to send in string format that is what because there we have to push it at the float floating point number but here we have to uh basically uh make the whole whole json format in in basically a string so what i will do i will just add these parentheses um this quotations not parenthesis we will just put the quotation single quotation and then i will do gr dot so i'm going to get the value of this accrued so that accrued is i think see this and if i maybe i will just open leave bucket quickly and i have accr ued okay it's good and that's it for this one and then i will do leave details dot i can do balance that's another object i'm going to send and it will be like this i will do plus gr dot balance and then i can do leave details dot and here i can just put taken i will do equal to but quotations and i will do i will do like this taken semicolon so now you have created you are basically uh pushing the values in leave details now what exactly i have to do i will make it basically into a json format so i have to stringify it how will i do that so for that you can just uh let me maybe i will just format the code and here i will do return how you will basically stringify it you can do it with the help of json dot stringy file i think stf stringify that's what you can do and then you have to just push this leave details that's it so ideally we are sending just one uh basically object over here but it will send the whole object so whatever values we have in this variable that is leave details because we have created the empty json object now this will be sent with all the values we have under leave details to the client that's what it will send so we have all these values we will we are we are just stringifying it basically we are converting into string so we have this json format and we are converting into a string and we are sending it to back to the client so i think i am done with this i will just click on save so it is saved and let me check okay all good and now i will create a client script because i still have to create the client script so in that case i can just click on create application file and i can click on client development and i can click on client script so i will provide a name over here so let's say maybe i will provide populate leave details and i will select the table as leave request i can just put it as like all now here type i will select on change whenever type will be selected that's how it will automatically pull the data as per the type selected so here i will select type leave type basically so we have just mentioned leave type and if i come over here so i will write this script over here so i will start with calling the script include and i have to call the function and we also need to send the parameters which we created in our script include how would i do that so i will start with var uh that's a glide ajax variable i'm going to create so i will do new glide ajax and here i will put the script include name and our script include name is this one so i will just copy this i will go over there and i will just put semicolon and i can just do dot add add pattern because i have to mention that parameter so i have to send the first way we have to call the function so i will do cisperm underscore name i will do comma in quotations i have to provide the function name which we created and we created this one that's get leave bucket and i will put it here semicolon and then i will do ga dot add param again and this time i will do cis perm underscore user because we have to send the value of uh the employee the basically requested by so i can just do like this so let's say i will i will select it because we i have to get the current logged in user and that will also be populated in that requested by field automatically that's what you're already doing so in that case what i can do i can quickly do g underscore user dot i think i can do dot okay i have to do like this this user id now this will basically uh store the cis id and it will send to this parameter via this parameter that is a sperm user so i'm done with this now next is basically we are sending the type as well so i will do ga dot add paren and i will do quotations single quotation and then assist perm underscore leave type that's our parameter i will do comma now here we will select the new value so i will select the new so whatever value you are selecting that is something you can mention here so because the the type will change the value so you have to always get the new value the what exactly it has changed so that you can basically uh you can basically populate the rackets as per the type that's what you have put in in your glide ajax as well like in your script include that it will get the leave type and then populate the rackets return the rackets accordingly so i will come over here now as of now you just need to remember one thing because here if i talk about here its leave type is automatically populated that means this it is already showing the default value what you can do you can make it none so that user has to select it and it is already mandatory so in that case what i will do i will come over here and first let me complete this uh glide ajax first and then we will change that so i will do ga dot get xml and here i have to basically mention the callback function so maybe i will just do get bucket semicolon and i have to now call this callback function so i will do function get bucket similar kind of syntax which we learned earlier so we will do like this and like this we will do answer equal to response dot response xml dot document document and i can just do element dot get attribute it's done and here i can put the string in answer on sir semicolon now basically as you know we are going to get that data into into json format because we are putting all the values we have created that json object so what do you have to do whatever data you will get you basically you have to parse it so you have to pass the json whatever json you will get it so that you can basically segregate each values you are getting in in that particular json so how will you do that you are going to do so we will just maybe create a variable result and i will parse it so initially we were stringifying it that we will basically making it in the string and then now we are basically whatever string we will get we will basically convert it will parse it so that i can read the data how will i do that you can just do like json dot parse now what exactly the response is that is answer so answer will basically store the data coming from the script include and then i will set the values how will i set it it's quite simple i will do g underscore form dot set value and here i will put the field name so it's accrued okay and here how will i read the data now in this case you just need to do result dot acc accrued that's it whatever values you created over there you were trying to push the values that is what you can do over here you have to just fetch it now so i will do g underscore form dot set value and i can do it for balance i will do comma and i will do result dot balance semicolon and i can do like this dot set value and i can do okay i can just make it a little bit up so you can read it perfectly and i can just do taken and i will do comma and here i can do result dot and it is taken i'll do semicolon and i think it is done you don't have to do anything else it will set the value uh on the client after leave bucket lms ajax we have this function name so all good we are stringifying it no syntax error i don't think sleeve details all seems to be good and if i maybe um i will i will just save it now so this is saved so now we will see so we have created uh the glide ajax uh function that's something we have in client callable script include and we have also declared basically the glide ajax variable and we have we are just calling that script include and we are also calling that function so in that case i will do a quick test how will i do that so in that case first if you remember we have to make it none so i will go to leave request i will come over here i have this leave type and i will remove that default so i will come over here i will just remove this default type now here i can make it with none so that user has to specifically select it so i will save this saved and if i will come over here and if i will reload it you will see that now you can see it says leave type i have to select it specifically now if i will do that let's say i selected casual you can see i'm not getting any racket why because i'm not getting any data on the client right here because we didn't create anything for system administrator so let's do one thing if i go to this leave bucket like i'm i have i have admin access so i can create some value so let's let's say uh we will create uh a racket over here i will select system administrator let's say we can get this yep i will d accrued maybe we will put 24 uh taken maybe we can mention zero that's fine uh balance is yes i can i can put 24. now this is for casual leave just an example and i can maybe just save this record so in that case that means i have one more leave bucket racket for system administrator now if i will basically open the new form now what do you think will i get the data from the server side about this data let's see if our script is working fine or not so if i click on this and no so you can see okay it will not come right now i have to change the leave type i forgot it in just few seconds i don't know why i forgot it but that's okay i will select now casual leave let's see what happens if i select it absolutely you can see over here we are getting accrued we are getting taken we are getting balance as well and i can submit this leave and this should basically so while i will submit it and if it's get approved for example it should definitely basically deduct the data decrease the data from my leave buckets and that's what the calculation i have to do so but as of now you have created another client script and you have also created another glide a jack that means you have learned script include glide ajax client script again and this time we again i i i would say we also did some calculation and at the same time you also learned that how we can send the response uh from server side if we have to send the response uh multiple responses basically back to the client how can we do that we can just create the empty object json object you can push the data and then fetch it over here you have to parse the json and then you can populate it utilize that data anywhere you want so that means it's a really i would say it's another kind of learning as well it's a live development which you can learn and you can apply in your daily development as well as per the use case you get from your customers and clients

View original source

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