
- 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 02:28 AM
Is this for normal csv export of table data or for Export set functionality?
Regards
Ankur
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-23-2020 03:47 AM
Hi Ankur,
It is normal CSV export
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎09-18-2021 07:06 PM

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎11-23-2020 02:32 AM
<instance name>.do?CSV would export the header also.
Generating a csv without a header is possible by writing a Scripted REST API.