Mirth (NextGen) Connect Bits #7: Code Templates Usage

If you’re a developer familiar with full-stack technologies and some software design patterns like MVC, you’ll know that you have parts of your codebase where you have helpers, model, controller, and others. Basically organizing functions, classes, and methods to be re-usable. In Mirth (NextGen) Connect, you can do scripting directly from the Transformer, Destination, Source, Filter, etc… But what if you’re handling multiple interfaces/channels that have the same functions to use? An example case scenario is a patient checker function. This is where “Code Templates” checks in, this is basically where you can write, group, and store custom scripts.

In your Mirth Connect dashboard, you can find it under “Channels” and at the left side panel named “Edit Code Templates

mirth connect edit code templates

Here’s the Code Template page.

mirth connect code template page editor
  1. Creates a new library by clicking “New Library
  2. In my case named “Project X Bidirectional” where all code templates or custom functions for this project will be group here.
  3. Under the said library, you can create a custom function or code template by clicking the “New Code Template“.
  4. You can see I named my code template as “checkPatient
  5. This is where you write your JavaScript code, in my case, here’s a sample of it where you can connect to a database.
/**
	Check Patient using last name, first name and dob
	
	@param {String} first_name - first name of patient
	@param {String} last_name - last name of patient
	@param {String} dob - dob of patient
	@param {String} env - set environment if dev or production
	@return {String} return stringify JSON object of patient details
*/
function checkPatient(first_name, last_name, dob, env) {
	
	var dbConn;

	if(env == 'dev') {
		user_db = 'test';
		pass_db = 'test';
		db = 'db_dev';
	} else if(env == 'production') {
		user_db = 'prod';
		pass_db = 'prod';
		db = 'db_prod';
	}

	var result = {};
	
	try {
	    dbConn = DatabaseConnectionFactory.createDatabaseConnection('org.postgresql.Driver','jdbc:postgresql://127.0.0.1:5432/'+db,user_db,pass_db);
	
	    var params = new Packages.java.util.ArrayList();
	        params.add(first_name);
	        params.add(last_name);
	        params.add(dob);
	
	    var sqlQuery = "SELECT first_name, last_name, date_of_birth FROM patients WHERE first_name = ? AND last_name = ? AND date_of_birth = ? LIMIT 1;";
	
	    var getPatient = dbConn.executeCachedQuery(sqlQuery, params);

	    if(getPatient.size() > 0) {
		    while(getPatient.next()) {
		        result["first_name"] = getPatient.getString('first_name');
		        result["last_name"] = getPatient.getString('last_name');
		        result["dob"] = getPatient.getString('date_of_birth');
		        result["sex"] = getPatient.getString('sex');
		    }

		    return JSON.stringify(result);
	    } else {
	    		return false;
	    }
	
	} catch(e) {
	    throw "Error: " + e.toString();
	} finally {
	    if (dbConn) {
	        dbConn.close();
	    }
}   
}

If you’re not familiar with using JavaScript to perform database operations, you can check this other blog post.

After successfully creating the library and code template, you can assign it to your channels by any of the following methods.

Method 1:

  1. Click on the library
  2. You can check and write the description
  3. On the left side, you can see list of channels you have and tick the box where you want it to be accessible.
mirth connect library assignment

Method 2:

  1. On your channel under “Summary“, hit “Set Dependencies
  2. Move to the “Code Template Libraries
  3. You’ll find your function(s) there, tick the box of the library for the channel to use it.
mirth connect assign library

Once the library is assigned to your channel, you can call the code templates in your JavaScript areas like a Transformer, below you can see that if you type a part of the function it’ll automatically show (or you can also hit CTRL+SPACE) it as an option including the description you wrote.

mirth connect call code template

Below is a sample code, where I get the patient names and date of birth from the PID segment of the HL7 message, then pass them as arguments in my checkPatient code template.

mirth connect sample code template code

Sample code:

var env = 'dev';

var first_name = msg['PID']['PID.5']['PID.5.2'].toString();
var last_name = msg['PID']['PID.5']['PID.5.1'].toString();
var dob = msg['PID']['PID.7']['PID.7.1'].toString();

var patientObject = checkPatient(first_name, last_name, dob, env);

if(patientObject) {
	var patient = JSON.parse(patientObject);

	logger.info(patient['first_name']);
	logger.info(patient['last_name']);
	logger.info(patient['date_of_birth']);
	logger.info(patient['sex']);
} else {
	logger.info('No patient found');
}

If you’re new to Mirth (NextGen) Connect this is a great way to start using Code Templates to properly organize your projects and re-use functions. You can also find some useful Code Templates from the NextGen Connect Github repository here.

For more Mirth Connect related blog posts, check out the Mirth (NextGen) Bits tag or Health IT page.

Add a Comment

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