Friday, 13 May 2016

Transforming repeat fields to single values in Mirth Connect

With HL7, repeat fields are always tricky to deal with. Often when I'm bringing in a repeat field I just want the first instances value for passing on to another system.
An obvious example of this is a patients phone number, where the receiving system in the integration only support a single phone number (or more likely one for home, and one for business).
If we create a default transformer variable in Mirth for the phone number it generates the code:
As we don’t want this to error when the phone number is repeated we need to select only the first entry by adding an index pointer to the first item:
And there you have it - or so I thought - Welcome the next problem…
Because my patient is used in several destinations, I have this code as a source transformer. This means that the code is executed for every message coming in.  But not every message has a PID segment. The code without the 0 index works fine still, but my code that only gets the first instance starts showing errors in Mirth's Dashboard.
ERROR (transformer:?): TypeError: Cannot read property "PID.13.1" from undefined
It doesn't actually stop the channel from working, but nobody wants to see the dashboard filled with errors the whole time.
So now I have to change my code again. I added a check that makes sure that field actually exists, and only if it does will it attempt to get the first value.
(msg['PID']['PID.13'].toString() !='')? msg['PID']['PID.13'][0]['PID.13.1'].toString():null
There you have it. This is my recommended technique for handling repeat fields in Mirth. I recommend that every field you want a single value where HL7 supports repeated values should use this code.

1 comment:

  1. Thank you so much!!!! You are a life saver.