![]() |
Mirth Tools: User defined functions
Hi,
Out of a desire to learn, partial boredom, and a double-dog dare, I decided to create a thread topic to list some things that I find useful when dealing with Mirth, creating Channels, or fine-tuning the database. First, I'd figure I would start out with some functions that some may find useful when developing channels. Feel free to use them, tweak them, or add more to this thread of others to gaze upon. I hope to start other threads with other tools/scripts/code examples to assist in making our daily Mirth Connect experience that more enjoyable and less migraine inducing. These functions can be defined and called from within a single Channel itself, or placed into the Code Template section for use throughout all of your Channels. 1. Determine a Channel's status Purpose: To determine the current state of a channel. Sometimes, you need to know the status of a certain channel, from within another channel. To accomplish this, you can use the following to see if the channel is : -STARTED -STOPPED -PAUSED -NA (Non-available) Code:
function GetChannelState(channel_id) { |
2. Quick SELECT/UPDATE/INSERT functions
Purpose: If most of your channels are connecting to the same database, you can use these functions to cut down some lines of code and for re-usability Let's say that all of your channels are connecting to the same MySQL database. You could create a SELECT function as such: Code:
function CDRSelect(sql) { Code:
var results= CDRSelect("SELECT * FROM results"); Code:
function CDRUpdate(sql) { Code:
var results= CDRUpdate("UPDATE results set name = 'test'"); |
3. Misc. Trim functions
Purpose: Sometimes, you need to do some trimming of strings. If you do not feeling like memorizing Regex, you can use something along the lines of the following. Code:
function trim(in_str) { Code:
function ltrim(in_str) { Code:
function rtrim(in_str) { Now, the function can be called within a transformer or mapper step: Code:
var fname= trim(msg['patient_first'].toString()); |
4. Word-Wrap
Purpose: If for some reason, you need to perform some Work Wrapping, then this function emulates PHP's 'wordwrap'. -The string to be wrapped. -The column width (a number, default: 75) -The character(s) to be inserted at every break. (default: ‘\n’) -The cut: a Boolean value (false by default). Code:
function wordwrap( str, width, brk, cut ) { Code:
wordwrap('The quick brown fox jumped over the lazy dog.', 20, '<br/>\n'); |
5: Local Timzones
Purpose: Sometimes, your not running Mirth locally and you need the correct timezone. Code:
function getCurrentLocalTimestamp() { Code:
tmp['MSH']['MSH.7']['MSH.7.1'] = getCurrentLocalTimestamp(); |
1 Attachment(s)
Quote:
xmlToHL7 Deserializes an E4X XML node into HL7 v2.x. This allows you to pass any E4X node of a message and convert it into HL7 v2.x correctly. Here's the code (I've also attached it as a code template): Code:
function xmlToHL7(node,fieldSeparator,componentSeparator,repetitionMarker,subcomponentSeparator) { Deserialize the entire message: Code:
logger.info(xmlToHL7(msg)); Code:
logger.info(xmlToHL7(msg.PID)); Code:
logger.info(xmlToHL7(msg.PID['PID.3'])); |
Function to Stop a channel if it reaches a particular error count. Also sends an alert message via another channel (this sends a sms via our monitoring system).
Code:
function stopOnErrorCount(cid, errorCount) { To use this in the Postprocessor: Code:
// will stop the current channel when errorcount greater than 10. |
Converting HL7 DTG to English Date
I'm swamped so can't add much but here's something:
Code:
This one does the time as well Code:
function DTGConvert(strString) { |
Thanks for all of the contributions! These seem really useful and they should probably be posted to the wiki so they don't get lost in the forums.
|
1 Attachment(s)
Quote:
When you add fields/components/subcomponents out of order in a message, it isn't serialized correctly by Mirth. For example, if you have the following code: Code:
var nte = <NTE/>; Code:
NTE|1||comment|TX fixHL7NodeOrder takes in a single HL7 v2.x node (it can be the root HL7Message, or a single segment/field/component/etc.), and returns the same node, with all children sorted in ascending order as per the HL7 dot notation. So the following node: Code:
<NTE> Code:
<NTE> Code:
/* |
All times are GMT -8. The time now is 06:49 PM. |
Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2019, vBulletin Solutions, Inc.
Mirth Corporation