Monday, June 20, 2016

A Few Miscellaneous Scripts and Snippets for Mirth

These are some scripts and snippets I have found useful in Mirth Connect.

---

Quite often when I am developing a new script for a channel, I've found it to be particularly helpful to have something akin to a break-point within the script that sends output to the console.
Using the logger.info() method is great when you are just inserting a single point here and there, but occasionally there are times when I need to know where exactly things stopped working, and to do that I want to be able to trace the script's execution much more easily.
To accomplish this without having to spend much more time going back and forth from the code to the channel, I wrote a quick script which I then added to the Code Templates section of Mirth.

function brk(tag) {
logger.info("Test Point "+tag);
}

That's it. Now all I have to do when I want to keep track of each time the code hits a certain point in the code is to insert brk(1); and the console output shows "Test Point 1whenever it goes off. Sure it's a bit lazy, but then again, I am all about making my job easier.

---

Something that really qualifies as more of a trick and less of a script: If you want to add multiple segments to an object for easy passing back and forth, instead of trying to come up with some crazy complicated object structure, just create an array and add the objects as items in the array. You can still call and pass like a regular object, but the number of items is purely dynamic and is quite easy to reference. (Pro-Tip: The simplest way to do this is to create a temporary object and then assign a spot in the array to it)
For example:

var objectArray = [];
var tempObject = {};
    //get the next spot in the array and set it as your new index
var index = objectArray.length; // lets call it '3'
    //assign properties to the temp object
tempObject.patientId = msg["PID"]["PID.2"]["PID.2.1"].toString(); // we'll say it's "1234"
    // etc...
    //now assign the object to the next index in the array
objectArray[index] = tempObject;
    //make sure to clear the temp object before each new round
tempObject = {};

...

    //now you can call the properties using the index of the array
    //eg -
logger.info(objectArray[3].patientId); // "1234"

No comments:

Post a Comment