3

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

dividius
  • 820
  • 6
  • 16
  • It looks like you're trying to do one of two things, but I'm not sure which. 1. Extract the value from the NK1.3.1 component of the first instance of the NK1 field, map it, and write that value into the NK1.3.1 of all the NK1 fields. 2. Extract and map the values from the NK1.3.1 component of each individual NK1 field – csj Apr 13 '12 at 20:13

3 Answers3

4
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());
}

length needed () to work.

Mihai Iorga
  • 39,330
  • 16
  • 106
  • 107
Amin Wahi
  • 41
  • 1
3

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.

dividius
  • 820
  • 6
  • 16
3

I'm not following all of your code, but here's a start.

  1. To loop through all the segments, try tis format:

    for each (seg in msg.children()) {
        if (seg.name().toString() == "NK1") {
            foo = bar;
        }
    }
    
  2. Your loop to iterate through the segments starts with 0. Multiple segments are numbered starting with 1, though.

If you look at your input message it'll be like this:

NK1|1| ...
NK1|2| ...
NK1|3| ...

Even though javascript arrays start with zero. Yes, it's confusing.

I don't recognize:

$('NoK')

... so I'm not sure what you're doing. But I may just be having a slow morning.

user11583
  • 431
  • 3
  • 11
  • what are the seg functions other than name() please can you give us some documentation link about it thanks – Fadi Aug 01 '18 at 09:48