Script for Pushing Files to Mid-Server - ServiceNow


Here is the script to Push the file to
 ServiceNow. (Example: .txt)
The example below demonstrates the usage of the script in a Flow Designer -> Action.


Flow Action Script:

(function execute(inputs, outputs) {
outputs.ecc_agent_attachment_created_sys_id = '';
outputs.ecc_queue_created_sys_id = '';
/* Sending file to MID SERVER starts here */
function exportToMid(attachment_name, mid_name) {
var fileDropResult = {};
// Create ECC attachment record used to send the file to the mid server
var ecc_att = new GlideRecord('ecc_agent_attachment');
ecc_att.initialize();
ecc_att.name = attachment_name;
fileDropResult.ecc_agent_attachment_created_sys_id = ecc_att.insert();
//Copy single attachment custoom script
var gAtach = new GlideRecord('sys_attachment');
gAtach.addQuery('table_name', 'sys_properties');
gAtach.addQuery('file_name', attachment_name);
gAtach.orderByDesc('sys_created_on');
gAtach.setLimit(1);
gAtach.query();
if(gAtach.next()) {
var targetRef = new GlideRecord('ecc_agent_attachment');
targetRef.get(ecc_att.sys_id);
var glideSysAttachmentRef = new GlideSysAttachment();
var guid = "";
var newFileName = gAtach.getValue("file_name");
guid = glideSysAttachmentRef.writeContentStream(targetRef, newFileName, gAtach.getValue("content_type"), glideSysAttachmentRef.getContentStream(gAtach.getUniqueValue()));
}
// Get the sys_id of the exact attachment file to be exported and used in ECC Payload
var at = new GlideRecord('sys_attachment');
at.addQuery('file_name', attachment_name);
at.addQuery('table_name', 'ecc_agent_attachment');
at.addQuery('table_sys_id', ecc_att.sys_id);
gAtach.orderByDesc('sys_created_on');
gAtach.setLimit(1);
at.query();
at.next();
// Create XML for ECC Payload
var xmlString = '<?xml version="1.0" encoding="UTF-8"?>' +
'<parameters>' +
'<parameter name=\"stream_relay_response_topic\" value=\"ExportSetResult\"/>' +
'<stream_relay_source attachment_sys_id=\"' + at.sys_id + '\" type=\"AttachmentSource\"/>' +
'<stream_relay_transform attachment.table_sys_id=\"' + ecc_att.sys_id + '\" order=\"0\" stream_relay_transfer_progress_interval=\"150\" type=\"AttachmentProgressTransformer\"/>' +
'<stream_relay_sink path="\/' + at.getDisplayValue('file_name') + '\" type=\"FileSink\"/>' +
'</parameters>';
// Push file to the Mid-server - Create ECC Record
var eccQueue = new GlideRecord('ecc_queue');
eccQueue.initialize();
eccQueue.agent = 'mid.server.' + mid_name;
eccQueue.topic = 'StreamPipeline';
eccQueue.queue = 'output';
eccQueue.state = 'ready';
eccQueue.payload = xmlString;
eccQueue.name = 'TXT-FILE-Export'; //GiveYourOwnName
eccQueue.source = attachment_name;
fileDropResult.ecc_queue_created_sys_id = eccQueue.insert();
return fileDropResult;
}
var mid_name = gs.getProperty('MidServerName'); //Mid-serverName
var attachment_name = 'TextFileOutPut.txt'; //File name - HARDCODED
var exportToMidServer = exportToMid(attachment_name, mid_name);
if(exportToMidServer && exportToMidServer.ecc_queue_created_sys_id !== undefined && exportToMidServer.ecc_agent_attachment_created_sys_id !== undefined ) {
outputs.ecc_queue_created_sys_id = exportToMidServer.ecc_queue_created_sys_id;
outputs.ecc_agent_attachment_created_sys_id = exportToMidServer.ecc_agent_attachment_created_sys_id;
}
/* Sending file to MID SERVER ends here */
})(inputs, outputs);