Mirth (NextGen) Connect Bits #6: HL7 Data Update & Manipulation

HL7 data manipulation in Mirth (NextGen) Connect can be done using the different options in the Transformer. However, for complex conditions where you want to change specific parts of the HL7 message, you can code it using JavaScript.

If you’re reading and outputting the data as HL7, then the data type set in the channel is by default set to HL7 v2.x.

mirth connect data type

Updating Parts of the HL7 Message

When the message is received by Mirth, it’s serialized as an XML object. Which means you can access parts of the message easily. The main object variable is called msg. That is accessible in all of the filter and transformer. If you update the msg object in Source->Transformer, that will be the encoded data that will be used in your Destination(s).

Below is a sample code where I want to standardized the PID.5.1 which is the sex/gender field.

// main variable to access hl7 xml object "msg"

msg['PID']['PID.5']['PID.5.1'] = msg['PID']['PID.5']['PID.5.1'].toString().toUpperCase() == 'FEMALE' ? 'F' :
		msg['PID']['PID.5']['PID.5.1'].toString().toUpperCase() == 'MALE' ? 'M' :
		msg['PID']['PID.5']['PID.5.1'].toString().toUpperCase() == 'OTHERS' ? 'O' :

By directly referencing to the msg object and using the segment names and position, you can access and update the HL7 message itself.

If we want to loop to repeating segments like OBR, we can do that by using a for each loop, below is a sample code where I want to format all OBR.7.1 date to yyyyMMddHHmm (check out this other blog post on how to use the DateUtil function).

// Format date on all OBR.7
for each(obr in msg.OBR) {
	obr['OBR.7']['OBR.7.1'] = DateUtil.convertDate('yyyyMMddHHmmss', 'yyyyMMddHHmm', obr['OBR.7']['OBR.7.1'].toString());

The code below is another scenario where I want to standardize the OBR.25.1, which is the result status for result messages.

// Standardized the result status in OBR.

for each(obr in msg.OBR) {
	obr['OBR.25']['OBR.25.1'] = obr['OBR.25']['OBR.25.1'].toString() == 'F' ? 'Final' :
	obr['OBR.25']['OBR.25.1'].toString() == 'C' ? 'Corrected' :
	obr['OBR.25']['OBR.25.1'].toString() == 'X' ? 'Cancelled' :

If we want to get repeating segments after a specific one, let us say all the NTE segments (notes and comments) but only after each OBR segment, then that’s what the code below does. The idea is that we want to get the NTEs of the OBR segment. Unfortunately, there’s no parent-child relationship directly in the XML object. Because of that, we’ll use the getSegmentsAfter which is a code template available in the NextGen Connect Github repo. You can learn more about its usage there.

var textStr = '';

for each(obr in msg.OBR) {
	textStr += obr['OBR.4']['OBR.4.1'].toString() + '\n';
	for each(nte in getSegmentsAfter(msg, obr, 'NTE')) {
		textStr += nte['NTE.3']['NTE.3.1'].toString() + '\n';


The code above will get all the values in NTE.3.1 after each OBR and concatenate it to our variable textStr.

Creating a new Message

There are a couple of ways on how you can create a new message.

Solution 1:

You can use the outbound template in the Transformer, this is accessible using the tmp object variable, basically the same as the msg object. You can create your bare minimum HL7 message segments and fields, and add it to your outbound template.

mirth connect message tree data

To get the HL7 message segment, field, and position, you can click the Message Trees allowing for easy readability and to get the xml path where you can assign the values on the tmp object. You can collapse the message tree and drag and drop the fields on your javaScript text area.

mirth connect message tree path

Here’s a screen cast on how you can get the specific fields or their xml path or position.

mirth connect message tree gif

Solution 2:

You can copy the receiving msg and alter the data. You can then access the tmp content the same way how you access the msg object, just replace the object name to tmp.

// copy message
var tmp = msg.copy();


// access the copy of the patient name

Solution 3:

Create manually an XML object and add segments directly. Here I created an XML object newMsg. Then I create separate XML objects for segments I want to add including its content (Check out this another blog post on how to add new segment and repeating fields). Here I created an MSH segment, assign values and use appendChild() in the tmp object.

// create a new message manually

var newMsg = new XML('<HL7Message></HL7Message>');

var newMsh = <MSH/>;
	newMsh['MSH.1']['MSH.1.1'] = '|';
	newMsh['MSH.2']['MSH.2.1'] = '^~\\&';
	newMsh['MSH.3']['MSH.3.1'] = 'EMR';
	newMsh['MSH.4']['MSH.4.1'] = 'DOCTORS';
	newMsh['MSH.5']['MSH.5.1'] = 'LIS';
	newMsh['MSH.6']['MSH.6.1'] = 'LAB';
	newMsh['MSH.7']['MSH.7.1'] = DateUtil.getCurrentDate('yyyyMMddHHmmss');
	newMsh['MSH.8']['MSH.8.1'] = '';
	newMsh['MSH.9']['MSH.9.1'] = 'ORM';
	newMsh['MSH.9']['MSH.9.2'] = 'O01';
	newMsh['MSH.10']['MSH.10.1'] = UUIDGenerator.getUUID();
	newMsh['MSH.11']['MSH.11.1'] = 'T';
	newMsh['MSH.12']['MSH.12.1'] = '2.3';


logger.info('newMsg: ' + newMsg);

For more Mirth Connect related blog posts, check out the Mirth (NextGen) Bits tag or Health IT page. Feel free to leave a comment or feedback.

Add a Comment

Your email address will not be published. Required fields are marked *