How to transfer a file to the MID server

Jean-Fran_ois A
Tera Expert

Hi,

CSV files are created when a certain event happens. The CSV files are created within a business rule ans should be sent to the MID server in order to forwarded to another server via SCP. What's the best way to send the CSV files to the MID server?

 

thanks,

1 ACCEPTED SOLUTION

simonezini
Mega Sage

Hi @Jean-Fran_ois A 

I think this script should help you. It should create a CSV file (just edit the text variable with the content from your CSV file) and using ecc_agent_attachment and ecc_queue tables it will save that file in the /agent/export folder of your ServiceNow installation on the MID Server.

 

var d = new Date();
var filename = 'DummyFilename_' + d.getTime().toString() + '.csv';
var text = 'Column_1;Column_2;Column_3\n';
text += 'Lorem;ipsum;dolor\n';
text += 'sit;amet;consectetur';

var midServerAttachGR = new GlideRecord('ecc_agent_attachment');
midServerAttachGR.initialize();
midServerAttachGR.name = filename;
midServerAttachGR.short_description = filename;
var new_table_sys_id = midServerAttachGR.insert();

//Create file
var sa = new Attachment();
var attachmentId = sa.write('ecc_agent_attachment', new_table_sys_id, filename, 'text/csv', text);

var gr = new GlideRecord('sys_attachment');
gr.addQuery('table_sys_id', new_table_sys_id);
gr.query();
if (gr.next()) {
    var ecc_queue = new GlideRecord("ecc_queue");
    ecc_queue.initialize();
    ecc_queue.agent = "mid.server.XXXXX"; //Put here your MID Server agent service name
    ecc_queue.name = filename;
    ecc_queue.queue = "output";
    ecc_queue.topic = "StreamPipeline";

    var xml = '<?xml version="1.0" encoding="UTF-8"?>';
    xml += '<parameters>';
    xml += '<parameter name="stream_relay_response_topic" value="ExportSetResult"/>';
    xml += '<stream_relay_source attachment_sys_id="' + gr.sys_id + '" type="AttachmentSource"/>';
    xml += '<stream_relay_transform attachment.table_sys_id="' + gr.table_sys_id + '" order="0" stream_relay_transfer_progress_interval="150" type="AttachmentProgressTransformer"/>';
    xml += '<stream_relay_sink path="/' + filename + '" type="FileSink"/>';
    xml += '</parameters>';

    ecc_queue.payload = xml;
    //Get SysID of new ECC Queue record
    var ecc_sysid = ecc_queue.insert();
}

 

Hope this helps.

 

Regards,

Simone

View solution in original post

5 REPLIES 5

jonsan09
Giga Sage
Giga Sage

You could try setting up an Export Set: https://www.youtube.com/watch?v=XV_C5OrHzrk

In this video, we'll cover export sets. Export sets give us a way to automatically export a set of records to a remote system via a MID server. Links * Docs: Export sets (Rome) - https://docs.servicenow.com/bundle/rome-platform-administration/page/administer/export-sets/concept/c_ExportSets.html *

I had a look at export sets but that won't work since there is a fair amount of logic that needs to occur for putting the CSV files together. 

I had a look at Export sets and they would work if the data that goes into my CSV files would come from a table. In my case, there is some logic which has been implemented in the business rule to create or adapt the data in the CSV files.

 

Any other possible solutions?

Jean-Fran_ois A
Tera Expert

Any suggestion?