logo

NJP

How to Generate CSV Files Effortlessly with ServiceNow Scripts : ServiceNow Scripting Usecase

Import · Feb 04, 2023 · video

[Music] hi all and welcome back so in today's tutorial we will be trying something different okay so in today's tutorial we will see how we can generate CSV okay so actually I was exploring around service now and this question bring in my mind that how the CSU gets generated okay can we generate the CSV on our own so let's see how we can generate the CSV and then that PSP will be attaching it uh to some incident or any other record okay let's work on the incident table only so before that let's jump on to what we have to write let's let's write a quick script for the same okay I'll go to fix create and we will give it a name as so we'll generate a CSV for all the incidents which are in active State okay I think that is that is a good example okay so let's give it a name like incident [Applause] and red and see that PS3 and we'll get started so first of the foremost thing is like whenever we want to create a CSV we have to Define what are the rows we want to have okay so uh basically rows will visit the columns uh sorry columns we have to Define so what all columns we want so we will continue with couple of columns okay and then based on your requirement if you have such kind of requirement then you can extend it out the way you want so for example what I will do I will start with valuable headers and let's give couple of field names okay so it's not necessary that you have to give the backend name only because you are generating a file so let's give some of them so we will give giving number then what is we can give caller whatever is coming in my mind I'm just giving it out then short description description then what else a seven group and the last one we will give assigned to I think this is the coin for assigned to so these are the headers so this this is the information which you we are trying to extract today now the second step is likely we have to give the file name because the file will get generated what should be the name of the file you can keep it diameteric also if you have a use case but now we are creating a static file one file only right so we will get a file name variable file and we'll give it as return dot CSV and we have to give it into inverted commas so we have given the file name now what next now we have to create us we will be creating what is the CSU data so first of all we will be creating a string and then we will be converting that string into attachment okay so let's create a string so we will give variable PSP data equal to and in the start it will be empty string only right so this is done now we will create the headers for for our file okay we will just fix the headers and then we will start populating the data for the same so how we can do that we can simply do it by headers right it does dot for for it and your function so what I'm doing I'm just iterating through that uh headers okay what you can see number color short description and so on okay inside this we will give CSU data equal to CSU data so what I'm doing I'm just appending to the headers okay that's why I'm giving like this yes Plus in single inverted comma you have to give double inverted comma reason for this is like it's it's it's a string we are defining right okay so we uh we are putting all the operation in single inverted comma and double inverted comma is a part will be a part of string don't get confused into this plus ple Plus same single aortic comma horrible denoted comma we will delete one okay it will create one and then at the last we have to give the comma because all the columns will be separated by commonly right so in Singularity comma we'll give comma so this is how the headers can get prepared so we are done with the headers we can think we are done with the headers what exactly is the error which is semicolon that gives the semicolon so using this we have created the headers okay we have extracted the headers on the CSO data now we want active incidence as a part of this right so let's extract that to instead of variable PR incident equal to new Glide record on which table is incident sorry my word present and then we want the high incident we want only act two incidence so we'll add active query incident query now what we have to do we have created a column headers right CSV headers now we have to start populating the data in the same okay how we can do that so it will be while CR dot next yeah it's written dot next one thing I missed over here okay I just remember that after this we want a line break also right okay because we want the data to be populated on the next line next line the same way so what we can do for that we can do CSU data equal to CSU data Plus we have to give backslash R backslash n so that the date then next data will come into the next line okay it's a line break right okay so while gr dot next what we have to do we have to iterate through the data and just append it to the CSV data string so we can do CSV data equal to now this is a hectic part okay I don't like it so we first we will first took the data which we have created till now the string the header string Plus now again the same thing in single in order comma you have to add double inverter I will just zoom out so it will be pretty clear okay so we have to assign it for each and every phrase Plus we have to do foreign DOT number and you have to go with the same sequence right okay what's there at the top and then it should be closed with the same so it will Plus signal inverted comma this time it should have comma also right okay because two elements get separated by comma itself so this is done for the first film in the similar way we have to do for all the fields okay which are there on the form so how many fields we have one two three four five so let's do it the five times foreign time is done to plus I will add it to the next line only so that I will get idea if I'm messing something out because this is Plus it will be tap tap tap tap so I'm just making it just to give some readability to the code as people will not understand right what exactly I was writing and it also signifies that I'm writing the same thing again and again right so let's start filling it out so the number is done now what we have to do the next one is caller right so we have want to display value for the same so what how we can do give it we can hear the screw followed underscode ID Dot get press play value why get display value okay why display value we want to use because of how an external will understand right what is the society for so let's give a display value for the same so this particular attribute is done then next one is short description let's go with the short description now the backend name for short description chart third description now here you have to use the backend name okay don't think in the terms of the headers only here you have to give the backend name also only what is all so short description is done then what's next so after short description we have assignment group so for that we have to give fine man underscore group Dot okay and the last one is assigned to I will just copy this out and just change this this is the wrong way Let's test it out and for assigned to it's just we have to change group to do I think I'm done over here and for the last one as you know in the Json also for the last one we don't require comma so I just removed it out give it seven column to the same and then what this error is all about let's check this out first so this particular surgery was started and over you this is over here what others are doing you don't need you guys so we will be following the same thing again I will just copy paste it out after every iteration after every row we want a line break Ctrl a shift tab and once this is done the CSV data got created now now the next step is like we have to attach it out to certain incident for example so let's do that for that it's pretty easy right variable Dr record I will just give because I have already used CR incident at the top equal to new Glide record let's attach it to any incident and let's attach it to any incident so I will just go here I will just repeat that dot net cancel or save it out I don't want to lose my things now okay so we will do okay then dot list and let's take zero zero one one this particular instance so let's attach there is no attachment to this particular incident let's copy this ID for the same okay I go back over here I can put this time first of all I will spawn the screen and here I can do it VR record Dot get what you want to get the record with us is I did this and then if the record exists right we want to create an attachment so let's create an attachment attach it the same so let's do that so it will be variable how to create an attachment attach it to the record okay so there is a EPA for it which is blood system attachment okay so we'll be using that okay GR ouch man equal to new light this this attachment okay and now from this particular API we will be using function called Write what this will do it will create an attachment and attached to a specific record so what we have to do is attachment Dot right and now it asks for four attributes okay first is the the record on which you want to attach so it will be it will be gr record so the first thing you started the second thing is the file name okay what should be the file name of the created record so we have already created an attribute called file name so we'll be giving that the third attribute is like what kind of what kind of file this is so it's equation slash CSE because it's a CSV file and the fourth attribute is like the string the data string which you have created which is PS3 and done so what we did over here we have created first we have defined the headers first okay we have to find the file name we have defined the CSU data which is empty then we created headers then we right I'll Glide record the incident table uh add a query of active query okay look through that record okay and just create this creators string CSS string and then we are attaching it to the sit creating an attachment and attaching it to the record using this attachment let's attachment API I'll save it out and now let's run so five run this out frosted that I API got executed let's see if any incident was created in the record oh we got this incident.csv file right perfect this is what we were expecting now what we want we will go to the downloads let's download this out and see whether we got the data or not for the same so I will open the file and here you can see we got the data with the headers called number caller short description assignment to a seven group okay these are the headers and these are the records for the same assigned to came undefined why let's look into that maybe I made a mistake SSI G assigned to a it's not assignment to assigned to to get this level okay this was a minor grammatical mistake I can say okay so this is all for today's tutorial in today's tutorial we have seen a script to generate CSU how we can generate CSV using script this is what just an OV how we can do it out based on your use case you can redefine the script out okay you can modify the script out and use it accordingly one of the use case which I can see is like you wherever you want to generate you have to send a notification to someone okay by generating CSV so you will write this script which will generate the CSV and on the fly it will not get attached to the notification and you can send the notification with all this information so hope you like this video uh if you like this video please subscribe to my channel so that you will get you will be notified for future coming videos till then stay safe stay happy and have a nice day thank you

View original source

https://www.youtube.com/watch?v=7Mo2YgJVxAU