Tuesday, June 28, 2016

Passing Database Results to Outbound Messages in Mirth Connect

Recently I had an interoperability project that required reading from a database at regular intervals, and then feeding the information out through an ORU message. Because this was the first time in a while where I had to read FROM the database, it took me a minute before I was reminded that parsing the resultant data doesn't work quite the same as your typical 2.X/3.X/XML message.

 On the plus side, Mirth Connect makes it very easy to handle database results. For a very simple example, start by creating a channel, set the source type to "Database Reader", and set the response to "Destination 1". In the SQL textarea put your query, and set up the rest of the source connector according to your system requirements.
Here is a screenshot of my example setup.

Once you have your source connector set-up, you can either choose to handle the message using source transformers, or pass the message to a destination transformer.
If you pass the results to a destination it is important to note that you will need to create a reference to the message that will be passed to the destination.
A simple way of doing that is to add a javascript step into the source transformer and include the following code:

tmp = msg;

This tells Mirth to send the results onward as a feeder message for the destination transformer.

Now comes the best part about handling database results.

You can now call all of your results in the destination using the column name as the reference!

For example, if I wanted to get the value from the "msg_id" column, I can write something like this:
var msg_id = msg["msg_id"].toString();

The basic syntax for getting the values is:
localVariableName = msg["columnName"].toString();

(This syntax will work in both source and destination connectors as long as somewhere in the source connector you add the tmp = msg; command.)

Now I can use that variable to insert a value into the outbound message, affect the logic for further processing, or output to a file.

No comments:

Post a Comment