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);