logo

NJP

What is GlideRecord in ServiceNow | A Complete practical demo of GlideRecord | GLIDERECORD METHODS

Import · Aug 22, 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 with servers now in this session we will talk about glide racket service now provides lot of javascript apis which you can use in scripting to achieve some complex functionalities there are a lot of classes and methods which you can use to extend servicenow platform capabilities glide racket is one of the api provided by a service now it also has different methods which you can use for different operations in service now glide racket glide racket is used to perform database operations now those operations are acquiring records from a table insert record in a table update records in a table or delete records in a table the actual use cases of these operations are like updating short description in multiple incident records and these multiple incidents because these are incident table creating a new problem from incident that's another use case now glide racket contains both rackets and fields so once you will learn how to write glide record queries and want to apply in your instance then you have to make sure that you are running all those queries in lower instance and not on production instance directly because you have to make sure whatever script you have written it is giving the expected result once you are getting the expected result in lower instance then you can run the same query in production glide racket api is the mostly used api by servicenow developers i would say that you would use glide record in your daily development you will mostly use it so if you are a servicenow developer if you are a person who are right keep on writing scripts in service now then you will definitely use glide record because service now is all about table so you always have to update something create something query something in service now so you definitely have to use glide racket in most of the in achieving most of the functionalities where you can write glide racket glide racket should run on server side so basically the syntax you have for glide record script which you write you always run it on server side you can write glide racket script in business rules script includes ui actions mail scripts and scripted schedule jobs and there are some other elements as well in service now where you can write server side code wherever you you can write that you can use glide racket let's learn about the syntax of using glide racket class and calling its different methods to perform different operations so this is the syntax of initiation of glide record object in which you first need to create an object of light racket class now this is not something related to servicenow script when i when i'm talking about creating an object because this is about core javascript syntax because that's how you create an object of a class so you need to mention where keyword now var keyword is basically a reserve word which is used to declare a variable in javascript then you mention the name of the variable like gr which can be dynamic so it's up to developer what variable you want to put here then you mention equal to new and new is again a reserve word of javascript then you put glide racket in which g and r should be in upper case and then you put parameter which is basically the table name on which you want to query the records now this should be in a string with quotations and then you mention semicolon basically this is the actual example for incident table in which incident table is mentioned in the parameter so in order to perform that operation that table operation you have to mention that tables in this example we have mentioned incident table and in this whole script this line of code gr and incident are dynamic which can be changed now rest should be same so if i talk about syntax perspective when you will use in your scripting then rest should be same but gr that's a variable you can change it as a developer and as per your requirement from what table you want to do that query whatever operation you want to do that's something you can select or mention in glide record parameter now once you have initiated the glide record object then you can call the method of glide record class so in order to query the records the method is query so you can call it by mentioning gr.query with brackets and then semicolon with brackets and then semicolon now this code will perform the query on the table mentioned in the glide record object parameter so in this case we have incident so this will do a query on incident table now then you mentioned gr.next which is another method to move to the next record of the table when you query it will keep on moving to the next record and that's what you mentioned as part of this line of code gr dot next and that's another method of glide record class now i will show you this particular example practically in my instance soon the overall syntax of glide racket is like this in which you initiate the glide racket object by mentioning the table name then you call add query method which is used to put the filters then you call query method which will which will query the table as per the filter you have mentioned in add query method and then you will do a loop for every record of the table by calling next method in while loop and then you will keep on printing every record retrieved from that table and that's that's a basically kind of a main script which you will normally use but yeah you will definitely change a lot of i would say a lot of methods for in order to do some different operations as per your requirement background script now scripts background is a powerful tool to run javascript in servicenow instance so if you want to test some server side logic you can directly go to background script and write the script as you can see on the screen and run the code and check if you're getting the expected output now i will write all glide record records scripts whatever glide racket scripts i will be giving demo in this particular session i will write those scripts in this particular section i will show it to you in this particular tool in my practical demo now in order to test your script you should have a way to print the output on the same screen so that you can test your script logic without going to logs racket like as in service now you can definitely do like gs.debug gs.log which will basically put the information in the logs table but with the help of background script what can be the way that you can get the output right away on the same screen in that case the way is basically using glite system method that is print so if you want to print the output from your script then you can use gs.print and you can mention the variable or string in the parameter and it will print the output accordingly so whatever output you will get from your script and if you want to print it you want to check whether i am getting the right output or not that's something you can keep in gs dot print and then you can see the output accordingly as per your script so this is my personal developer instance so what i will do first i will go to the scripts background module so if i type here by background and you will see under system definition we have this script background and if i click on it you will see the background script tool and here is the section where you can write the scripts now in order to run the script to commit the script you can click on this button run script which is at the bottom over here and then you can see the output so let's say we just talked about gs dot print so if i type here or maybe i will just make it a little bit bigger so that you can see the script properly so i will mention here gs dot print and here i can just give it this is the demo of uh glide racket and i will just do semicolon and if i click on run will you get the output answer is yes you can see that it says this is the demo of glide record so now i hope you must have got it that if whatever script you will write in in this particular section and whatever output you will get from your script that you can print it with the help of gs dot print and you can check we can basically uh validate the logic whatever logic you are putting whether you are getting the right output or not glide record methods as i mentioned earlier servicenow provides classes and methods now we will see different methods used in glide racket the first method we have is query and we already talked about this method so this method basically queries the table mentioned in the glide record object and look for the records next method is next after query which basically moves the to the next record in the table mentioned in the glide record so it will keep on moving to the next record as per the script you are mentioning if you're doing a loop it will keep on going to it will keep on moving to the next record in in that particular query so maybe if it is returning 10 records it will keep on going to the next record that's what this particular method does so if i go back to my personal developer instance so what we will do we will write the first script for today of this particular session with glide racket and which we will do with the help of these do these two methods which we just learned about so let's start so if i just delete it for now and maybe i will make it a little bit more bigger so what i will do the first line of code as we mentioned earlier we have to create and create a glide record object so what i will do i will do where and we will just do gr or maybe this time just to so that you understand better i will just do maybe demo demo demo glide and i will do equal to and then we will do new and here i will mention glide record and i will give the table name i will do table name here in the string semicolon now the next line would be querying that means i have now created an object or glide racket object mention the table name now i have to query this table how exactly i will do that i will do demo glide dot i will do query brackets and i can do semicolon i will go to next line now before going to the next line or mention the code let's run this code and see if we will get any output will i get it if i go here and if i run this answer is no you will not get any kind of output overall we don't even have i think mentioned gs.print so you will not get anything what i will do now i will mention here demo glide dot next now this time it as of now it has just queried that particular table but now it is not keep on moving it is not moving uh two to those different records of that particular table in this case incident table now what i have to do to print this so what i can do i can do here gs dot print and what i will print here let's print gr dot number now after acquiring the table when script will return the racket and then you can push the value to a particular field in the table in the output for that you have to use gr dot field name that means if you want if you have queried the table and if you have also moved to the next record so maybe the next record is the first record and then you have to uh push some value for that particular table in in of of maybe one of the field for a particular field how can you do that that's something as i mentioned here you have gr so uh i think i think that's again we have done a little bit of mistake here it should not be gr we will do here demo i think that gr variable still in my mind so that's the reason but it is it should not be gr you will mention demo glide dot number so what it will do it will pick this object here's object and then it will automatically pick the number field and its value so it will basically print the value of that particular field for the first record basically retrieved with this query so what i will do i will just commit basically run this script and see if i'm getting the right output or not so i will go a little bit bottom here so i can do run script if i will do that you can see here we have got incident number so because we are getting this number because that's what we have mentioned in our script if i will do for example maybe let me change this if i will change it to short underscore description so that's a basically syntax so if you want to print the value for for this particular rackets whatever records we are getting from from this particular table you have to use object dot and then you have to put the field name and i can just close it and i will run it this time it will basically pull the first record but print the short description the data from the short description field so this is how you use the syntax so that's that's how you provide the syntax for for glide record now as of now you can see we just got one racket single racket now how can you get how can you print multiple rackets now in order to print multiple rackets you have to use loop so if you remember in just our previous section we were talking about the main script where we mentioned while loop so as of now i'm not looping anything there's no loop in this script as of now but let me loop it so that you will see the difference in the output so what i will do here i will quickly write here while now in while i will do gr.next now why exactly it is checking do i have the next record answer is yes so let's print it if i have the next record that's what the loop is so i'm looping it but it is still checking do i have the next record that means do i have something in this object as a next record if answer is yes yep okay go and print it so this time i will remove this and i will do brackets here i will come here brackets here curly brackets and now i'm going to run this what will happen if i will click on run it will print all the records the short description of all the records of that particular table now how many records we are getting that's something we don't know as of now for that we can do one thing if i go back so there is another method which you can use with glide record and that is something if i do maybe let me do it here i will do it every time i am printing it maybe so i will do a gs dot print and the method you use demo glide dot and that is get row count now if you do like this this will again print it multiple times now why exactly it is printing it multiple times because that's what we have mentioned here if i'm finding the next record printed finding the next record print it and that's what it is keep on doing it so in that case i will run this and if you will see we have total 70 incidents how can i validate it so let me go to incident table and check the number of brackets we have so if i go here and let's open incident module here and i will go to all that's what we have to check and if i open the list of incidents and the answer is totally correct we have 70 number of incidents and that's what it is doing so basically what it does whatever records this script is returning whatever rackets we are able to return that's what this particular uh basically this this method will store so it will basically store uh the number of records number of rows you have in that particular query that is what you can store here it is still printing one by one but overall if you will just put the count you will just get the total count so for example if i remove it from here let's see and if i will if i if i will keep it in the loop then it will keep on printing like it was doing before but let's say i'm i'm not doing anything and i will just keep it out of loop i will just print it just one time and if i go at the bottom and if i click on run you will just see 70 that's the basically the row count how many records we are returning how many records because there is no filter as of now as you can see we're not adding any kind of add query as you can see here it's just querying the table and returning the records the next method is add query which provide ability to filter the records you want to query from the table so overall if you have the filter the data which you are acquiring from a table then you have to use add query there are some other methods as well which you can use to filter a specific type of records now this method also needs parameters to be passed as you can see first is field name so you have to specify the field then you have to specify the operator and then value of the field this will filter the data while acquiring that particular table which you have selected which you have mentioned in glide record object so if i go here in my script now if i just enter just add that particular method so what i will do i will put here demo i will do glide dot add query and then i will put here first field and field maybe so let's see we will just pull all maybe the incidents which are priority two just an example but how we will validate it in order to do validation we will just go to our incident list here and maybe i will just filter here and i will do let's say priority is 2 that is high and if i run this overall we have 4 records here now if i do same thing here so i have to first put the priority field the backend name of the field and i will do comma what i can do i can just put operator here which is equal i will show you what operators you can use but before that i'm just putting this operator and here i can put the backend value of that 2 minus high choice and that would be 2. so what exactly it will do this will return only those records where priority is two and we can also count the number of records so here what i can do i can do gs dot print and then i can just mention um maybe number is that's a string i'm putting number is space and i will just print here demo glide dot this time i will print number so that you can also validate it so i will click on run script you can see that we are getting four records because of that filter and all those records are right here one nine two seven four four two nine if i come here one nine two seven four four four nine same records no difference that means we are getting the right records with the help of same script let's see the operators which you can use in service now while using add query method so basically operators are basically divided into two sections i would say one is you have query operators and then you have string operators so the above one which you can see here in in this bar that's basically query operators and then you have string operators now how can you use them so the first one have we have already used that is equal to now how can we use others so let's see that let's start with uh the second one so if i go here and if i go back and i will go to my script now here i have to do not equal to two so what will happen this time so this time prior to two incidents will be ignored they will not be fetched in this particular query and if i go here and i run this you will see we are and we have 66 rackets overall we had 70 rackets and now we have 66 that means we are getting the right output now if i go back and if i check here we have greater than as well i think uh greater than is something it's up to you maybe all all those choices maybe for example here if i mentioned [Music] maybe wherever we have greater than one that means all the choices will be included uh so it's up to you it's totally up to you what kind of output you want it's up to the developer what kind of output they want but that's how they can select the operator so here we have greater than one and if i run this so you can see we are getting 41 racket that means this does not include priority one incidence so if i go back here again let's use some string operators so for example if i do short description description and here i put maybe contains and that should be in capital letters in uppercase and here i can do email so let's search for this email word whether short description contains email word or not it should only query those records and if i do this and we can also type short description so that you understand you're getting the right output so and sd is that's fine we can do here demo glide and i can do dot short underscore short description you're done script is done and if i click on run script yes you can see that it is printing all those incident records where we have this email word and you can see we have total as well we have eight rackets and you can also search it if i go here and maybe i will just select all and if i do here like this short description and i search for email and i press enter yep you can see that we have eight records so wherever we have this email word we have those eight records in those incidents so that's how you can put query and you can just use these string operators and you can use basically query operators the next method we have for glide record is add active query now this would apply the filter to return only active records now wherever active field in the table is true that means whatever rackets you have and you have a field called active wherever that field value is true it will return only those rackets similarly we do have another method so that it's better to show you both the methods at the same time that is add inactive query now this will apply the filter to return all the inactive rockets where active field is false let's take a look so if i go to my personal developer instance here and i go to incident lists so here in the list if i just do all rackets and if i just apply a filter um while applying the filter if i do let's say this active so we have this active field and if i do active equal to true so if i run this how many records we have let's see 42 and if i go here and let's say uh we will just comment this one so this is again javascript i'm just commenting this particular line and this time i am going to use demo glide dot add active query and then it doesn't need any parameter and then i can just do semicolon query next that's it and here gs.print demo number sds this time we will maybe active field let's say and we will just use active we will print basically the value of active field for all these rackets because we want to see now we are querying basically active records that means active is true so what exactly we should get here all should be true and we will get the row count as well i'm going to run this you can see now and we can see all the records are active field true and we have 42 records and here as well we have 42 records now overall if we had around 42 rackets overall we had 70 rackets and if i will make it false and if i run this we have 28 rackets out of 70. so if i go back here and this time i will just run this inactive query so i will just do in active query that's it this time this will return inactive records where active equal to false and if i run this you will see the magic we have active field is false and we have 28 records same count which we have here so that's i think you are able to uh compare and see the difference that as you are getting the same result that whatever we are doing manually as a user you can do it in service now with the help of script as well and that's what i can say the power of glide record what you can do in a database in a table same thing while you do for example sql query you write right similarly you can do that same same result same kind of method operation you can use with the help of glide racket here is another powerful method of glide racket add encoded query which also needs parameter so basically this will filter the records as per the query you will mention so as of now you were seeing that we will basically putting the query we were filtering the data with the help of fields and and operators but here you just need to get the query paste it in the parameter and this will automatically filter the data as per the query you will put in that particular parameter let's take a look so if i go here in my instance now first before i show you the script first we have to check how exactly you will get that query now the way you have to get the query is i will go to here so you have to go to the list view of any table like here we have incident list and whatever whatever filter you want to apply here let's say maybe i will i will apply this time i will date i will just do maybe empty it should not populate the empty i will do filter out let's see how many records i think i don't have any record so maybe i will remove this one so you can see here we have three records now even after after these three rackets maybe i want to i want to just filter more um let's can we do we have some difference maybe this time we can make it critical one maybe i will just remove this one as well so if i do like this and and if i filter it out so now you can see we only have these two records that means i have already filtered it out with the help of these queries here now this this particular filter how exactly i can convert it into query so the way is i think you must have learnt it in your administration as well administration training you just need to right click here you will get this copy query you get this option coipy query you will go to your script this time and what you will do i will comment this one as well and this time i will go i will just go to the next line and i will do demo glide dot add encoded query now here is the string we were talking about the parameter and we will put it in the string and you just need to do ctrl v just paste the parameter the query which you have just got from your list view that's it nothing else you have to do you are ready with the filter you don't have to put this multiple times you don't have to use this add query anymore just put the encoded query as per the filter you want to put copy the query paste it here and run it and see the show so number is activist feel true i think we don't need this but maybe we'll just have number and i will just run it let's see so you can see that we are getting two number two rackets from from this query and if i go here we also have two records same and if i validate the incident number 233 same here no difference that means we are able to validate it and we are able to get the right data as per your script you have mentioned with the help of encoded query and i would say that encoded query guys it's very powerful because sometimes uh it's really hard to put multiple ad query because that why i'm saying multiple ad query maybe i will quickly show you that as well so if i will make it as a comment here why i'm talking about multiple query now same thing i can also achieve it here as well so for example short description contains email let's say and if i go here so let me let me make that particular condition first and then i will show you what exactly i am talking about so i will do here a short description and i will put here contains email and i will remove second one and i will do add add end now this and will basically applied in the same way while you will do in scripting so i will just mention maybe priority is two and if i run this we don't have single racket so maybe let's let's run the racket and see the priority first so we do have some critical as well we don't have priority two that's the reason you were getting empty so in that case i will [Music] i will do and priority is critical and if i run this you will see i'm getting four records so overall we had if i remove this we had eight records but now after that after putting the priority filter filter we were getting four records if i go to my script now now we have already added this one add query what i will do i will apply another filter how exactly i can do that just repeat the same thing demo dot glide demo glide dot add query and here i can put priority in a gain string priority comma now you will think we have to put the operator but there's another way that you don't need to put operator as well and that operator is basically you can say it's a silent operator and that's equal to so if i will not put any operator and i directly put the value of the field so maybe one here and if i put semicolon will i get the racket will i get the output in this case should it give error because i'm not putting the operator answer is no but let me just close it as well here and now if i run this you will see the magic we are getting eight records so that means i think it's still doing priority contains email and priority is one short description contains email okay the reason behind it because our our query syntax is not correct and that's the reason i was also surprised at why i'm not getting the right result but this time result will be 100 correct what we were getting over here so if i go here run the script you will see the magic now you will see we have four records right here same records 1 4 13 5. if i go here 1 5 13 32 okay because we have not put in those those filters uh i was again like worried that why i'm getting i'm not getting that so maybe i will just put priority again priority is p1 i will run this and absolutely 1 13 5 4 and if i go here 1 13 5 4 absolutely we're getting four records same here same output that means this is how you can use that means you can put multiple queries multiple filter as well with the help of add query but if you want to use uh encoded query i think that is also very powerful in which you don't have to basically mention these multiple queries just provide the filter here it's very easy right because you can do it manually you don't have to remember what query you have to provide so you can just copy it here paste it over there get the data expected data whatever you want as per your requirement the next method is add join query where you have to provide the parameter as well and that's another table name now this particular method is used to apply filter to return rackets based on a relationship in a related table so let's say as an example as you know we have incident records they are also related with the problem so for example we have this list of incidents and if i go here you will see we have a lot of problems as well attached to those incidents now i want to see some problems as per the condition of incident rackets can i do that answer is yes so you might get those kind of use cases where maybe incidents are active maybe incidents are inactive how can you how can you query basically problem brackets as per the incident incident data that's that's something you can achieve with the help of add join query so if i go to my script here let me first just comment all the line of code i have because we will write a new script and i don't want to delete it so that we don't have to just rewrite it so i can just comment and i could go a little bit bottom now i will write the script so maybe i will i will copy this particular line of code i will put it right here and i can just change basically this because we have to query problem records on the as per the condition of incident records so maybe i will do like this first and then you will do just create another variable where we will store that another table conditions so we will have a equal to and you can do here demo glide glide dot add join query this will join the query and here i will mention the table and table i will mention incident and i can put it here maybe let's say i have to provide the condition so what i can do for that i can do a dot add condition it's just like add query we do so you have to put a dot add condition and then you have to do maybe priority is critical if i do like this and then i will do demo glide dot query demo i think i can just do while and then i can do demo glide dot next and here i can do gs dot print the incident number incident number and i can just do demo i think here we have to print problem number not incident number problem number here we will see how many rackets we will get so i will do demo glide dot number that's it so let's see how exactly we will get the data if i will do run script no errors you can see here we are getting only four records that means in all these four problem brackets the incident priority is p1 and that's the reason we are getting this maybe if i change this let's see if let's make it two can i get the output run script you can see we're getting only one so if i go here and maybe i try to filter here two you can see we only have one problem bracket and that's what we have got here as well that that means your script is working so this is how you can use add join query to basically fetch the data as per the related tables related data as well the next method is add not null query and the parameter you provide name of field the feed name the backend name of the field this will basically apply the filter to return the records where mentioned field in the parameter is not empty that means is not null similarly we have another method as well which is just opposite of this one that is add null query where you have again you have to provide the parameter but this time this will apply the filter to return the records where mentioned field in the parameter is empty how can you achieve it so let's take a look in my instance so add null query add not null query so if i go here or maybe i will just remove this one and now i will also remove all these queries maybe all this line of code which we have written and i will go here and i will just delete it i will keep it as it is for now because we have to see what kind of query we need so if i go to list of incidents here and if i see here let's say we have this parent incident so if i just see all the all the incident list if i click here i will get all the incident list and we want to see wherever we have that particular field empty so we will start with not null not empty so let's see we have this one let's see the problem field let's let's use that one so if i do not null that means the field is not null so in that case i can just filter it out if i will do that so here here are the basically a list of incidents where problem is not empty so but it's still showing this one as well maybe i will just do like this and i think maybe just because of the uh force uh maybe basically data that's it is showing like this maybe it's kind of a bug i would say but here you can see we have 17 records which are not null so in that case if i go to my script here and i just do demo glide dot add not null query and the field name we will do the problem i hope that's a name of the field so maybe i will quickly open it because we want to see the backend name of the field so i'm just opening the form and i will go to related rackets here if i right click here it's problem underscore id and if i go here and i can just do problem underscore id that's it i can also comment this one i don't need to use this code it will query and then next and it will print um and let's do one thing and if i maybe and problem number is and i can just do here problem underscore id because we are doing not null that means it will definitely print the problem id as well and we are putting the number incident number and if i run this let's see what we get so we are getting you can see now it is not putting the problem id you can see it is basically pushing uh printing the cis id because it's a reference field problem number is a reference field but we are getting 18 rackets and that's what we got over there but we had to remove and that's because of as i mentioned some kind of a bug maybe that field is still has some value maybe it's not display value which is which which we can see but something there at the back end maybe but overall we are getting the right brackets you can see here we are we have all the all the data which we are getting they have problem number available now as you can see we are getting this society how can you make them so that you can show the number for that what you can do you can come over here if i do now any reference field you have whenever you will print it it will always print or provide you the output as the cis id but if you want to get it the display value that's like a number depend on the kind of display value we have you can just do get this is another method which you can use with the glide record as well with the current as well not just with glide record it's a server side code so we can do get and if i do display and if i do here value like this and if i run the script again you will see the difference and you can also see the same record one to this one where we removed that particular basically uh data from our list as well from this list that's what we try to remove so we are getting but else we do have all the problem rackets you can see here but this time it is showing me the display value the number number field of problem table as well else if you will not put get display value it will just show you the cis id of that reference field so let's try another as well that is null so as of now we have this not null now we will do null so if i just remove it that's it that's what your basically query would be so if it's null and if i just run this you will see the magic if i run this you can see we are not getting single problem bracket because that's a null problem feed is null there's no value in it and we are getting all those incident records we have about 52 and if i come here and if i maybe just just a problem i will just do like this problem is empty that's what we have and if i run this you can see we are getting 52 records and over here as well we have 52 records so that's how you can run use these kind of methods in order to query the record from different tables you have in servicenow instance the next method we have is set limit which requires parameter that is number in digits and this basically method is utilized to limit the number of rackets returned from a table so as you can see here in this script so if i run this again now as of now you can see we are getting 52 rackets now even if you are getting 52 records but if you want to limit this number maybe you just want to fetch only five rackets even it is matching the same condition and it is returning 52 rackets but you as part of the display perspective it will only return five rackets in that case and you might get this kind of requirement as well uh so if i go back here so what you will do i will just do here demo glide dot and you can see here all these are comments right now maybe i will put them at the bottom so that you don't confuse that is much better so i will just put it at the bottom i will cut it here and put all these in comments over here so this code will not run because we have commented it so if i just go here um and here you will do demoglite that's our object and then i will that's a variable we created and then i will just do set capital letters l will be in capital letters set limit now here you can provide the number so maybe let's say we will provide and it should be in string so if i do 5 and semicolon so now this will not return those 52 rackets this time it will only return 5 rackets any random five rackets and if i run this you will see that it is returning only five records the next method we have is set workflow which will require a parameter as true or false now you will use this particular method when you do not want to run any other business rule in the system when you are updating something so for example as a use case if i talk about the use case so you are basically acquiring some rackets and you are updating those records now how you update it that's something we haven't talked about it yet because as of now we were just querying the records for updating the records we will talk about it so but in this case once you will update the rackets if you don't want any other business rule because it might happen that if you're updating maybe short description if you're updating maybe description it might happen there is some business rule written in the system that means for every update run this business rule in that case if you do not want while updating all these incidents then you can put the set workflow to false so it's pretty much straightforward if i go to my instance you just need to mention that demo glide dot set workflow and here you can just pass false that's it so if you will put false here that none of the business rule which normally gets triggered with the update or whatever action operation you are doing on the incident they will not trigger that's a functionality of dot set workflow by providing the parameter the next method we have is order by that means whatever records you are fetching if you want to sort them as per one of the field in that case you can use order by and you have to put the parameter as the field name now similarly like order by this will do ascending we have another one that is order by d e s c and then you provide the field name as a parameter this will basically order the field sort the records in descending order let's take a look so we have order by and then we have order by descending so d that is d e s c so if i go to my instance here and uh maybe maybe we will just keep the limit as it is and i will do just i will put this one i will just put this one over here maybe let me just this one i think we have not added semicolon but that's fine because we didn't have to run this code so i can just mention right here that's it for now and now we have to order so how exactly i will do that so i will do demo glide dot order bean caps by now which field you want to order so maybe i will put number field in a string so i can put number and then i can run this code if i run this you can see that it is giving the output in order three four five seven eight now if i go back and if i want to do it in descending order how can i do that i will just go here order by and i will just mention d e s c that's it it will automatically sort the records in descending order with this particular field which you have mentioned as a parameter so i will just go here click on run and absolutely yeah records are not definitely same because you are just pulling the random brackets but you can still see that it is still in descending order so that's how you can do that you can still do it with the help of priority or any i think wherever you can sort it accordingly like a to z or z to a that's kind of a sort of sort you have that is something you can do that's that's how you you can do sword in service now whenever you do glide racket and you pull the records from any particular table the next method we have is update so as of now we were seeing the query of rackets so we were just pulling the rackets and we were just displaying the output we were not basically updating or inserting anything as of now but the next operation which you want to do is update off brackets and that you can do with the help of update method how exactly you can do that we will see it now so if i go here i just go back so this time what i will do i will just try to maybe uh just update short description maybe let's let's see the list i will just leave this page and if i go to the list now whatever five rackets we are getting let's let's update the short description on all of them whatever five rackets we are getting so in that case what we will do so maybe we will just add prefix we will not replace the short description we will just add the prefix so for that what we will do so you can just go here and mention so i will do here gr so we have to update short description so for that what you need to do you have to do demo glide not gr that's a different variable because our variable is demo glide so we will use demo glide you will do dot short description and then you will do equal to and whatever prefix you want to do maybe i will do a update method like this maybe i will just add like this and i will give it a space or maybe semicolon colon not semicolon and then we will do plus demo glide dot so in this way this will add the prefix and then i will do semicolon now so it will also print it if you if you want to print it so i will do like this demo glide dot you will just use this update if you will do this update it will automatically update that particular racket so in that case we have gs.print number is this problem i think this time we will not use this uh problem one maybe we will use um and sd that is short description is and we will see whether we are getting the right shot description or not and i will do short underscore description that's it and if i just go here so yeah i think it should work as expected uh let's not i think you can just still keep as it is like order by that's i think it won't hurt anything um add not null i think this is okay we will update the short description so let's run the script and see if it is working and i will just copy this so that we don't miss it if i run this let's see if it will absolutely so if i go here it is completed and you can see basically it is it is showing you for each racket and you can see this update right here in front of all the description short description we have and total number we have five brackets and if i go to the list i have and maybe i just try to search for this update and i enter you will see update yes you can see here we have the starts with update this is just because of this word over here but in real we got these rackets updated and you can see update keyword as well as a prefix in in short description but that's how you update the rackets now let me talk about one important uh one important method which is very useful if you are updating the rackets and that method is autosys fields now this particular field is basically this particular method is basically for not updating some of the system fields you have and what are those system fields which you get with every table like sys updated by cis updated on sys mod counts just created by ansys created on so for example you have multiple rackets and yours your organization i think already has a lot of data in incident table and you are maybe updating for example 50 000 rackets if you will do that if you will update it without using this autosys fields method that what it will do it will also update like these fields where we have like system updated assists updated on because but you don't want because you definitely want these dates should uh as it is in that case what you can do you can just use autosys fields false when you will do that it won't even touch these uh fields when you will update the rackets multiple rackets so that case if i just go here and and if i go back so for example i'm updating all those random brackets so what i can do i can just maybe i can just do demo glide dot and if i see it's auto auto says field it's fields and then we have false that's it this way this will not touch those fields when whenever you are updating multiple records single record as per your requirement so that's how you can use this particular method as well in your script whenever and i would recommend whenever you are updating the rackets you should use the this particular method because it's very i would say kind of mandatory because you don't want to touch these uh system fields right that's something you don't want so in that case you should definitely use this autosys fields method uh when you don't want to touch these system fields of a particular table and racket till now we learned about query of rackets and update off brackets but now we will learn how you can insert the racket in a table with the help of glide racket so we will start with the first method for creation of the racket that is initialize which basically creates an empty racket before record is created as per the table mentioned in the glide record object and that's what you use initialize and this will just create an empty racket but what method will insert the racket and that method is if i go next here that's insert method so as we had update method similarly we had insert as well to create to insert a new racket this will basically insert a new record using the field values that have been set for the current racket how will you create it let's see that so if i go to here and maybe i will just uh make all of them as comment because we have to write a different we will not query this time we're not going to query anything so that's the reason [Music] i'm just mentioning like this and i will send them at the bottom that's it so what i will do here i will start with so you definitely have to initialize glide record object so i will do there and this is demo uh demo creation that's a variable i'm just creating and i will do new and i will do glide and here i will do glide record and this time we will create the record in maybe incident table again that's fine we will use or you want to create some some other table because you must have got bored uh that we are keep on creating the records in incident table so let's create this record in any other table maybe um agile table that stories so we will create it in stories table here so as of now we have total seven records or maybe if i just click on all yep we have seven records and if i click on new because i'm just seeing the mandatory fields uh short description is mandatory only so we can create it so if i just go back here and i go to my script so what i will do and the and the table is adam underscore story and i will just put it here rm underscore story semicolon now here you will use that initialize so you will do demo creation dot initialize you're done that means you have initialized you have created that empty racket now as of now it's just empty but you have to set some fields you have to provide some data in the fields of that particular record which you want to create so in that case what you will do now you will just do demo creation dot short description that's a mandatory field and we will do equal to and we will do string this is a demo of insert rackets with glide racket semicolon so you have set the value and then i will do demo creation dot insert that's it just four line of code and this will insert a new racket so you can see we have used two methods which we just talked about talked about it insert and we have talked about initialize so both are important to create the record and if i run this code let's see the magic if i run this you can see yes it is created and if i go here and if i just refresh this we have eight records and our yes it is here the record which was created is this one and you can see the same short description which we tried to populate this is the demo of insert records with glide racket same one but that's how you create racket in service now with the help of glide racket with the help of script there is another great functionality which you can use with glide racket while creating a new record in a table and that is apply template so we have a method applied template where you have to provide the parameter as the name of the template in that case the record will be inserted as per the template you will mention and that's a great feature you don't have to provide any kind of field value in that particular initialization or whatever script we wrote so what you will do now if i go here and if i just come over here and i just see if i have to create a template let's create a template for this story and we will see whether we are able to create it or not so we will put the name as uh maybe demo underscore story creation that's just a demo i'm using um and then i can just put test or maybe i can use the same one uh if i go back here you will see the script we had and we will just copy this one that's it but we will mention with template just to make a difference so that you understand with template uh block this fall short description is okay we have mentioned okay this is i think the short description of uh story so that's fine you have mentioned over here so i can just um i can just mention the end and i can just put it like this maybe i will copy this one and i will just do this one that's it uh status draft that's fine uh do you want to put description let's put description as well so that at least you understand uh i will just do test for glide record and maybe let's select any other one um uh do we have type or let me let me check any other one we can select um let's put acceptance criteria that is text ac that is test uh test acceptance criteria and i can just click on submit that's it so we have created uh this template and uh the name of the template we have is demo story creation so i can just copy this now if you will go here and if i just go to the list of story we had right here we have eight now what it can do i can just remove this code this line of code here and what i will do demo creation dot and you have apply template and i will provide the name and the name is this one but i think that d was in lower case so i will do demo um i think that's it and let's try to insert it and see if you're able to get the all same kind of template while creating the racket so if i go here and click on run it says yes it has created and if i go here and refresh the list so yes answer is yes there is one more record if i go at the bottom it is created with the same template yes you can see here test for glide record this is the demo for insert record with the same template that's what we selected in the template test ac as well and that's it so that means this is how you can create records with a template so if you have different templates just create it and i think you should use it will be i think pretty much easy for you to create records via script that'll be very very very easy for you guys as a developer so that you don't have to write multiple field values to populate them in in the script so you can just use apply template user template and push the values while creating of the while creation of the rackets the next method we have is get now this will basically return basically one racket whatever as per the specification you will mention as part of the parameter so this will only return one record how can you do that let's take a look if i go here now here now first i will make all of them as a comment because i don't need them for now i'll just send them a little bit bottom what you will do as of now you have basically initiated the glide racket object for this story table so what i will do i will come here so you had this story i will just copy the sys id of this particular record we have copied it now when you query and you put the filter and all but if you have a society so what you can do you can just query the single record how can we do that you can just do here let's say demo creation dot get and then you can put society that's it and if you want to print the number of this one yes we can do that as well so if i do gs dot print and i put here the number is it would be same as it is and you have demo creation dot and i can just do number that's it and if i will run the code you can see we got the story number which is the same story well that's how you can use this get method to basically get the information of a particular racket which you know the society next method we have is get display value which we have already used and you have seen in the previous script which we have written so wherever you have recent reference fields if you want to get the display value then you can use this get display value method and the next one is again we are keep on using it the same method get row count which will retrieve the number of rows in that glide racket object so whatever rows it is returning you will get the row count with this method and then we have has next now this particular method is basically used to check if there are rackets in the table with mentioned filter do you have a single record if it's true or not that's something you can use with the help of has next the the basically the syntax is still the same the object dot has next if you will do that it will basically if if that is true then only it will take some action in the next step or next line of code that's how you can use has next next is delete rackets so as of now we talked about query of the rackets in a table we talked about update of the rackets in a table we talked about creation of record insertion of rackets in a table now let's learn about delete off brackets deletion of rackets in a table now delete racket will basically delete a single racket how can we do this i will show you right here so we had this particular racket so let's say we will just make a filter uh maybe i will delete this one so i will just do i will just do show matching so if i go a little bit up and if i get this one and if i just get the encoded query i will just copy the query i will go to my script here i will delete it this time but i think we still need to use till this here so that's fine i can just do demo creation dot add encoded query and i can put the query here which you want to delete add encoded query so syntax is still wrong you can see now it is correct add encoded query and i will do demo creation dot query because as of now we just put the filter now we are acquiring the racket or records as per the filter it will run because query doesn't know whether as of now it is one record or multiple records and then i will do demo creation dot delete racket that's it will it delete it answer is yes so if i go at the bottom if i run this yes it ran did it really ran did it really delete the record we had if i go here we have this one record and if i refresh it's gone number is one zero zero that's what we had let me check again yep delete record our script is still defined delete record creation delete single racket and table uh i'm doing the query uh where demo new live record story uh add encoded query this is also fine uh and if i go here i think it should delete it let me check it again it is not uh queries and okay that's that's the reason it is not doing it the reason behind it because we have not moved to the next record so for that i will do dot next here that's it this time it will for sure run and if i go here run the script yes it did could not find the field that's fine if i go here and i refresh it you will not find that incident anymore you had nine in nine story not incident story and now you have eight that means it deleted that particular story that means our script ran successfully now we have another method as well that is delete multiple so this particular method will delete single racket the one which we just used delete racket but if you want to delete multiple rackets then then you can use delete multiple delete multiple rackets from a table and if i go here so for example we have to delete two rackets so let's say maybe i will just do let me check if i can just try to use this scrum word so wherever we have scrum word we will delete it so if i have the short description contains scrum word and if i run this i'm getting three records and if i copy this go to my script now and i put it in the encoded query next next yeah okay in that case if i do demo.create and this time i will do delete multiple will it run if i'm just using like this let's see that that's something we will check whether we are getting the right right result or not uh and i will just do demo creation delete multiple and let's run this if i'm running this it's done as of now and i'm getting three alerts over here it's just because of some maybe related tasks or something related racket that's the reason it is showing you this particular record it says delete delete item story three yes it has deleted and if i come here and if i refresh do you think it will show the magic i think it will show it and if i refresh the list absolutely you can see that it has deleted all those three rackets now we only have five rackets in this table and that's how you delete multiple rackets but i just want to remind you one thing and you have to make a note here that if you are basically selecting this delete method you have to be very careful because this can delete any data maybe it can delete any any data which you really want so you have to be very careful while writing the script what exactly you're doing and first of all as i mentioned in the beginning do not run any of the script especially this delete one in production directly without testing it multiple times in lower instance then only you should run in production environment ideally if i talk about as a general practice in servicenow you should not delete racket you should just disable them but even if you want it you can you can do this with the help of this script but you have to be very careful so this is what we learned in this session about glide racket and glide racket methods that how you can do database operations in service now if you want to do a practice i would recommend you you can also try these scripts in business rules as well because as of now i just showed them in background script but if you really want to check them how it is updating the racket because if you will use business rules and you want to update the current record you will definitely have to use like current object and then you can update the racket so in that case you can use glide racket and then use them in different scripts different server side scripts you have one thing i can tell you that you can also write glide racket in client script as well it will definitely run it but that is definitely not recommendable it's a worst script i would say so do not run glide racket in client script because that's not the way because else you will definitely impact the performance of your system so i hope you liked today's session so please continuously do multiple practice uh different scenarios so that you can learn this scripting very well so i hope you like my session please like comment on my videos provide any feedback and do not forget to subscribe to my channel and also share my channel and videos with your colleagues and friends thank you and have a great day

View original source

https://www.youtube.com/watch?v=-GPjps0r0KI