Skip to content

Commit

Permalink
Create DBReadertoCSV.js
Browse files Browse the repository at this point in the history
  • Loading branch information
pacmano1 authored Oct 14, 2023
1 parent 3f66e5f commit a5b3c2c
Showing 1 changed file with 27 additions and 0 deletions.
27 changes: 27 additions & 0 deletions DBReadertoCSV.js
Original file line number Diff line number Diff line change
@@ -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('<row/>');

// 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 + '</' + name + '>');
headerRow.appendChild(headerColumn);
}
}

// Prepend the header row to the XML message
msg.prependChild(headerRow);

0 comments on commit a5b3c2c

Please sign in to comment.