diff --git a/Server-Side Components/Background Scripts/Get Outstanding Incidents/README.md b/Server-Side Components/Background Scripts/Get Outstanding Incidents/README.md new file mode 100644 index 0000000000..9e71d20f00 --- /dev/null +++ b/Server-Side Components/Background Scripts/Get Outstanding Incidents/README.md @@ -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. diff --git a/Server-Side Components/Background Scripts/Get Outstanding Incidents/get-outstanding-incidents.js b/Server-Side Components/Background Scripts/Get Outstanding Incidents/get-outstanding-incidents.js new file mode 100644 index 0000000000..43eb3584db --- /dev/null +++ b/Server-Side Components/Background Scripts/Get Outstanding Incidents/get-outstanding-incidents.js @@ -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)); diff --git a/Server-Side Components/Script Includes/Read CSV file from Mid Server/CSVReaderUtil.js b/Server-Side Components/Script Includes/Read CSV file from Mid Server/CSVReaderUtil.js new file mode 100644 index 0000000000..60aba414aa --- /dev/null +++ b/Server-Side Components/Script Includes/Read CSV file from Mid Server/CSVReaderUtil.js @@ -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 = ''; + + 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' +}; diff --git a/Server-Side Components/Script Includes/Read CSV file from Mid Server/README.md b/Server-Side Components/Script Includes/Read CSV file from Mid Server/README.md new file mode 100644 index 0000000000..69684e5f79 --- /dev/null +++ b/Server-Side Components/Script Includes/Read CSV file from Mid Server/README.md @@ -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);