How to download csv file using Script without using URL ?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-04-2022 03:00 AM
We would like to create a Ui Action on the Database View list screen so that a Csv file can be downloaded when the button is pressed.
When the UI Action button is pressed, the Script Include will be called, and the file can be downloaded according to the code in the Script field of the Script Include.
I was thinking of using the code in the following image as a reference, but the code in this image attaches the file to the record at the end.
I did not attach the file, but downloaded it as is.
If possible, I would like to use this code as it is and replace the attached part with a direct download code.
Any help you can give me would be greatly appreciated.
Thank you in advance.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-06-2022 08:41 PM
Hi,
I currently don't have any sample code.
But you can try to search in community or other blogs.
Ankur
✨ Certified Technical Architect || ✨ 9x ServiceNow MVP || ✨ ServiceNow Community Leader
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎10-06-2022 11:34 PM
Hello @Yuya2009
Please check if the below code helps you. Although the code will still attach an attachment to the record. So may be you can create a Property and attach the attachment to this property record. Every time when you click on the Download file button the previous attachments will be deleted and a new Attachement will be attached. This new attachment will be auto downloaded:
UI Action:
Client: checked
Onclick: downloadAll()
List banner button: checked
function downloadAll() {
var ga = new GlideAjax('CustomIncidentUtilsV1');
ga.addParam('sysparm_name', 'getIncidentData');
ga.getXMLAnswer(parseReceivedData);
}
function parseReceivedData(answer) {
if (answer) {
var url = "/sys_attachment.do?sys_id=" + answer;
g_navigation.open(url, "");
} else {
alert("No File to Download");
}
}
Script include where client callable is checked:
var CustomIncidentUtilsV1 = Class.create();
CustomIncidentUtilsV1.prototype = Object.extendsObject(AbstractAjaxProcessor, {
getIncidentData: function() {
var fileHeaders = ["Number", "Caller", "Short Description", "Assignment Group", "Assigned to"];
var fileDataArray = [];
fileDataArray.push(fileHeaders.join(","));
var incidentRecordGR = new GlideRecord("incident");
incidentRecordGR.addActiveQuery();
incidentRecordGR.query();
while (incidentRecordGR.next()) {
var fieldData = [];
fieldData.push(incidentRecordGR.getValue("number"));
fieldData.push(incidentRecordGR.getDisplayValue("caller_id"));
fieldData.push(incidentRecordGR.getValue("short_description"));
fieldData.push(incidentRecordGR.getDisplayValue("assignment_group"));
fieldData.push(incidentRecordGR.getDisplayValue("assigned_to"));
fileDataArray.push(fieldData.join(","));
}
var attachment = new GlideSysAttachment();
var agr = attachment.getAttachments("sys_properties", "217e8ac3979211104cadfb000153af45");
while (agr.next()) {
agr.deleteRecord();
}
var propertyGR = new GlideRecord('sys_properties');
if (propertyGR.get("217e8ac3979211104cadfb000153af45")) {
var grAttachment = new GlideSysAttachment();
var attachmentID = grAttachment.write(propertyGR, "Incidents.csv", "CSV", fileDataArray.join("\n"));
return attachmentID;
}
return "";
},
type: 'CustomIncidentUtilsV1'
});