Call Script Include from Client Script in ServiceNow using GlideAjax
hi guys in this video we'll discuss about how can we call a script includes from our client script so we take into account one of the most common use cases where we have a user field on the form which is a reference field actually and we need to populate other details like contact numbers variety or maybe department name as well on the form itself once the user details are populated so I have just catered this one sample form where I have a lesson suite called user and - whether it'll tilts to other fields called contact number and user ID so let me just select one user over here and see it should not operate the details as of now so I just selected a loop in accounts and none of the details of populated as of now since that scripting load is not in place so I just go ahead and create one client script you Lipton II must populate the details and since a little should populated every time a user the core values change in that case I mean user field value is change so I will give the diapers on change and here I like the cold for my script in blue and calling is in glider checks let me just give any variable name like user begins I'm going to create script until with this protocol then this first parameter or in I will pass the name of the function or which I am going to call and this function name will contain the logic which will send the details so my script include two metal script then in this particular parameter I'll pass the name of the user selected on that record for this I can get to where you bring this and then fastest Lu over here so one says if evil spawns on the server-side script I'll pass the response over here and then I can set the values in the corresponding fields so we'll continue on this particular code once I am done with my server-side code so and I just move on to create a script include now so I'll just click on new I will give the same name from my script include and since I am planning to call it for my client script it should be client Colleyville ok and the next step is I discrete of function with this same name I declare these objective and I will be passing the details and I will be returning this particular object for my function to my client script now I Drive through the user table from where I need to fetch the other details so the details which I passed in this particular parameter suspend the squeeze a solid can be obtained in my script include using this dot getparameter function when I will pass the name of the parameter then I'll set those details which I have to return from here in my user object which I have declared on top would say this one is contact number so the actual name of the field and the detour business let's check for them so name for the field which I'm trying to fix let us assume in the field name is mobile phone so it should be mobile and disco phone the next details which I wonder that is using ID so actually you get value of usual ready the name of the field is using underscore name for my example case I've used these two fields but we can if required in the future for any requirement we can fetch more details and add it the workers have topped it and we just need to now return the values which we have certain the object so to return those values what are those as it simply returned to objects by using chase 1.25 since we can not directly return an object from the scripting look at the transcript cause it won't be able to pass in that case so I think my scripting code is ready and let us say this thank you jump onto my client script once again so the object once returned from there we need to pass it and pass it over here whatever the response is coming and accordingly we need to set the form filled with those values which are part of the user object coming from let's continue now I'll pass the same using JSON door pass and I will store it in my is the object of transcript let's say those things in we come from field so that does form which I have created contains two field names or contact numbers because and the value constantly is an object which I have told in this particular no I said usually don't use word if you lease use and discard it I don't script include the name is ready so looks good now I just go ahead and save my client script so now once I create a record and I select any user value what should happen is and passing some easier details in the function called that user details I am calling this could think about then once the script include is it executed it will write to the user table then it is fetching the value for this particular user which I am passing from a client script then it is creating an object with these details and in the end it is written in that particular is an object and once I get that particular user object in my client side code I am just passing it and setting the form field values so let's see if it works we'll have to debug so just one form I click on you now let's lit any until both days worthy and the phone number this is one such example so I did not work what code with the possible reason that it did not work now let us just analyze why it is not working because this code is fine we have a gliding user table then I'm setting the values in this object so this looks good even client Colleyville is checked so the script should have been called now going back to our client script code I pass the name correctly I am passing this value as well so the type is on change but the field names contact number so when do we need to call this on change of the user field so let me just update the field and then we will test it again you know one more once again it will come you I will select a using movement gods and successfully contact number details and the user ID details of that person whose name is selected in the user fields are populated over here so this is one way in which we can operate the details using leaddigits from the database couple of other ways include using the client record query in the client script or using get reference in the clients field but those are not recommended
https://www.youtube.com/watch?v=DVivsjacyOQ