Mirth processing multiple segments

dushi kanagasabai picture dushi kanagasabai · Mar 30, 2012 · Viewed 11.4k times · Source

Here I am doing some mapping for Next of Kin $('Nok') (see mapping table).

Then to process this I have the Javascript below. The reason that I am trying this was is, at times we get multiple next of kin segments come through. If that is the case, mirth throws error as ‘DETAILS: TypeError: Assignment to lists with more than one item is not supported’

var i = 0;
msg['NK1'][i]['NK1.3']['NK1.3.1'] = $('NoK')

for each ( nk1 in msg.NK1) {
   nk1 = $('NoK').toString();
   i++;
}

But unfortunately my script doesn’t work. Basically, it doesn’t throw any error, but it doesn’t do what it supposed to do for multiple segment. It does works for a single segment

This my outbound message:

NK1|1|BENNY^BEN^^^MR^^L|<12K1.3.1>22<12K1.3.1>627^^RELTN|PRETTY GREEN^LONDON^""^""^GH15 3KW^^^Q36|||^^RELT|20030321|||||||9 NK1|2|^^^^^^L|SP^^RELTN|41 PIPERS GREEN^LONDON^""^""^NW9 8UH^^^Q36|||^^RELT|20010923|||||||9

Answer

dividius picture dividius · Mar 30, 2012

I see a couple of issues.

  1. Your assignment to $('Nok') in the first transformer step is for the first HL7 segment only; it doesn't effect any subsequent steps.
  2. Your Javascript function is mixing/matching two different approaches to looping- on the one hand trying to do a for each and on the other using i as a loop control variable which is assigned and incremented but never really used.

If you fix #2 only, I would expect you to end up with the first segment repeated n times.

What I would recommend is to move all of this work into a single Javascript transformer step.

You can start by taking a look at the javascript which is generated by your RegEx Mapping step and turning that into a function in your JS transformer--one which takes i as a variable. Then you can fix up your loop to be a simple for loop that calls your function. Something along the lines of:

for(var i = 0; i< msg['NK1'].length; i++) {
    msg['NK1'][i]['NK1.3']['NK1.3.1'] 
        = YourTransformerFunction(msg['NK1'][i]['NK1.3']['NK1.3.1'].toString());
}

You can see the JavaScript generated by the mapper function by exporting the transformer as XML and opening up that file. You'll need to do some replacement for HTML Encoded Values, but the core will be there.