|
| 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 | +}; |
0 commit comments