logo

NJP

#17 What is Script Include and Glide Ajax in ServiceNow | ServiceNow Developer Training

Import · Jul 27, 2020 · video

[Music] in order to subscribe to my channel please click here or click here please share comment and like my videos and channel hello guys welcome to sas world service now this is follow-up session of servicenow developer training in this session we will talk about script includes and glide ajax what is script include a script includes store javascript functions and executed on server side a script include code can be reused in different scripts of service now you can call script includes from client side which is an ajax call and referred as glide ajax unlike business rules and client scripts you need to call script includes from another code to run the code mentioned in it let's see script include form and its fields first field is name to identify script include then we have api name which is the name of the scope and script include name client callable you can check this box if you want to call script include from client side like client script code active you can check this box to enable or disable the script include description details of script include and the main field we have is script in this field you have to mention your code to run from another script out of the box script includes servicenow has various out-of-the-box script includes created for different features and functionalities this is my personal developer instance in order to see a script includes you need to go to system definition you will see this application under this application you will find a module called script includes if you will click on script includes you will see list of script includes available in your instance click on new this will open new script include form and here is the script field where you can write your script which can be called from another script types of script includes there are three types of script includes in service now first one is classless in this type of script include you create one function which you can call from another script and you don't create any class in this kind of script include and this basically runs on server side second one is extend a class in this type of script include you need to extend another class like abstract ajax processor which is a another script include and you can extend it and call it from client script and third one is utils in this type of script include you can create a class and add multiple functions which you can call from another script and this can also be called from client side classless script include this is how you create classless script include which just has a function which is called from another script in this case you're not creating any kind of class that means it is a classless script include in my personal developer instance if i go to script includes click on new button and here i just type demo test i'm just giving it a name i press tab you will see this class is created automatically that's a functionality out of the box you have in service now but what i will do i will just remove this whole script and rather than that what i will do i will just declare the function so i will call the function here we have this demo test and i can do a comma b and here i will give for example c equal to a plus b and then i can do gs dot log i will do gs.log the addition of addition of plus maybe i will take a and here i will mention [Music] a plus here i can just mention plus space like this because this is how the string would be and i can do plus b and i will do plus and here i will do is space and i can do plus now this is a class less script include now i can call this function from a business rule and you will see the results how exactly it works so in this case i will maybe i have given demo test demo test here c equal to a i think um i will do there as well because that's why you have to declare it's a c variable now a and b are the parameters now here we are just adding it is kind of an addition we are doing now how exactly we will get these parameters values so first i will save this let's save this and then you will understand i have saved this what i will do i will go to business rules and i will create a business rule here so i will just click on create new so i'm going to create a business rule to call that particular script so here i will just maybe give a name that is demo maybe let's give demo test as well or maybe a script include classless that's what we are learning classless demo that's the name we have given and here i'm just selecting a random table maybe incident because i want to run that so for that i will just select incident table and i will check this advanced check box and i will select when and this can be before and it should be before update so before update of any incident i will go to advanced because we don't want to take any action for now my only action for now is i want to calculate that value or whatever value we have written in that log that is something we want to populate so in that case what i will do i will send a parameter and i will actually run that code how exactly i will do that so here what i will do i will do where a equal to 10. let's say it's just an integer i'm giving verb equal to 90. now i am going to call that script include the function which we have created how exactly you do that you just need to write demo test that's a script include we created that's a function we have as well so in that case i will just mention here a comma b because this is the parameter we have to send it that means value so a and b value will be captured here and it will be sent to that function we have in our script include so this is done and what i will do i will save this now how exactly you can test this in order to test this you have to just update any random incident so maybe we will go to incident so let's open any incident we have let's say this one and we will also just just open logs as well because that's the reason we have done gs.log because our results will be saved in the log so maybe i'm just writing here test and i'm doing save and this is updated so if i go to logs because we can't see results anywhere else because as of now we can see the results in the logs that's what but you can you can definitely utilize this anywhere you want so i'm going to the logs and you can see we have this result the addition of 10 plus 90 that means it automatically took 10 and 90 this is the one we put in our script include and it is it is also showing the value 100 that means your script is running without any issue you are able to call a classless script include from a business rule but that's how you can work on classless script include a script include with class now this is slightly different so this is how you create a script include with class in which a class is created and function is created in the class light camera action script include with class this is how you create a script include with class in which class is created and function is also created in the class and you can call both script include and the function directly from another script we will see the same example in my personal developer instance but this time we will use class so in my personal developer instance i will go here and i will go to script includes again and i will just sort it out with here we have demo test so i have just sorted this out with the updated uh date and time so i am just clicking on new here i will give a name demo test class i will press tab so if you remember it will automatically create the class for you you don't have to create yourself now in this case you can see we have this class under this class we will create a function so i will just do function and i would do demo test a comma b same script no difference i have to put comma here and then i can do where c equal to a plus b or maybe this time i will do b just to make a difference and i will do gs dot log and i will write same thing what we wrote over there the addition of plus a now here it says unexpected token demo test okay we can just i think i just did a mistake uh i will show you quickly why i did a mistake you always define a function like this function that's how you you define it so i have created the function now that's that was something different we created and this is how you declare in a class so we have this function demo test and here i will mention addition of a and i will do plus and here plus b plus and i would do is plus and i have d that's a result so that's what we have gs.log and i will save this let's save this one so i have saved function what we will do we will utilize the same business rule which we had so if i go to business rule i have this business rules here let's sort it out to see our latest business rule we created this one so in that case uh we we have just called that demo test but that's that's not you have to declare here this time you have to call that script include and then you can call that function so in that case what you will do you have to do where you have to create that particular script and you have to declare that here so i will do where maybe i will do v equal to i will do new now i will provide that script include name because you are going to access you are creating the class for that particular script include which you had over there so i am going to write demo test class that's what we had and if i go here i do v dot demo test and i just give that's it you are done you have called that so in that case i will just save this and let's see we get the result or not so i hope we have our names are correct so you have demo test now i will go to any incident i will come here and just try to update any existing incident updated and if i come here this was 705 i think this is showing us time and yes we have this here as well so we got similar result but this time we have used the class so that's how you can use classes and script include so you can use classless as well and you can use class as well it totally depends the kind of requirement you have because in in class you can create multiple functions because in this case if you will use classless in that case you will you can just declare one function over there and you can just call it from from your business rules or server side code but in this case in classes you can add multiple functions and then you can call those functions from your any different scripts you have calling script include from client script so once you create the script include you can call it from client script you create a glide ajax variable in which you have to call script include then you add parameter to call the function mentioned in script include then you also add a parameter which you want to send to script include so you capture it on the client send it to the script include and then you make a asynchronous call to your script include like ga dot get xml and you also define a callback function which you have to declare here and then you call that callback function which returns the data and store the value in answer variable so whatever parameter you sends to your script include it returns that data after using that parameter and that results are basically captured in this variable called answer so this is kind of a syntax that how exactly you can use glide ajax in service now let's create a script include with this glide ajax so i will go to script includes click on new give it a name as demo test ajax i will press tab when you will press tab you will see this class is created now we have to make this client callable the reason behind it because we have already talked about it glide ajax is something you call it from client so i will make it client callable when you will click on client callable you will see the difference that this will extend this particular script include now abstract ajax processor let me show you quickly so that you will understand what exactly it is that because abstract ajax processor is another script include and we have in our system out of the box see we have here abstract ajax processor hit this one i think it's it's at the top so we have this abstract ajax processor which we are extending so if i go here and you can see we have this particular uh this particular class so now we will write a script so we will create a function so i will start here we have to write in this particular place so we will start with i have to write the function so i have to declare a function so we'll write demo test function and here i will write function now i'm not getting this parameter from server side right now we're getting this parameter from client this time i will show you how exactly you do that you have to first close the curly braces and then you also put this comma here for each function you declare you have to put this comma then i will do one thing what what we will do we will try to populate the user id user name and in first name and last name let's see that that's what we will do so on incident form if you select the caller we will populate the first name and last name of the user so in that case what we will do i will just type where user id we will do var user id equal to this dot get para meter now here you are putting the name of the parameter so which you always start with cisperm underscore and then you can give any name you want so here you i am putting like user so whatever parameter i will get from client it will be saved in user id the variable which we have created this user id now i will do var gr equal to new glide record and i will do cis underscore user because i'm going to search that value which is coming from client i will search in users table so that i can get first name and last name and then i will populate that now this is just a demo for for just showing you how exactly you can use this kind of glide ajax because overall you already have some i think api so that you can directly call and check first name and last name that is out of the box but i'm just showing you this demo just to show you the example that how exactly you can use glide ajax so then we have the sys user and i will do g r dot add query i will look for the society which i will get from client how exactly i will get that society i will show you in a while here i will just put user id that's it and then i will do gr dot query if that is found in the table if that user is found next and then i will do where a equal to gr dot first name that's the name of the field we have and then i have gr dot last name i'm sure these are the names we have field names on on user table and then i will just do return so now here you are going to return the response which you are capturing in this variable so i'm returning a so whatever value we have it will be returned by this function so i think this is it i will just save it now what you have to do you have to call this script include from your client script so i will go to incident maybe so let's go to incident and here we will write a client script so i will come here and i will go to client scripts and i will click on new button new client script form is opened and i will give it a name maybe demo test ajax so that you can understand uh type this this something we will put on change and i can just put caller and here after here we will write the script now once once you will create the script include you have to call that script includes now that is as of now it is a client callable however you call it from here from client that's the syntax we are going to use v-a-r-g-a you're going to create a glide ajax variable first so i will do new glide ajax now here you have to give the script include name so if i come here and just go to script includes so i have this script include let's go to updated one latest one we have this demo test ajax i will just open this and we will just copy this and put it here so this is the first step you have to create a glide ajax variable now you have to call that function which you have over there that's the add perm that's that's that's what you use so here you will define it as let me you will start with cis perm underscore name that's how you call that function uh so when once you are declaring that script include now you have to call that function how you call it you have to mention it here so i will come here and i will copy this one and i will put it in the string and this has to be string so semicolon and i will do ga dot now here you have parameter you can add multiple parameters but in this time we just need to send the cis id of the user who is in the caller field so in that case i will just do add param and here i will just put the same parameter which we had over there that says sperm underscore users if i will take you over here you will see we have same parameter so this should definitely match so we have that parameter i will do comma now what parameter value should be there so you have to get that value that value will be will be basically you have to just put new value that's what you have you're already capturing it on change that's the functionality of on change client script so here you have that value available in new value and i will just put semicolon and now i will do a asynchronous call and asynchronous would be get xml ga dot get xml and i will have callback function so maybe i will just type call back function you can just keep your name as it is as as per you want so that is not restricted this is not part of the syntax the syntax we have is this one this one can be dynamic which you have to call this script include uh this one is not dynamic till here but this is something dynamic you can change the name as per the function you want to call and this is something the parameter till here it is definitely static but this one will definitely change this one can also change as per the parameter value you want to send to the script include to process the script you have in your script include callback function that definitely you can have your own uh name whatever name you want to give we will do it now we will call that callback function we will have call back function and here we will put the parameter response so this is something we will get from that script include once it will be processed you will get it and here i will do where answer now this is also part of the syntax when you start where answer so you're going to create the answer variable and you will store the value the response you will get it from your script include so we will do response dot response xml dot it is also capital letters dot document element dot get attribute so we have this till now everything should be same nothing should be changed this is this is the part of the syntax so accept this so whenever you call any function you put there rests would be should be always same then you put here and then you do i think you have to do alert okay that's what you want you want to show that alert so i will do alert and i will just put answer so whatever answer we are getting getting from script include once it is processed it will be stored here and then you can utilize that value anywhere you want so i'm capturing that value and let's see what kind of answer we will get so here we have alert answer and we will save this and if i go to my script include this is what we have here and now i can i can just do a test so let's go to incident form i will click on create new and if i click on create new and i just select any user here let's say this one and we got the answer you can see it is not returning the value there should be definitely some issue that it is not returning the value so we are doing gr dot next we are selecting here says sperm user demo test function i think the name is also correct uh get parameter is also correct newglide record add query everything is fine add query we have sys id i'm putting that okay we have done one mistake so i will go here i do remember now but it is not returning that so i will go to my client script again maybe that value is not coming so let's let's check that because it is returning answer we have this alert with the reason behind it because you have mentioned is that as a string which you don't have to answer is a variable so it should definitely show that value now so let's save it and i will click on create new again i will create a new incident and select a caller and let's see if i'm getting that value on the client from server so i will just select here maybe abraham lincoln yep we are getting that you can see there's no space because you have not mentioned that space but overall you are getting server value on the client so this is how you always call a server side code on the client that if you want to fetch some value on the client you always use glide ajax there are some other ways which you have learned in my other videos like get reference we also have g underscore scratch pad but glide ajax is the recommended and the best approach to get the server data on the client let's see some practical examples with live scenarios i will also show you how can you use script include in other places like reference qualifier or business rule conditions starting with first scenario on problem form there should be a field problem owner and user can only select those users in this field who has problem underscore manager role this is my personal developer instance now before i create that script include to fulfill this requirement let's add that field on problem form first so i will go to problem application and i will open a problem form so here is the problem form and we don't have that particular field available because that is not out of the box so in that case i will go to form layout and create that field so i will type here problem owner and this will be a reference field and this will be referencing to user table so i have this user table here i will click on add this will be added here and i will take it on the top of the form and above state field i will click on save now you will see that this field will be added on top of the form that is above state field i will close this you will see that field is created and added on the form now if you click on this button you will see that this will show you all the users available in your instance but as part of the requirement you have to restrict the choice that means you should be able to select those users you should you should be able to see those users in this particular list to select the people who have problem underscore manager role else others should not be visible in this particular field let's create a script include to filter this data i will go to script includes modules click on new provide a name so it will be prb like functions you can see this class is created i'm not going to check this box because we are not going to call this a script include from client we will call it from server side code so i will create a function here so maybe get prb managers function i will put comma here now i will create a variable so that i can store all the societies of the users who have that problem underscore manager role so in that case what i will do i will do p managers managers equal to i'll just make it as a string where gr equal to new glide record now which table you will select here you have to select the user role table basically where you can see the relationship between user and role and the name of the table is cis underscore user underscore has underscore role and it has two major fields role and user and i will do gr dot add query we will do query here we will search for that role which is problem underscore manager for that we need society of that particular role so what i will do i will quickly get the society so i will go to role table so here we have roles and i will search for problem manager so here is the role i will copy the society i will come here and i will put that same society here in the string then we will do gr.query in this case you will find all the users with this role then i will do while gr dot next and here i will do p managers that's a variable we created equal to p managers plus we will add comma so after every user we are getting with this loop we will always add a comma after it so in this case you will do comma and then i will do plus and i will just add gr dot user now this will capture the society of the user and it will search for all the records where role is problem underscore manager with the society we have mentioned and it will populate all the society of those users so it will basically capture those values in this particular variable which we have created now with this string so what exactly you have to do in order to filter the data you have to populate that particular thing in your reference qualifier you have to push that value you have to push that particular query so that system should search the rackets as per the query mentioned in reference qualifier so what you will do here i will just mention here there and now i will store all whole query in results because but i have to make that query so i will do cis underscore id in now this is out of the box which you're using because we are searching that if society is available in all those uh all those societies which we are getting for the users then it should filter accordingly so this is the query we are creating so i will do p managers and then i will just do return result and i will save this so once it is saved now i will apply and call this script include from our reference qualifier so basically we will go to the dictionary of that field to filter the data so i will go to problem form let's open any problem form i will open this one here we have problem owner i will go to dictionary now here we have to provide the reference qualifier basically with custom condition you will see reference qualifier here we have normal condition but you have to go to advanced view so that you can see more options for reference qualifier so you can see here we have this reference qualifier as of now it's simple but we have to make it advanced now here you have to mention and call that script include and your function so we will start with javascript colon we will do new and then i will first put the name of the script include so the name of the script include we created let's check that quickly we have this prb functions so in that case i will just copy the name here i will come here and i will put that here and then i will call the function the function which we have created so i will copy this one as well put it here but that's how you call a script include and its functions so i am putting this in reference qualifier i am saving this you can see that it is saved and you have put in this reference qualifier that's a custom which is basically taking the data from script include let's go to problem form now so if i open any new problem form and here's the field if i click on this button you can see that it is only showing three records to select but before we applied this script include and we changed the reference qualifier it was showing all the records of your instance so this is how you can call a script include from server side and also from the dictionary of a field in reference qualifier scenario two when user selects a configuration item on incident form then assignment group should automatically set to support group of selected ci on the client so if i take you to my instance let's see the configuration items first so if i open configuration items these are all application servers maybe i just do cmdb underscore ci dot list dot list in this case i will get all the cmdb rackets now here is a field called support group i can just take it here in the list and i will filter out and see the records where we have this support group we have 37 ci's where we have support group available so the requirement is if i go to incident form and if i select ci and if that ci has a support group it should automatically populate here normally in every organization people definitely implement this functionality that they should have support group for every ci so whenever you do that whenever you select any ci it should automatically populate on client in this assignment group field how exactly you can achieve this now this is something we will use glide ajax that means you will get the data that server data from cmdb racket and once it will be selected here you will push that value here so whatever data you will return that support group you will populate it here so let's start with creating the script include first so i will go to script include and we will click on new and i will give it a name as set support group i will make it client callable so that we will extend this abstract ajax processor and i will create a function now so i will do get ci now this is a function i am going to create so i will do function comma here curly braces and then i will start writing so i will first get the ci the value of the ci we have on the client because that's how you will search for that record because we have to get the society of that particular ci so in that case i will do cmdb ci it's a variable i'm creating i will do this dot get para meter same syntax if you remember we used in glide ajax example and i will do cis perm underscore ci semicolon and i will do where gr equal to new glide record now table i will search for cmdb underscore ci and here i will do gr dot add query i will do says underscore id and here it should be same value so i will do cmdb ci that's a variable we have and this particular parameter value will come from the client and i will do gr dot query and if that ci is available that means gr dot next you can also do one one more thing that if support group is available then only you should run this i think that's a i would say additional check you can put so maybe i would do add query and or maybe i will just do gr dot we will had add not null so you will put the field name here and the field name is support group so if it is not null and then i will do g we will do return gr dot support underscore group so we are returning support group from this script include so i'm clicking on save so our script include is ready now i will create a client script so that i can call it i can call this script include to get this data get this support group so for that i will go to incident here i will keep this script include open because you would need that just to copy the names and i will go to client scripts click on new and here i can just give the name set assignment group set assignment group incident here i can just put on change and i can write the script here now here i have to create that glide ajax variable so i will do where ga new glide ajax we will copy the name of the script include i will put it here and then i will do ga dot add param sys perm underscore name and here we will put the function name and the function name we have is getci i will put it here semicolon and i will do gr now we will have parameter we have to capture it and send it to script include so that will be cis perm underscore ci and how exactly we will get the value the value you can get same thing i think you can do a new value because that's variable is that parameter is already available with this on change type of client script so we will use this and we will do ga dot we will do a call get xml and i will just call functions so maybe uh callback function i can do populate group that's a callback function so we will do function populate group with the parameter response and i can do where answer equal to response dot response caps xml dot document dot document element dot get attribute get attribute and then i can do answer semicolon and this time i have to push that means that set that value in the assignment group so i will do g underscore form because this client script so i will do set value and we will mention the field name that is assignment underscore group and the value would be which you will get from the script include that is just answer which will be stored in answer variable and that's a society of support group so that's it and i will save this it is saved now i will just try to look for configuration items so we'll do cmdb underscore ci dot list this will open the list of configuration items we have in the instance here i will filter it out and now i'm going to select let's see this one so i will go to incident form maybe i will create a new incident new form is opened and i will select this configuration item it is not setting it so there should be some mistake in our script let's take a look so i think that's that's that's this is how kind of interesting because if you don't get the response you don't get the expected results then it will be really easy now you can just go for debugging so in this case i already found the issue you can see we have this incorrect name here assignment group so i will just save this i think i think this is kind of giving you the learning as well while if i'm doing the mistake you can understand that you have to make sure that while writing the script or before you perform the testing make sure you're checking your syntax if everything is written uh i think with with correct syntax and the names are correct or not that's something you have to check before you perform the testing so um it's correct now let's see if if i do it again so i will reload it and i will select the same configuration item let's select it and if i select it it is not setting the value again that means there's still some some kind of issue so if i go here get ci i think we have the set assignment group maybe let's check that set assignment group answer we have here set value okay so we have done mistake here as well populate group another mistake we have done so let me save this and that's okay it is kind of debugging you are doing not at the actual debugging but you are just checking your script whether it is it is uh it is working you have written all the all the words or names you have given right names or not not that is something you can check and now i am going to my form again i will reload it and you always need to reload that's how your updated script can action can take that action the latest action which you have just updated so i will just do this select it and if i select it it is still not setting it is not working so i come here again and the reason behind it you can see that because we have selected active field here you have to select configuration item i think it's good we are doing a mistake i think it's good at least you will not do those mistakes if i'm doing the mistakes so in that case i have selected this so you can see we have these i think everything should be good now i hope fingers crossed we should not get any error now and let's select let's select the configuration item and it is not working yet we still have this new value we have sperm ci get ci it is again another mistake we have done you have given a space because it's a string it will definitely take that space as well so i will reload and we are doing a lot of mistakes i think so let's see i'm sure this time it should work if i have not done any kind of mistake yes at last we are able to populate assignment group the names which we have created that's something it's kind of wrong we have given so but but it is working you can see now it is populating id securities automatically so this is how you can achieve this kind of requirement if you will get this kind of requirement where you have to populate something on client and you can use glide ajax you can call server side code pull it on on the client and use that value on your client

View original source

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