Script to export data in csv

koley
Kilo Explorer

Hi All,

I want to generate a report.

below is the requirement:

1) Query multiple tables.

2) Format the data using script.

3) Export

I want to export this in csv.

Is it possible?

I am using Schedule job. I am able to get and format the data. But how to export in csv ?

Thanks,

MK

7 REPLIES 7

tony_barratt
ServiceNow Employee
ServiceNow Employee








5 Exporting Directly from the URL

You may want to export data from the URL if you need to dynamically export data from a script or web service. You must be familiar with the ServiceNow table and column names to export data directly from the URL. See Navigating by URL for more information about navigating to forms and lists.


To export data directly from the URL, create a URL containing the following parts:


  1. Specify the instance URL. For example, https://<instance name>.service-now.com/.
  2. Specify the table form or list to export. For example, incident_list.do.
  3. Specify the export format processor to use for the export. For example, ?CSV.
  4. [Optional] Specify a query and sort order with URL parameters. For example, &sysparm_query=sys_id%3E%3Db4aedb520a0a0b1001af10e278657d27.


Any thoughts on the above?



Best Regards



Tony



If you have a very long URL with filter criteria in it. Its impossible to export directly from URL. I also tried shortening the URL with tinyURL, it can't do it since its several thousand characters long. What alternative do you provide in this situation?



Can we somehow directly get hold of the resultset from the report and run a script against it to export a csv file?


mfamphlett
Mega Expert

Hi,



It's probably a bit late for you but just thought i'd post here to help others looking for a solution to this. Since I came across this post whilst trying to work it out.



(function() {


  // create mail (don't need to do this but was just part of my requirement so thought i'd include it)


  var mail = new GlideEmailOutbound();


  mail.addRecipient('user@domain.com');


  mail.setSubject('Test CSV attachment');


  mail.setBody('Test');


  mail.save();


  // get the mail gliderecord (this can be any gliderecord you want to attach your csv to, doesn't have to be an email)


  var mailGr = new GlideRecord('sys_email');


  mailGr.get(mail.sysID);


  // create the attachment


  var csvData = '"Header 1","Header 2"\r\n"Data 1","Data 2"\r\n';


  var sa = new GlideSysAttachment();


  // this function has the following required parameters (GlideRecord to attach file to, filename, content type, file data)


  // it will return the sysId of the sys_attachment record


  return sa.write(mailGr, 'test.csv', 'application/csv', csvData);


})();


Hi Matt,



I need to export some asset data (alm_hardware) in CSV format within workflow and attach the CSV files   in the email.


your code meant to be a business rule, if so which table?



I will appreciate your help



Thanks


Ak