Mapping Multiple Segments to a Database - rbeckman-nextgen/test-mc GitHub Wiki
Created by Jacob Brauer, last modified on Mar 23, 2009
This post will explain the likely situation where you need to process more than one segment of the same type from your current HL-7 message to a database. For example, consider an ADT^A04 with multiple NK1 segments for multiple next of kins.
Now, if we want to store this information with Mirth, the transformer does not and, of course, can not predict the future so that we would know how many next of kins the message has. You can specify a certain max to the next of kin you have, but this tutorial will give you the ability to map every next of kin (or any other segment) into your database.
All of the following will be placed into a filter:
//first thing you may want to do here is ensure that you are
// only dealing with messages that have the segments you are
// looking for. In this example, we will deal with ADT^A04 messages.
if(msg['MSH']['MSH.9']['CM_MSG.1'] == "ADT" && msg['MSH']['MSH.9']['CM_MSG.2'] == "A04")
{
//the driver will depend on your database. we will use MySQL
var driver = "com.mysql.jdbc.Driver";
var address = "jdbc:mysql://localhost/rawhl7";
var username = "mirth";
var password = "password";
var dbConn = DatabaseConnectionFactory.createDatabaseConnection(driver,address,username,password);
//in my bridge table that is storing the kin for my patients, I need the patient ID to identify my patient
var patientID = msg['PID']['PID.3']['CX.1'];
var i = 0;
//whenever you have more than one segment, Mirth essentially creates an array of them. Which you can access traditionally varname[index].
while(msg['NK1']['NK1.2']['XPN.2'][i] != null) {
var kinsFirstName= msg['NK1']['NK1.2']['XPN.2'][i];
var expression = "INSERT INTO kinTable values (DEFAULT, '" + patientID + "', '" + kinsFirstName + "');";
var result = dbConn.executeUpdate(expression);
i = i + 1;
}
//simply closes the connection to the DB
dbConn.close();
}
//finally you can return whatever you want to do with your filter.
return true;
Document generated by Confluence on Nov 11, 2019 08:40