JavaScript in #ServiceNow | A Quick Walkthrough | ServiceNow JavaScript Tutorial
[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 word service now this session is about javascript in servicenow if you want to be a servicenow developer and want to do scripting to perform complex customization in servicenow then you definitely need to know about javascript this session is not about advanced javascript training as the focus would only be on the basics of javascript which is basically used in daily development in service now topics to be covered in this training we will start with overview of javascript then we will talk about syntax used in javascript then we will see variables and data types and then we will see functions will also talk about conditions and then we will see how javascript is used in servicenow with all these above topics and then i will show you some examples by writing some scripts in javascript and service now before i start this session i would like to recommend you a free course of javascript on codeacademy.com if you are new to servicenow and new to development world you should definitely complete this course before you start learning development in servicenow i do have a complete scripting course you will find the link in the description as well that will train you and on the complete development training of servicenow but how exactly you can use different functionalities of service now and do the scripting into those functionalities into those elements of servicenow like business rules client scripts but before that it is very important to learn about javascript because that's a base scripting which you have to use in service now while doing development and while writing the script and service now now before i start my first topic i would also like to show you my personal developer instance now we will talk about different keywords different reservoirs different elements of javascript different syntax what exactly syntax we use in javascript so in order to show you the examples live examples what i will do i will go to background script so if if you have little bit experience in service now administration as well you must have seen or you must have learned about this module as well which will take you so if i click here you will see this background scripting it is something which is which is basically the server side scripting you can write the script here and you can check and and get some data this is something you can use in your i would say usual development process as well we have another feature similar to this like fixed script so it totally depends on you as per your use case what exactly you want to use but i just want to show you all examples practically in service now in that case i will use background script so i will make it a little bit large so i will zoom it a little bit and so that you can just see all the scripts i will write in this particular section overview of javascript javascript javascript is a scripting or programming language that allows you to implement complex features on web pages javascript is object oriented programming which means that you can build smaller objects and you can keep them and put them in a container there are three types of javascript client-side javascript which is an extended version of javascript that enables the enhancement and manipulation of web pages and client browsers server side javascript which is an extended version of javascript that enables backend access to the tables database file systems and servers and the third one is core javascript which is the base javascript language used by both client and server side java scripts in servicenow you will use all of these however servicenow has their own scripting methods and apis which you need to use while doing scripting in service now in javascript syntax before you write a code in javascript in servicenow it's very important to know about syntax used in javascript which is basically syntax of javascript not servers now but those are the syntax of javascript javascript is case sensitive so if you are declaring a variable in capital a letter with a string value test a and a small letter with same string value test a in that case capital a and small a will not be the same will not be equal which means that you have to make sure about case sensitive when you write a code in javascript in service now try not to use extra spaces while writing code in javascript you need to try to minimize the spaces in the statements so if we have this statement of code with spaces and another one without spaces then first one is not recommended and second one is recommended so if i take you to my personal developer instance so in this case as i was talking about for example where x equal to and here i just put maybe test same thing maybe test x and then i do where y just capital x and then i do test y now or maybe x now in this case you can see now these both values x and x are totally different they're not equal so that is something that means it is case sensitive so you can also declare a variable a in lowercase and you can also declare a or x or y any any variable in uppercase as well and if i talk about the spaces so you don't have to put this kind of space the basically the ideal ideal one is like this you should not have much of spaces anyhow this is not going to harm it but even i think even in servicenow you will get some i think error that a lot of spaces are there that's something you will definitely get i think while you will write the script in service now so the next is semicolon now in javascript every statement of code ends with semicolon so if you are putting semicolon that means that line of code is completed so in this case as you can see here so i have this where x equal to test x and this is what i am putting here so this is basically ending this line of code so whenever you want to end any line of code of statement you can just put semicolon so that's how you use that it's kind of a period it's kind of a stop for for any kind of lineup statement in javascript strings so when you define string values in javascript then you have to use double quotes or single quotes the value should start with code and end with code it can be double or single which will make it a string so like this you can see test and test now both are string but one is with single code and another one is double code so if i take you to my personal developer instance and here if i i want to make any string now in order to make any any valid string for example we have where x equal to if i type test now this is not string if i have to make this as a string value i have to use double quote or i can use single quote but you have to remember you have if you want to end the statement you have to put semi colon so that's how you can declare you can use string values in javascript parenthesis parentheses are used with conditions and functions so if you have to confirm any value of any variable or compare the values of two variables then you use parenthesis and the conditions should be mentioned between parentheses like you can see in this case if a equal to equal to 1 and you also declare a function with parenthesis so for example if i go here and if i have to write like this so in this case if i have for example where x equal to test uh here if i want to just con i want to confirm that so i can do where i have to put that in parentheses as i mentioned we have to use it with conditions so the condition is if x equal to equal to and i can just do test and the answer will definitely come true because that's a value of x so that's that's how you use parentheses similarly if you want to declare a function so i can just type function and i can type maybe my name and i can just put parenthesis that's how you declare a function as well comments now comments plays very important role in javascript developers can use comments to help other developers and mention about the purpose of line of code or block of code you can use double forward slash which will be treated as a comment and will not be executed as part of the code so if i come to my background script here and for example i'm going to write the script and if if i want of basically a line so that other developer can see the code and understand what exactly this code is about in that case i will put two forward slash and then i can just type this is just just a test code or whatever the purpose of the code is you can just mention you can mention anywhere you want even after this line even after this line wherever you want where you would you want to mention specifically for yourself as a developer if so you want yourself to make sure that you also remember it and maybe if you want to help other developers so as a developer if you're a good developer you should definitely make habit of using comments this is really great habit if you are if you're working as a developer if you have to write script in servicenow or anywhere as a developer you should definitely make this habit escape characters when you work with strings and if you need to add a spatial character like single or double quote which should be treated as a value then you need to use escape character an escape character enables you to use spatial character in the string and will be interpreted as a value in this case test is surrounded with codes however this code will return an error so in order to use this without giving an error you have to add backslash character before the spatial character every time to be interpreted as a value so if i go here if i come here in my personal developer instance now here if for example we have this string but for example test is a is a value or maybe i want to populate this value somewhere or maybe i can show you a quick example i think that is better let me let me try this so i will just remove this code whole code again now in this background script if i have to show you the printed results so i i just want to tell you that i will use g g s dot print if i will use this i would be able to print this basically so i will i will remove it from here first and then i will write the codes for example we have a equal to now i'm doing here uh sas now is a youtube channel that's good i put semicolon and now i write maybe gs dot print and i'm just printing a now if i will do that if i run this code now how exactly you can run the code in background script i will show you you need to go to a little bit bottom here so what i will do i will run the script so you can see it says sas now is a youtube channel that means you are getting the output now if i go back same code here and i put here double quote in the string now you put double quotes to make a value as a string but let's see if i it is i'm able to get the results so if i do this you can see that it is giving me javascript compiler error exception it says missing semicolon before now that's the reason it is not able to print now so if i go back now this time what i will do that's the reason it is asking for this it is thinking that this is kind of a quotations just to create the string value and you have to put some semicolon here that's what it is recommending you but that's not the case i really want to populate this so in that case what i will do i will do a backslash here i will come here i will do a black slash here and this time i will click on run script and let's see the magic if i click here you can see now and you can see the result it says sas now is a youtube channel so you can see we can see those in codes now and it is ignoring that and that's what you call as escape character reserve words in javascript you cannot use some reserve words as variables labels or function names those words are break case catch continue default delete do else false finally for function if in instance of new null return switch this through true try type off where and that that's the one which you will always use while doing scripting and service now void while and the last one is with so these are reserve words and you don't have to use these words in labels variables i will show you quickly in my personal developer instance so let me go back here now here we have this variable a let's say if i put variable f the if is a reserve word which i just showed you should i use this should i really use this as a developer let me run this first so what i will do i will run this and i will get an error you can see that it says javascript compiler error again missing variable that's something it is telling you missing a variable so because you are using a reserved word you're using a javascript reserver that's the reason it is giving you an error so if i go back and if i use a because a is not a reserve word and if i run this i will get the result i will get the output without any issue i'm getting it maybe let's say we have any other reservoir maybe like break and if i use here break let's say i'm using break and if i run this again you can see that you are still getting that error missing variable that means it is saying you have not declared any variable yet because break is a reservoir which you cannot use you're not allowed to use break but that's how you can you can use these reserve words so you have to know before using those words as variable you have to make sure that you are not using reserve words in your script variables and data types variables javascript variables are containers for storing data values in javascript variable can contain any type of data string values integers objects arrays functions you can initiate a variable with their keyword which is also a reserve word so if i take you to my personal developer instance and if i come here and i'll just put there so whenever you have to initiate a variable which has to store a value so in that case i can put for example a so a is a variable and how exactly you are initiating is it as var keyword and that's a keyword reserve word which is which we just learned so that's how you can declare now if i just maybe for example run this let's see if i get any error so you can see that i did not get any error but it didn't it didn't have any kind of value we didn't do any kind of print so if i do for example gs dot print let's say if i if i get something so i'm doing uh where a then i'm doing uh gs.print i'm not giving any value now and i'm running this let's see it says undefined that means if you will create a variable and you will not give any value you will not try to store any value in that case you will get this kind of error in your script as a developer you have to know it because in servicenow this is really really important so if i go back we have this a but here if i give it a value maybe equal to not double you have to a equal to 100 and now this time i print it i run this if i'm running it you can see i can get the value stored in a a variable and that is what i am getting here data types there are five data types in javascript five types of basic data types string number boolean array and object so these are five basic data types used in javascript first one is a string you provide the string value by putting codes between the values and the string is basically made you can see with the help of double codes and you can also make string value and single quotes as well so if i take you to my personal developer instance here and if i just do maybe we can just change here and i can put maybe youtube that's one string i can do where b equal to and i can do channel and if i do a plus b let's see if i can get something and if i run this i'm getting youtube channel but here i have just some double quotes but i want to put it as a single code but that's what we are learning you can make the string with double quotes and single quote i will click on run script you can see i'm still getting the same output no difference that means you can create string with double quotes and single quotes number variable is defined by using integer values so that means so if i have a variable which will store integer value in that case that means that case that will be called as a number data type it will be integer data type that's what you can call here so that's this time a is basically a number data type variable because you're putting here integer values boolean boolean is true or false so if you want to use boolean you can just do as a true or false so for example if i come here and i do uh a it's called to true now a variable is a boolean type of variable because the value you have storing here true but there's a big difference if i put them in for example single quote now it becomes string so a is the data type for a variable would be string now but if i will remove it if i'm just keeping as true then it will be called as boolean and you know that true and false are reserved words as well array array data types which holds different values like test 1 test 2 in this a array so if i come here and if i want to store different values so i can just do for example x i can do comma i can put here y i can do comma and then i can do z so in this case a basically is a array data type variable if you mention like this then we have objects the last one the last data type which we were talking about which is a way to hold structured data like an array but with named properties and here a is an object with different properties like name roll number so you can use the value of property by calling object dot property like in this case it has used a dot name so if i come here here rather than this i just do curly brackets and i just put like a equal to not a maybe i will do channel equal to sas now but i can do maybe um platform and i can do i can do colon not equal to so i can do colon so i can do channel colon sas now platform uh colon i can do maybe service now now if i want to use that so for example i have to print the value so i will show you quickly how what exactly i'm talking about i'll make a little bit bigger again you can see here gs.print um where like this and here i can do semicolon what i will do i will do dot a dot channel what do you think what exactly it will print will it print or not yes it will print but it will print not these values it will print sas now because that's what the value of this property and if i go a little bit bottom here and i click on run script i got some error javascript var equal to script line error okay it's giving me an error but where we have that line one i i forgot one thing now these values have to be in quotations yeah that's something we forgot so i can do like this this time it should not give any kind of error and i can click on run script now you can see we have sas now so we have got the value of that property and you can use this and i'm sure you will definitely use this in your service now scripting you will definitely this is kind of a daily uses thing which you have to use in service now functions a javascript function is a block of code designed to perform a particular task it is executed when something calls it a javascript function is defined with the function keyword followed by a name followed by parenthesis you can define a function by starting with function keyword then you have to mention the name of the function like in this case we have used my function then you have to use open and closed parenthesis and then you will mention start and closing curly break brackets now in these curly brackets you can put your code which you want to run for an output you can also use parameters as well in function and the value of parameter can be posed basically when actually you call the function to run that particular block of code so this is how you define a function if i go to my personal developer instance for function i can type function as i mentioned so we have to declare it with function keyword that's a reserve word as well and then i can give any name maybe my channel that's just a that's just a name of my function and i can just use parentheses or i can put these curly brackets and now i can i can put a code here so for example in this case i want to do gs dot print hi this is or maybe this is sas now channel and i just do like this now you will see i will run this code will it run it yes and if i do run script you can see there's no output but the code ran but i don't see any output the reason behind it after defining a function you also need to call it so if you can just little bit rewind and that's what i mentioned when i was defining the function when i was telling you about the function now you can define a function but you also need to call it now in order to call this i will just go down here and i will just put same the name of my function which i have created so i can just do channel put parenthesis semicolon and now you are done and if i go a little bit bottom here and i click on run script you will see the magic you can see this is this server this is i think we're not getting that this is maybe i forgot to write that maybe i can just do it again and run this you can see now we have this is sas now channel so we are getting the output as well so this is how you declare function we were talking about that that parameters that how you give parameters as well you can still do that here let's say i'm putting parameter a here now and i can do this is maybe let me do some some tweaks here what i will do this is i will do plus a i will do plus again i'm just making the string now i don't have that sas now but i will still show you the output as sas now and the same thing same output no difference how we can do that with the parameter this time what i will do while declaring by calling the function i will also push the parameter and for that i will do here i will make it sas now and if i run this script if i run this you can see that we got the output this is sas now channel no difference there is no indifference in the output same output and that's how you can use parameters in the function so while you use scripting in servicenow you have to remember functions because functions i would say the most important um element of of the scripting javascript and service now as well javascript as well and even for servicenow scripting you will use a lot all of these functions there are a lot of functions you will use you will create a lot of functions and service now you will write a lot of scripts in function in service now conditions and service now in order to do scripting in order to get some output you definitely have to use conditions because in that case you you want to perform some action and you want to perform or run a code depend on the kind of condition you have and condition is being met so for that we will talk about conditional statements conditional statements are used to perform different actions based on different conditions when need to perform some action once condition is met then you use conditional statements you can use if else else if and switch so these are the four four basically different and these are basically reserve words as well these are reserve words which basically you can use and create conditional statements you can use if to specify a block of code to be executed if a specified condition is true the syntax for if condition starts with if keyword then you put condition in parenthesis like a equal to equal to hundred then you put a code between curly brackets to execute the condition execute the code if condition is true so if a is really equal to equal to 100 then you can just run that code so once you are done with this if and you want to execute that code with that if condition then you can also use else that means if that first condition is not being met and then you can use else that what else you want to execute that you can mention over here you can also use else if we're going to put another condition and you can put the code which you really want to execute in order to do comparison in condition for if else if there are different operators used in javascript you should remember these operators as these will be used in your daily scripting for service now we have equal to equal to which will basically say that whether one variable is equal to that particular another value or not then we have not equal greater than greater than equal to less than equal to less than exact same if you have mirror value or not exact same that means equal equal equal that's exact same and you have not equal equal that is basically exclamation and then equal equal that is not exact same you will definitely use most of these operators while using or scripting in-service now in javascript if i take you to my personal developer instance here so let me show you about if else if else if conditions if else if or else conditions so let's say i'm i'm going to write a script so maybe i will do a equal to maybe 10. i will do verb be equal to maybe 20. now how can i use it so let's say i can do if now i will start writing this script with this if condition i will make it a little bit large we will do if a equal to equal to 10 if the value of a is 10 then i should get output as g s dot so if that condition will be true that means a equal to equal to 10 then that it will execute this code and i can do or rather than this i can do one thing here let's say if a is less than 11. or maybe i will just remove this one let's not use this variable i think that's that's better so that you can understand uh in that way and i have a equal to 10. so if a is greater than greater than five then you can print a is greater than 5. and i can do else if and if a is greater than or maybe i can just do less than five then it should execute this code that will be geos dot print and i will have a is a is less than five now so what i will do i will run this code now so if a is greater than five it should print like this if it is less than five it should print like this if i go at the bottom and i run this script running it no error and you can see that the output is a is greater than five i can change this output how because it is checking the condition if it is true print this if this is true print this so in this case i will make it 4 and if i run this it says a is less than 5 so that's how you can use if condition else if condition now if both is not true let's say both is not true so in that case i can do maybe here else else but i need to change a little bit of code as well so i will do gs.print and i can do nothing nothing is true and i put semicolon i go at the top now here it should the if nothing is there a is not greater than five so in that case if a equal to a equal to equal to 10. and if a is less than else if a is less than five or if both conditions are not true then it should print print nothing is true so in that case i will just change this value so maybe i will make it 15 and let's say if it works and i'm going to run this you can say nothing is true because above two conditions are not being met if you're not able to so these are returning false and that's the reason it is not printing these values because it will only print if the value become true so in this case because we are getting that true value here both of them are not true but okay let's print this if both are not true else print this that's how you use if else if and else how javascript is used in service now scripting in service now servicenow provides apis which provide classes and methods that you can use in scripts to define functionality these apis are as javascript classes you can use apis for a server side and client-side is scripting as an example glide record api glide racket is a class for database operation rather than writing sql queries in servicenow glide racket is a special java class that can be used in javascript exactly as a javascript class a glide record is an object that contains records from a single table how exactly you can use glide record we will see soon you can get all the information about apis provided by servicenow on developer.servicenow.com so whatever api is provided by servicenow you can just go to developer.servicenow.com and if you want to navigate over there then you have to go to developer.servicenow.com you need to click on reference then you have to click on client side api you will also see some other options like server side as well and then you can access all the apis provided by service now this is my personal developer instance so i will just remove all the scripts we have so i will just remember remove from here now what i will show you so let me just make it a little bit big now what exactly i will show you now as i mentioned that how exactly you can use javascript in service now so as of now whatever script i was running that was not somehow related to servicenow scripting now what exactly servicenow scripting is now servicenow scripting is somehow that what exactly you are doing anything to get or or define any functionality of servicenow like as an example that you have to fetch some number of rackets from incident table so for that you have to write a script and servicenow has provided apis for that like we were talking about glide racket for glide rocker if i talk about the syntax perspective so you start creating a glide racket object so for that we just do like write var we do gr equal to new that's a syntax till here everything is same except this variable name it's up to you what variable you want to put and then you have to select the table name which has to be string so if i put here i can just put incident that's a table name and service now and then i can give semicolon now i need to get for example i need to get some records so maybe in that case what i will do gr dot add query so in this case we will we will try to get all those records we will again use the string and i will do comma now here i will put the value what value i need so maybe i will do 6 all the result i think 6 is the result value we have gr dot add query and then i can do gr dot query that means it will query the table now and then i can do while so that it can loop all the rackets we have with the same condition we are putting here and then i can do gr dot next that's it and if i come here and now i can do gs dot print now what exactly you want to print we will print their number plus i will give a gap maybe colon and then i will do gr dot i can give maybe state we just want to see the state as well and then i can do get display value now for full development training i i do have a separate series as i mentioned uh earlier as well so uh you'll find that link in the description of this video as well but for now it's just kind of a demo because overall our training is on javascript and how exactly you can use javascript in-service now uh with the glide with with servicenow out-of-the-box apis we have so i'm doing here gr.getdisplayvalue i think maybe i i won't use it for now um and gr.state and then i can just put semicolon and now i can just run it if i will run it it says not authorized why exactly is that i should be i should be because i am an administrator um let me let me check i am a system administrator and we have then scope global um i will check again yep it ran but i didn't get any kind of output maybe we don't have any racket so maybe i can do maybe one let's check that if the state is one if i run this yes we are able to get this you can see we are getting a lot of incidents where we have state as one that's a backend value and i'm able to get it so this is how you can use javascript with the help of with with other apis you have provided by servicenow
https://www.youtube.com/watch?v=upO67dKd_HA