Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# ServiceNow Background Script — Outstanding Incidents Before Current Month (JSON Output)

This background script retrieves all "active" incidents created before the start of the current month, and outputs the results in JSON format.

It is useful for reporting involving aging incident records.
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
var result = [];
var gr = new GlideRecord('incident');
gr.addQuery('active', true);
var startOfMonth = new GlideDateTime();
startOfMonth.setDisplayValue(gs.beginningOfThisMonth());
gr.addQuery('sys_created_on', '<', startOfMonth);

gr.query();

while (gr.next()) {
result.push({
number: gr.getValue('number'),
short_description: gr.getValue('short_description'),
assigned_to: gr.getDisplayValue('assigned_to'),
sys_created_on: gr.getDisplayValue('sys_created_on'),
state: gr.getDisplayValue('state')
});
}

var output = {
total_count: result.length,
generated_on: gs.nowDateTime(),
outstanding_incidents: result
};

gs.print(JSON.stringify(output,null,2));
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
var CSVReaderUtil = Class.create();
CSVReaderUtil.prototype = {
initialize: function() {
this.midServer = 'YOUR_MID_SERVER_NAME'; // Replace with your MID Server name
},

// Method to read CSV file from MID Server
readCSVFile: function(filePath) {
try {
// Create ECC Queue input record (Probe)
var probe = new GlideRecord('ecc_queue');
probe.agent = 'mid.server.' + this.midServer;
probe.topic = 'Command';
probe.name = 'read_csv_file';
probe.source = 'ServiceNow';
probe.queue = 'output';

var scriptPath = 'cat ' + filePath;
probe.payload = '<?xml version="1.0" encoding="UTF-8"?><parameters><parameter name="name" value="' + scriptPath + '"/></parameters>';

var probeId = probe.insert();

gs.info('CSV Read Probe sent with sys_id: ' + probeId);
return probeId;

} catch (e) {
gs.error('Error sending probe: ' + e.message);
return null;
}
},

// Method to check and retrieve the response
getCSVResponse: function(probeId) {
var gr = new GlideRecord('ecc_queue');
gr.addQuery('response_to', probeId);
gr.addQuery('state', 'processed');
gr.orderByDesc('sys_created_on');
gr.query();

if (gr.next()) {
var payload = gr.payload + '';
return this.parseCSVPayload(payload);
}
return null;
},

// Parse the CSV data from payload
parseCSVPayload: function(payload) {
var csvData = [];
try {
// Extract CSV content from XML payload
var xmlDoc = new XMLDocument2();
xmlDoc.parseXML(payload);
var content = xmlDoc.getNode('//stdout');

if (content) {
var lines = content.split('\n');
for (var i = 0; i < lines.length; i++) {
if (lines[i].trim()) {
csvData.push(lines[i].split(','));
}
}
}
} catch (e) {
gs.error('Error parsing CSV payload: ' + e.message);
}
return csvData;
},

type: 'CSVReaderUtil'
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
ServiceNow MID Server CSV Reader

This utility enables reading CSV files from MID Server machines through the ECC Queue mechanism.

Overview

The solution uses a Script Include named CSVReaderUtil.js to send and receive data between the ServiceNow instance and the MID Server.
It supports executing read operations on local CSV files stored on the MID Server filesystem.

Steps to Use

Create the Script Include
Create a new Script Include named CSVReaderUtil.js in ServiceNow.
This Script Include handles communication with the MID Server and parsing of CSV data.

Trigger the Script from a Background Script

Use the following example to read data from a CSV file located on the MID Server:

var csvUtil = new CSVReaderUtil();

// Send probe to the MID Server to read the CSV file
var probeId = csvUtil.readCSVFile('/path/to/your/file.csv');

// Wait a few seconds to allow the MID Server to process the request
gs.sleep(5000);

// Retrieve the response from the ECC Queue
var csvData = csvUtil.getCSVResponse(probeId);

gs.info('CSV Data: ' + csvData);
Loading