Friday, September 30, 2022
Home Integration/Interfaces Leverage Mirth's Channel Scripts Now

Leverage Mirth’s Channel Scripts Now

Prerequisites

  • Basic knowledge of Mirth Connect
  • Basic knowledge of JavaScript

Each Mirth Connect channel gives you the ability to leverage four different scripts that have different triggers, depending on the state of your channel. These scripts are great for executing predefined actions when the trigger occurs. Additionally, these scripts will not be leveraged in every interface you create. In fact, it’s rare that all four scripts are used in the same channel.

The four types of scripts are:

  • Deploy
  • Undeploy
  • Preprocessor
  • Postprocessor

These categories describe the triggering action for the script, which will be explored in more detail in the following sections.

You can access these scripts from inside any channel by clicking the Scripts tab.

You will see a text editor for your scripts appear below.

Additionally, you will see a Script combo box that allows you to toggle between the four different types of scripts.

In each script, you will see comments that describe the purpose of that script. Below, the specifics of the deploy script are outlined.

Deploy

The deploy script allows Mirth developers to execute predefined actions when a channel is deployed, including when the Mirth server starts up. In fact, every deploy script for every channel is executed when the Mirth server comes back up. This is because starting the Mirth Connect service deploys all enabled channels in the instance.

The deploy script is typically used to complete any sort of setup needed for that channel. This is why you would want to run this code upon channel deployment. For example, you might need to load a large data dictionary to use in the channel.

This is how Mirth describes the deploy script in its own words.

Notice that developers can only reference the globalMap and globalChannelMap in the deploy script. Meaning, that the channelMap cannot be referenced. This makes sense since the channel is just starting up and the channelMap has not been instantiated yet. There is really no data to access from the channelMap.

Example

Consider the example below that maps dataIdentifier. Data in the Mirth Connect channel is always sent to identifier below. Since this is the case, it makes sense to only map it once when the channel is deployed rather than map it for every message that flows through the channel.

var dataIdentifier = "XXX-XX-XXXX";
globalChannelMap.put('dataIdentifier', dataIdentifier);

Undeploy

The undeploy script allows Mirth developers to execute predefined actions when a channel is undeployed, including when the Mirth server is powering down. This works exactly like the deploy script, except the script is triggered when the channel is undeployed.

The undeploy script is typically used to complete any sort of wrap-up or clean-up actions. For example, maybe you have a connection open to a file or another data source and need to close the connection. The undeploy script would be a logical place for such a task.

This is how Mirth describes the undeploy script in its own words.

Like in the deploy script, developers can use the globalMap nor the globalChannelMap; however, the channelMap remains unavailable for use.

Example

Consider the following example that closes a DB connection is one exists.

if(dbConn) {
  dbConn.close();
}

Preprocessor

The preprocessor script allows Mirth developers to execute predefined actions before a message begins processing at all. Specifically, the preprocessor script runs before the message even appears on the source.

This is very similar to the deploy script except that it runs before every message rather than upon channel deployment. The preprocessor script is typically used by developers to fix up the message before allowing it to pass to the source. For example, the preprocessor script could format a patient’s social security number with hyphens if it comes in without hyphens.

Mirth doesn’t offer much of an explanation of the preprocessor script like it does with the other scripts.

Notice that Mirth does not state that the channelMap is unavailable for use in the preprocessor script. Feel free to leverage the channelMap to do any prep work for your channel in the preprocessor script.

Postprocessor

The postprocessor script allows Mirth developers to execute predefined actions after a message has finished processing completely. Meaning, the postprocessor script runs after the message has gone through the preprocessor script, source filter, source transformer, destination filter, and destination transformer.

This is how Mirth describes the postprocessor script in its own words.

Again, Mirth does not state that the channelMap is unavailable for use. Feel free to leverage the channelMap to do any wrap up work related to messages in the preprocessor script. Also, note that any responses returned in the postprocessor script can be found in the responseMap.


Mirth’s four types of channel scripts provide versatility to developers when creating channels. However, you shouldn’t be using these scripts as a major part of your development. Most of your work should still exist in the source filter, source transformer, destination filter, destination transformer.

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...

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...

Get to Know Mirth’s Configuration Map Now

What exactly is Mirth's configuration map? And how should Mirth developers take advantage of it to create more maintainable channels? Before diving in, it should...

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