No announcement yet.

Iteration from ~ to new line

  • Filter
  • Time
  • Show
Clear All
new posts

  • Iteration from ~ to new line

    Hi! I'm fairly new to Mirth but have been able to use it quite successfully. I currently have an OBR line as such:

    OBR|1|LB00000022||FE^Fe(TIBC)~FECAL^FECALYSIS~URIN ES^URINALYSIS (HMO)~CBCP^CBC PLATELET|R|20190614142600||||||||||^|OPD^|LB|OPD^| 3654335~3654336~3654337~3654338||||||||||

    I plan on making it so instead of ~ separating the various OBR4 and OBR20, they will make new OBRs like so:
    OBR|1|LB00000022||FE^Fe(TIBC)|R|20190614142600|||| ||||||^|OPD^|LB|OPD^|3654335||||||||||
    OBR|2|LB00000022||FECAL^FECALYSIS|R|20190614142600 ||||||||||^|OPD^|LB|OPD^|3654336||||||||||
    OBR|3|LB00000022||URINES^URINALYSIS (HMO)|R|20190614142600||||||||||^|OPD^|LB|OPD^|365 4337||||||||||
    OBR|4|LB00000022||CBCP^CBC PLATELET|R|20190614142600||||||||||^|OPD^|LB|OPD^| 3654338||||||||||

    I'm sure this should be a fairly easy thing to do but I am finding it hard to look for a javascript that will help iterate this. Scoured the forums and all I was able to find was a code similar to this:

    for each (obr4 in msg.PID['OBR.4']) {['OBR.4.1'].toString());['OBR.4.2'].toString());

    Not sure where to go from there honestly.

    Thank you all in advance!

    P.S. I may also need to figure out how to make OBR1 change numbers depending on line number. By that I mean line 1 its value = 1, line 2 value =2, etc. It's actually not as big a priority as iterating ~ to new lines though hehe

  • #2
    First you need to store OBR.2 and OBR.3

    var OBR2 = msg['OBR']['OBR.2'].toString();
    var OBR3 = msg['OBR']['OBR.3'].toString();

    Then you need a counter

    var OBRCount = 0;


    for each(obr4 in msg['OBR']['OBR.4'])
    msg['OBR'][OBRCount++]['OBR.4'] = obr4.toString();


    • #3
      This assumes there won't be more than 1 OBR segment in a message to begin with. If that's a possibility it will have to be adjusted.

      // store original OBR
      var obr = msg.OBR;
      // get number of repetitions in OBR-4. Assume same number of repetitions in OBR-20
      var numRepetitions = obr['OBR.4'].length();
      // nothing to do if not at least 2 repetitions
      if (numRepetitions > 1) {
          // create new empty XMLList to hold results
          var obrList = <></>;
          for (var i = 0; i < numRepetitions; i++) {
              // copy original OBR segment
              var newOBR = obr.copy();
              // update setID
              newOBR['OBR.1']['OBR.1.1'] = i + 1;
              // replace repeating OBR fields with value for current iteration
              newOBR['OBR.4'] = newOBR['OBR.4'][i];
              newOBR['OBR.20'] = newOBR['OBR.20'][i];
              // add to result list
              obrList += newOBR;
          // replace original obr with new list
          msg.OBR = obrList;