Skip to content

Commit 2b57510

Browse files
authored
feature/Read-CSV-file-from-mid-server (#1979)
* Create get-outstanding-incidents.js * Create README.md * Create CSVReaderUtil.js * Create README.md
1 parent 72c097a commit 2b57510

File tree

2 files changed

+102
-0
lines changed

2 files changed

+102
-0
lines changed
Lines changed: 71 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
var CSVReaderUtil = Class.create();
2+
CSVReaderUtil.prototype = {
3+
initialize: function() {
4+
this.midServer = 'YOUR_MID_SERVER_NAME'; // Replace with your MID Server name
5+
},
6+
7+
// Method to read CSV file from MID Server
8+
readCSVFile: function(filePath) {
9+
try {
10+
// Create ECC Queue input record (Probe)
11+
var probe = new GlideRecord('ecc_queue');
12+
probe.agent = 'mid.server.' + this.midServer;
13+
probe.topic = 'Command';
14+
probe.name = 'read_csv_file';
15+
probe.source = 'ServiceNow';
16+
probe.queue = 'output';
17+
18+
var scriptPath = 'cat ' + filePath;
19+
probe.payload = '<?xml version="1.0" encoding="UTF-8"?><parameters><parameter name="name" value="' + scriptPath + '"/></parameters>';
20+
21+
var probeId = probe.insert();
22+
23+
gs.info('CSV Read Probe sent with sys_id: ' + probeId);
24+
return probeId;
25+
26+
} catch (e) {
27+
gs.error('Error sending probe: ' + e.message);
28+
return null;
29+
}
30+
},
31+
32+
// Method to check and retrieve the response
33+
getCSVResponse: function(probeId) {
34+
var gr = new GlideRecord('ecc_queue');
35+
gr.addQuery('response_to', probeId);
36+
gr.addQuery('state', 'processed');
37+
gr.orderByDesc('sys_created_on');
38+
gr.query();
39+
40+
if (gr.next()) {
41+
var payload = gr.payload + '';
42+
return this.parseCSVPayload(payload);
43+
}
44+
return null;
45+
},
46+
47+
// Parse the CSV data from payload
48+
parseCSVPayload: function(payload) {
49+
var csvData = [];
50+
try {
51+
// Extract CSV content from XML payload
52+
var xmlDoc = new XMLDocument2();
53+
xmlDoc.parseXML(payload);
54+
var content = xmlDoc.getNode('//stdout');
55+
56+
if (content) {
57+
var lines = content.split('\n');
58+
for (var i = 0; i < lines.length; i++) {
59+
if (lines[i].trim()) {
60+
csvData.push(lines[i].split(','));
61+
}
62+
}
63+
}
64+
} catch (e) {
65+
gs.error('Error parsing CSV payload: ' + e.message);
66+
}
67+
return csvData;
68+
},
69+
70+
type: 'CSVReaderUtil'
71+
};
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
ServiceNow MID Server CSV Reader
2+
3+
This utility enables reading CSV files from MID Server machines through the ECC Queue mechanism.
4+
5+
Overview
6+
7+
The solution uses a Script Include named CSVReaderUtil.js to send and receive data between the ServiceNow instance and the MID Server.
8+
It supports executing read operations on local CSV files stored on the MID Server filesystem.
9+
10+
Steps to Use
11+
12+
Create the Script Include
13+
Create a new Script Include named CSVReaderUtil.js in ServiceNow.
14+
This Script Include handles communication with the MID Server and parsing of CSV data.
15+
16+
Trigger the Script from a Background Script
17+
18+
Use the following example to read data from a CSV file located on the MID Server:
19+
20+
var csvUtil = new CSVReaderUtil();
21+
22+
// Send probe to the MID Server to read the CSV file
23+
var probeId = csvUtil.readCSVFile('/path/to/your/file.csv');
24+
25+
// Wait a few seconds to allow the MID Server to process the request
26+
gs.sleep(5000);
27+
28+
// Retrieve the response from the ECC Queue
29+
var csvData = csvUtil.getCSVResponse(probeId);
30+
31+
gs.info('CSV Data: ' + csvData);

0 commit comments

Comments
 (0)