Extract data from ServiceNow to a .CSV file and put the file on a file server?

rody-bjerke
Giga Guru

Hi,

What's the best way to extract data from ServiceNow to a .CSV file and put the file on a file server at the customer location.

Will export sets do the job?

Best regards,

10 REPLIES 10

Hi Ankur,



How would a post script look like if you want to move the .csv file from the current location to for example:   E:\files\exports\*



Best regards,


Hi Rody,



So you want to move the file from one location of mid server to another location in mid sever.?


Mid server script include runs java code so you need to include java packages in initialize() method of script include and write code similar to java to move file from one location to another.


I have provided sample java code below which is working fine and moving files from source folder to destination folder. you need to replicate the same in mid server script include by including proper package call.



http://www.john-james-andersen.com/blog/service-now/javascriptprobe-and-mid-server-script-includes.h...



Sample Java Code:


File sourceFolder = new File("C:/Mid Servers/mid.helsinki-GE/agent/export/SourceFolder");


File destinationFolder = new File("C:/Mid Servers/mid.helsinki-GE/agent/export/DestinationFolder");




    // Check weather source exists and it is folder.


if(sourceFolder.list().length > 0){


System.out.println("Folder is not empty");


    if (sourceFolder.exists() && sourceFolder.isDirectory())


    {


            // Get list of the files and iterate over them


            File[] listOfFiles = sourceFolder.listFiles();



            if (listOfFiles != null)


            {


                    for (File child : listOfFiles )


                    {


                            // Move files to destination folder


                      System.out.println("FileName is:"+child.getName());


                            child.renameTo(new File(destinationFolder + "\\" + child.getName()));


                    }


                    // Add if you want to delete the source folder


                    //sourceFolder.delete();


            }


    }


    else


    {


            System.out.println(sourceFolder + "   Folder does not exists");


    }



}


else{


System.out.println("Folder is empty");


}



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Rody,



Also the sample code provided above should be written in mid server script include function and this function should be invoked via creating an entry in ecc_queue table.



Sample Post Script:


var jspr = new JavascriptProbe('mid server name');


jspr.setName('My Test Message'); //Any descriptive name will do


jspr.setJavascript('var parserObj = new FileUtils(); parserObj.moveFile();');


jspr.create();



the moveFile() method inside mid server script include FileUtils will have the sample java code provided earlier



Mark Correct if this solves your issue and also hit Like and Helpful if you find my response worthy based on the impact.


Thanks


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Hi Rody,



Any update on this?


Can you mark my answer as correct, helpful and hit like if you were able to achieve the requirement. This helps in removing this question from unanswered list and helps users to learn from your thread. Thanks in advance.



Regards


Ankur


Regards,
Ankur
✨ Certified Technical Architect  ||  ✨ 9x ServiceNow MVP  ||  ✨ ServiceNow Community Leader

Dave Smith1
ServiceNow Employee
ServiceNow Employee

Rody Bjerke wrote:



Hi,



What's the best way to extract data from ServiceNow to a .CSV file and put the file on a file server at the customer location.


Will export sets do the job?



Best regards,


Yes, export sets will.



But I'd first question: what is it you wish to achieve - and is extracting data as CSV files the best approach?



Most people that ask about CSV extraction then go onto talk about importing that data into another system.   If your end objective is connecting a third party to the platform to migrate datasets, you may want to consider XML or REST API feeds.