- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-10-2017 06:26 PM
Hi Guys,
Does anybody know how to send a file to MID Server via a script? The original requirement was to truncate the header row of the CSV file before the file is sent via an export set. We were able to remove the first row and have it attached either to the original ecc_agent_attachment or to a new record. Either way, the file is not being sent to MID Server. It seems that files will only be passed to MID server when they are triggered from an actual export set not when a record is updated/created in the export set table (ecc_agent_attachment) via a script. So, we are stuck in a situation where we have the file in a proper format the client wanted to have but we couldn't find a way of sending it to MID server.
Here is what the code looks like:
var data_array = [];
var table_sys_id = current.sys_id;
var attachmentGR = new GlideRecord("sys_attachment");
attachmentGR.addQuery('table_name', 'ecc_agent_attachment');
attachmentGR.addQuery('content_type', 'test/csv');
attachmentGR.addQuery('file_name', 'STARTSWITH', '1');
attachmentGR.addQuery('table_sys_id', table_sys_id);
attachmentGR.query();
if(attachmentGR.next()){
var stu = new GlideStringUtil();
var gsa = new GlideSysAttachment();
//Get the raw bytes in the file
var bytesInFile = gsa.getBytes(attachmentGR);
var dataAsString = Packages.java.lang.String(bytesInFile);
dataAsString = String(dataAsString);
data_array = dataAsString.split("\n");
var newArray =[];
for(var i = 1; i<data_array.length-1; i++) {
newArray.push(data_array[i]+ "\n" + "*");
}
var strArr = String(newArray);
gs.print("StringData1 - "+strArr );
strArr = strArr.replaceAll("*," , '').replace("*",'');
gs.print("StringData2 - "+strArr );
var stringUtil = new GlideStringUtil();
//var base64EncodeString = stringUtil.base64Encode(strArr);
//create a new record and attach the new file
var midServerAttachGR = new GlideRecord('ecc_agent_attachment');
midServerAttachGR.initialize();
midServerAttachGR.name = 'Export Set Attachment';
midServerAttachGR.short_description = 'Updated attachment';
midServerAttachGR.source = 'a6d9e79f4f0376007861a90f0310c772';
var new_table_sys_id = midServerAttachGR.insert();
var attachment = new Attachment();
var attachment_sys_id = attachment.write("ecc_agent_attachment", new_table_sys_id, "22222222.csv", "csv", strArr);
}
But even create a new ecc_agent_attachment record doesn't trigger the file to be sent to the MID server.
Regards,
Mussie
Solved! Go to Solution.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-11-2017 06:44 PM
I was able to resolve this by leveraging the MID Server Script Files module.
Here is the steps I followed:
Go to MID Server —> MID Server Script Files
Create New
Give it any name you like and click save
Then on my BR code, once I have removed the header row stripped from the attachment, I directly write the contents of the attachment on the script. This will automatically send the file to MID server, however the files will be stored on agent/scripts.
Here is what the BR code looks like:
Table - ecc_agent_attachment
When - async
Runs - on Insert
condition - current.source == gs.getProperty('pp_exportset_sysid')
(function executeRule(current, previous /*null when async*/) {
gs.sleep(10000); //wait for the related attachment to be created
var data_array = [];
var table_sys_id = current.sys_id;
//get the details of the attachment created via the export set
var attachmentGR = new GlideRecord("sys_attachment");
attachmentGR.addQuery('table_name', 'ecc_agent_attachment');
attachmentGR.addQuery('content_type', 'test/csv');
attachmentGR.addQuery('file_name', 'parking_permit_raw.csv');
attachmentGR.addQuery('table_sys_id', table_sys_id);
attachmentGR.query();
if(attachmentGR.next()){
gs.log('file name is ' + attachmentGR.file_name);
//if available, strip off the header row from the csv file
var stu = new GlideStringUtil();
var gsa = new GlideSysAttachment();
//Get the raw bytes in the file
var bytesInFile = gsa.getBytes(attachmentGR);
var dataAsString = Packages.java.lang.String(bytesInFile);
dataAsString = String(dataAsString);
data_array = dataAsString.split("\n");
var newArray =[];
for(var i = 1; i<data_array.length-1; i++) {
newArray.push(data_array[i]+ "\n" + "*");
}
var strArr = String(newArray); //strArr contains the string format of the csv without the header row
gs.print("StringData1 - "+strArr );
strArr = strArr.replaceAll("*," , '').replace("*",'');
gs.print("StringData2 - "+strArr );
var stringUtil = new GlideStringUtil();
var base64EncodeString = stringUtil.base64Encode(strArr);
//file name needs to have a time stamp in a format of 1-YYYYMMDD-hhmmss.csv
var gdt = new GlideDateTime();
gdt.setValue(gs.nowDateTime());
gdt.getLocalDate();
var execDateTime = gdt.toString();
execDateTime = execDateTime.replace('-', '');
execDateTime = execDateTime.replace(':', '');
execDateTime = execDateTime.replace(' ', '');
var execDate = execDateTime.substring(0,7);
var execTime = execDateTime.substring(8);
var fileName = '1-'+ execDate + '-' + execTime +'.csv';
var record_sys_id = gs.getProperty('pp_mid_server_script_file_sysid');
//update the corresponding MID Server script file with the content of the stripped attachment
var midServerAttachGR = new GlideRecord('ecc_agent_script_file');
if (midServerAttachGR.get(record_sys_id)){
midServerAttachGR.script = strArr;
midServerAttachGR.name = fileName;
midServerAttachGR.update();
}
}
})(current, previous);
Here is what the MID Server script file looks like:
Thanks,
Mussie
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-08-2022 11:40 PM
var record_sys_id = gs.getProperty('pp_mid_server_script_file_sysid');
I don't understand how this value comes from and what it is used for.
If you can see, can you answer me?
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎08-14-2018 02:20 AM
Hi Mussie
We have a very similar requirement with a client of ours and what we have managed to accomplish with your scripts here so far is to take the original attachment and strip out everything we do not need but for the last part we are stuck we are not understanding the last bit where you are transferring the attachment to the mid server. We cannot get the script to kick off that we created. Do we actually have to use the sys_id or do we just parse the sysid property. I am a bit confused there. I am referring to the below piece of code.
var record_sys_id = gs.getProperty('pp_mid_server_script_file_sysid');
//update the corresponding MID Server script file with the content of the stripped attachment
var midServerAttachGR = new GlideRecord('ecc_agent_script_file');
if (midServerAttachGR.get(record_sys_id)){
midServerAttachGR.script = strArr;
midServerAttachGR.name = fileName;
midServerAttachGR.update();
}
}
})(current, previous);
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎01-04-2019 02:47 PM
is anyone able to explain the general concept here to me? i get that there is a business rule that runs on insert, and it queries the attachment table to find the CSV, clean it up, and copy it to a script file... but then what?
general steps would be...
1) export set happens
2) business rule runs when an export set CSV is created
3) the CSV's data is copied to the script file?
4) then how does the file get saved/stored to the midserver or how do you access that file on the mid server?]
is this just intended to send the CSV document to the midserver? can i use this somehow to export attachments from incident records to store on the mid server?

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎02-18-2019 05:45 AM
Hi arabeau,
Following is the steps updated:
1) export set happens
2) business rule runs when an export set CSV is created
3) the CSV's file data is retrieved and header is removed
4)Actual content of the CSV file without header and the file name is updated in
ecc_agent_script_file table corresponding to the record created earlier based on Sys ID.
5. This automatically place the file in agent/scripts (NOT IN EXPORT folder) in mid server.
Hi ALL,
can any one confirm if this is best practices to use the ecc_agent_script_file table to update multiple time
to send the attachment. and will there be any performance impact due to this.
Thanks
Manimaran

- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
‎07-07-2020 10:18 AM
Call me picky, but does anyone have a simpler post that JUST pushes an attachment to mid without any processing of the attachment? It drives me crazy when the title says one topic but the content is doing 3-4 other things mixed in.