From a5b3c2c7f39436304f6b0c6497b8679a619096fd Mon Sep 17 00:00:00 2001 From: pacmano1 <44065187+pacmano1@users.noreply.github.com> Date: Sat, 14 Oct 2023 15:40:01 -0500 Subject: [PATCH] Create DBReadertoCSV.js --- DBReadertoCSV.js | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 DBReadertoCSV.js diff --git a/DBReadertoCSV.js b/DBReadertoCSV.js new file mode 100644 index 0000000..3b3837b --- /dev/null +++ b/DBReadertoCSV.js @@ -0,0 +1,27 @@ +// Notes: Assumes you have aggregate results set to yes on the DB reader +// Do not use an outbound template with this solution. +// Code courtesy of Tony Germano and Jon Bartels, original forum thread: https://forums.mirthproject.io/forum/mirth-connect/support/16869- + + +// Set the root element name to 'delimited' (optional but mentioned) +msg.setName('delimited'); + +// Loop through each result in the 'msg.result' collection and rename them to 'row' +for each (var result in msg.result) { + result.setName('row'); +} + +// Create a header row for the XML data +var headerRow = new XML(''); + +// Iterate through each element in the first 'msg.row' (assuming it exists) and create header columns +for each (var element in msg.row[0].children()) { + if (element != null) { + var name = element.name(); + var headerColumn = new XML('<' + name + '>' + name + ''); + headerRow.appendChild(headerColumn); + } +} + +// Prepend the header row to the XML message +msg.prependChild(headerRow);