
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-23-2020 02:15 AM
Hi Team,
Need to exort CSV file without headers in exported sheet,
Please help!
Solved! Go to Solution.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-23-2020 06:32 PM
Following Scripted REST API will export all incident records.
It'll create an csv as an attachment to a record and the send it back.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var tmpTableName = 'incident'; // name of table to create attachment
var number = 'INC0000055'; // record number to create attachment
var exportFileName = 'example.csv'; // name of temporary attachment file
var contentType = 'text/csv';
var exportTableName = 'incident';
var columnNames = ['sys_id', 'number', 'assigned_to.name']; // names of columns to export
try {
// insert attachment
var attachment = new GlideSysAttachment();
var grTable = new GlideRecord(tmpTableName);
if (grTable.get('number', number)) {
var attachmentSysId = grTable.sys_id;
var rec = new GlideRecord(tmpTableName);
rec.get(attachmentSysId);
var content = getRecords();
var agr = attachment.write(rec, exportFileName, contentType, content);
// get attachment and send back to user
var grAttachRead = new GlideRecord('sys_attachment');
grAttachRead.addQuery('table_sys_id', grTable.sys_id);
grAttachRead.addQuery('file_name', exportFileName);
grAttachRead.query();
if (grAttachRead.hasNext()) {
var count = 0;
var hdrs = {},
attachment_sys_id = exportFileName;
hdrs['Content-Type'] = contentType;
response.setStatus(200);
response.setHeaders(hdrs);
if (grAttachRead.next()) {
var writer = response.getStreamWriter();
var attachmentStream = new GlideSysAttachmentInputStream(grAttachRead.sys_id);
writer.writeStream(attachmentStream);
}
}
// delete temporary attachment file from record
var grAttachDelete = new GlideSysAttachment();
grAttachDelete.deleteAttachment(grAttachRead.sys_id);
}
} catch (e) {
gs.error("ERROR=", e);
return {
'error': e.message
};
}
// function to return records to export in csv
function getRecords() {
var records = [];
var grInc = new GlideRecord(exportTableName);
grInc.query();
while (grInc.next()) {
var columnValue = [];
columnNames.forEach(function(name) {
columnValue.push(grInc.getElement(name).toString());
});
records.push(columnValue.join(','));
}
return records.join('\n');
}
})(request, response);
curl script sample
curl "https://<service instance name>.service-now.com/api/<servicenow instance number>/exportcsv" \
--request GET \
--header "Accept:text/csv" \
--user '<user name>':'<password>' \
--output <csv file name>.csv

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-23-2020 03:48 AM
Hi
Could you please help here regarding scripted REST API for this CSV export?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-23-2020 06:32 PM
Following Scripted REST API will export all incident records.
It'll create an csv as an attachment to a record and the send it back.
(function process( /*RESTAPIRequest*/ request, /*RESTAPIResponse*/ response) {
var tmpTableName = 'incident'; // name of table to create attachment
var number = 'INC0000055'; // record number to create attachment
var exportFileName = 'example.csv'; // name of temporary attachment file
var contentType = 'text/csv';
var exportTableName = 'incident';
var columnNames = ['sys_id', 'number', 'assigned_to.name']; // names of columns to export
try {
// insert attachment
var attachment = new GlideSysAttachment();
var grTable = new GlideRecord(tmpTableName);
if (grTable.get('number', number)) {
var attachmentSysId = grTable.sys_id;
var rec = new GlideRecord(tmpTableName);
rec.get(attachmentSysId);
var content = getRecords();
var agr = attachment.write(rec, exportFileName, contentType, content);
// get attachment and send back to user
var grAttachRead = new GlideRecord('sys_attachment');
grAttachRead.addQuery('table_sys_id', grTable.sys_id);
grAttachRead.addQuery('file_name', exportFileName);
grAttachRead.query();
if (grAttachRead.hasNext()) {
var count = 0;
var hdrs = {},
attachment_sys_id = exportFileName;
hdrs['Content-Type'] = contentType;
response.setStatus(200);
response.setHeaders(hdrs);
if (grAttachRead.next()) {
var writer = response.getStreamWriter();
var attachmentStream = new GlideSysAttachmentInputStream(grAttachRead.sys_id);
writer.writeStream(attachmentStream);
}
}
// delete temporary attachment file from record
var grAttachDelete = new GlideSysAttachment();
grAttachDelete.deleteAttachment(grAttachRead.sys_id);
}
} catch (e) {
gs.error("ERROR=", e);
return {
'error': e.message
};
}
// function to return records to export in csv
function getRecords() {
var records = [];
var grInc = new GlideRecord(exportTableName);
grInc.query();
while (grInc.next()) {
var columnValue = [];
columnNames.forEach(function(name) {
columnValue.push(grInc.getElement(name).toString());
});
records.push(columnValue.join(','));
}
return records.join('\n');
}
})(request, response);
curl script sample
curl "https://<service instance name>.service-now.com/api/<servicenow instance number>/exportcsv" \
--request GET \
--header "Accept:text/csv" \
--user '<user name>':'<password>' \
--output <csv file name>.csv

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2020 06:09 AM
Thank you so much
File is getting exported, i am facing issue just with CSV format.
Not able to execute the curl command.
Could you please help here?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2020 01:35 PM
MS Windows has a bad implementation of curl command that actually doesn't work too well. If running on MS Windows, it's better to use either Powershell or JavaScript.
Source to call the REST can be generated from REST API Explorer. Just click the corresponding link under the Code Sample and update the username and password.
Information on using ServiceNow REST API Explorer is provided in the following pages.

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-24-2020 11:51 PM
Hi
Thank you.
Can I use the above script for exporting csv using export set as well.
The scenario is to export csv without header and as semicolon separated at MID server shared drive