Friday, May 27, 2022
Home Integration/Interfaces Why You Must Understand Mirth's msg and tmp Variables

Why You Must Understand Mirth’s msg and tmp Variables

Prerequisites

  • Understand the difference between an inbound and outbound channel
  • Understand JavaScript programming with Mirth Connect
  • Understand the array data type
  • Understand data types in Mirth Connect

If you’ve spent any time developing in Mirth Connect, you probably remember learning how to use Mirth’s msg and tmp variables. See the table below for a quick summary comparing msg and tmp.

VariablePurposeUse Example
msgreferences the incoming message/datavar sendApp = msg[‘MSH’][‘MSH.3’][‘MSH.3.1’].toString();
tmpreferences the outgoing message/datatmp[‘MSH’][‘MSH.3’][‘MSH.3.1’] = “EPIC”;

Developers need to know that a healthcare interface can either be inbound or outbound in reference to their application. An inbound interface comes from another application to your application while an outbound interface leaves your application and goes to another application.

Furthermore, when you are referencing data from an inbound interface in Mirth, you will want to use msg. Likewise, you are defining data for an outbound interface, you will want to use tmp. These concepts will be explored further in the following sections.

Msg

Msg is the pre-defined variable that references incoming messages and data for your Mirth channels. Internally, Mirth will serialize msg in XML format regardless of the set data type. Consequently, developers can reference msg in their JavaScript using array indexing. Array indexing works because data coming into Mirth is structured hierarchically.

For example, every HL7 message is made up of one or more segments. Each segment is made up of one or more fields. The below JavaScript variable references the first component of field three of the MSH segment in the incoming HL7 message.

msg['MSH']['MSH.3']['MSH.3.1']

The first index tells Mirth that it needs to look at the MSH segment. The second index tells Mirth that it needs to look in the third field of MSH. Finally, the third index tells Mirth to look at the first component of the third field.

Note: If you are unfamiliar with how HL7 messages are structured click here to learn more.

Msg usually appears on the right-hand side of the assignment operator in code because we want to assign this data to a variable in order to use it throughout the rest of the channel code.

Additionally, msg works a bit differently with the HTTP Sender and destination connectors of that nature. When you call a web service using an HTTP Sender in Mirth, you will often get a response depending on if the web service responds with data.

This response data is returned to Mirth via the msg variable. However, this data is only available in the response transformer. That is because the destination transformer code is executed before the web service is called. Meaning, if you reference msg in the destination transformer you are referencing the original incoming message. This is important to keep in mind if you plan on using Mirth to call a web service that returns data.

Understanding how to use msg is absolutely vital because it is the data structure that allows developers to access the data needed to build an inbound interface. If developers cannot access incoming data, their inbound interface is effectively worthless.

A good practice when developing inbound interfaces is to have an initial step in your transformer where you create variables and initialize them from the data in msg. That way, you can use, reference, and manipulate these values in the remainder of your code.

Tmp

Tmp is the pre-defined variable that references outgoing messages and data for your Mirth channels. Internally, Mirth will serialize tmp in XML format regardless of the data type. Consequently, developers can reference tmp in their JavaScript using array indexing when creating outbound healthcare interfaces.

The below JavaScript variable references the first component of field three of the MSH segment of an outgoing HL7 message.

tmp['MSH']['MSH.3']['MSH.3.1']

Just as with msg, Mirth uses the same array indexing notation to reference the first component of MSH.3 for the outgoing message.

Tmp usually appears on the left-hand side of the assignment operator because we want to set the value of this data before Mirth sends it out to the receiving system. In other words, we are building the message to send out to the receiving system.

Understanding how to use tmp is so important because it is the data structure that allows developers to build a message for their outbound interface. If developers cannot set outgoing data, their outbound interface is worthless since there is no data to send to the receiving side.

When using tmp, it’s vital that you understand the requirements for the data types that you are using because you have to know the correct place to place data. For example, if your interface requires the HL7 data type then you need to know to place the patient account number from your system in PID.18.1.

This is also true for msg; however, when developing the interface, you only have control of what you put in the outbound message; however, the quality of the data from another system is out of your control.

Msg, Tmp, and Mirth Data Types

Mirth allows developers create channels with the following data types.

  • Delimited Text
  • DICOM
  • X12
  • HL7 v2.X
  • HL7 v3.X
  • JSON
  • NCPDP
  • Raw
  • XML

Regardless of the data type, developers can reference the data in msg or tmp using array indexing. However, this looks slightly different depending on the data type chosen.

If you are ever confused about these slight differences, take full advantage of Mirth’s message template tool. You can access this tool on the right-hand side of any transformer, filter, or response.

Paste an example message in either the inbound or outbound message template.

Next, click on the message trees tab.

Expand the elements of your message tree.

You can drag green nodes into the code editor and Mirth will form the correct reference for you. If you drag a variable from the inbound message tree then Mirth will use msg. Similarly, if you drag a variable from the outbound message tree then Mirth will use tmp.

For example, you can reference the first column in the first row of a delimited text file using the follow variable.

msg['row'][0]['column1']

Take some time to change the data type on the message template and play around with msg and tmp in your channels.

RELATED ARTICLES

Why You Must Learn Mirth Connect’s User API Now

Prerequisites Basic knowledge of Mirth ConnectBasic knowledge of JavaScriptBasic knowledge of software documentation and APIsBasic knowledge of Java development NextGen provides several different APIs (Application Programming...

Leverage Mirth’s Channel Scripts Now

Prerequisites Basic knowledge of Mirth ConnectBasic knowledge of JavaScript Each Mirth Connect channel gives you the ability to leverage four different scripts that have different triggers,...

Reference a Custom Jar File in Mirth Connect Now

Prerequisites Knowledge of Java developmentKnowledge of Eclipse or another Java IDEKnowledge of Mirth Connect Many Mirth professionals are unaware that they can write their own custom...

Most Popular

Mirth Connect Extensions: What You Need to Know

Prerequisistes Basic Knowledge of Mirth Connect Overview Users can navigate to Mirth Connect's Extensions screen by selecting Extensions on the left-hand side. You should now see the following...

Take a Tour of System Settings in Mirth Connect Now

Prerequisites Basic Knowledge of Mirth Connect Introduction Mirth Connect has a multitude of system settings that are available for configuration. In larger companies, there will often be...

Why You Must Master Mirth’s Message Template Tool Today

Prerequisuites Basic Knowledge of Mirth ConnectKnowledge of Programming in Mirth Connect Introduction When writing scripts in Mirth Connect, you have a message template tool that allows you...

Make Sense of HL7 v2 Messages Effortlessly Now

Prerequisites Beginner Knowledge of HL7 Introduction If you've been in Healthcare Information Technology for any period of time, you have definitely heard of HL7 (Health Level 7)....

Recent Comments