javascriptmirth

NextGen Mirth: Loop through all OBR/OBX segments for output to Document Writer


I used Mirth about 10 years ago and realize I have forgotten pretty much everything and a lot of my resources are no longer available. I am looking to iterate through OBR and OBX segments to pull the OBR 4.2, 7.1, 16.2 to be used as a section header and OBX 3.2, 5.1, 7.1 fields to show below on the report.

Basically take this:

MSH|^~\&|LAB|LIS||EMR|202107201651||ORU^R01|082017045701|P|2.3|| |NE|NE
PID|1||M1302^^^MR||TEST^PATIENT^T^^||19670101|M||| 123 8th street^apt.22b^long beach^CA^90802^||(714)555-1212|(714)222-5555||||873lfif|4441113||||||||||
PV1|1|O|RGH^^^RGH^^^^||||999999^TEST^TEST^^^^^L||||||||||288^ALBRIGHT^MD^^^^^L ||V4735|||||||||||||||||||||||||201008201648|
ORC|RE||15175|||||||||288^ALBRIGHT^MD^^^^^L|
OBR|1||15175|CBC^COMPLETE BLOOD COUNT^L|R|202107200800|202107200800|||BB^RGH^^^^^^ ^^^^^^RGH||||201008201648|B|288^ALBRIGHT^MD^^^^^L| |||||20100820170246|||F|^|||||100.0||||||||||||||G A
OBX|1|NM|WBC^WBC^L||4.0|10\S\3/MM\S\3|3.6-11.1||||F|||202107200759|RGH|ADM
OBX|2|NM|RBC^RBC^L||5.0|10\S\6/MM\S\3|4.50-5.90||||F|||202107200758|RGH|ADM
OBX|3|NM|HGB^HGB^L||12.5|g/dL|12.0-18.0||||F|||202107200757|RGH|ADM
ORC|RE||15175|||||||||288^ALBRIGHT^MD^^^^^L|
OBR|2||15175|ALC^ALCOHOL,ETHYL^L|R|202107201650|20 1008201648|||BB^RGH^^^^^^^^^^^^RGH||||201008201648 |B|288^ALBRIGHT^MD^^^^^L||||||20100820170246|||F|^ |||||||||||||||||||GA
OBX|1|NM|ALC^ALCOHOL (ETHANOL)^L||36|mg/dL|0-32|H|||F|||201008201650|RGH|ADM

and make:

CBC 202107260800 ALBRIGHT
WBC 4.0 3.6-11.1
RBC 5.0 4.50-5.90
HGB 12.5 12.0-18.0
ALCOHOL, ETHYL 202107201650 ALBRIGHT
ALCOHOL (ETHANOL) 36 0-32

Any assistance is greatly appreciated.


Solution

  • You can play around with the html and css still, but here's how you can organize the data and loop over it in velocity. We start by looping over the OBR segments. Then we use the getSegmentsAfter function for getting the OBX segments associated with the current OBR segment. The function returns the results as an array of xml objects, which we then convert to plain js objects.

    The code template for the getSegmentsAfter function can be found here https://github.com/nextgenhealthcare/connect-examples/tree/master/Code%20Templates/Get%20Segments%20After%20a%20Particular%20Segment

    This works because javascript arrays implement the java.util.Collection interface, and javascript objects implement the java.util.Map interface, both of which velocity knows how to access.

    Transformer

    var reportData = [];
    for each (var obr in msg.OBR) {
        var  sectionHeader = {
            panelName: obr['OBR.4']['OBR.4.2'].toString().trim(),
            dateTime: obr['OBR.7']['OBR.7.1'].toString().trim(),
            providerName: obr['OBR.16']['OBR.16.3'].toString().trim() + ' ' + obr['OBR.16']['OBR.16.2'].toString().trim()
        };
        var sectionData = getSegmentsAfter(msg, obr, 'OBX')
            .map(function(obx) {
                return {
                    testName: obx['OBX.3']['OBX.3.2'].toString().trim(),
                    result: obx['OBX.5']['OBX.5.1'].toString().trim(),
                    unitOfMeasure: obx['OBX.6']['OBX.6.1'].toString().trim(),
                    referenceRange: obx['OBX.7']['OBX.7.1'].toString().trim(),
                    abnormalFlag: obx['OBX.8']['OBX.8.1'].toString().trim()
                }});
        reportData.push({
            header: sectionHeader,
            data: sectionData
        });
    }
    $co('reportData', reportData);
    

    Document Writer template

    <html>
    <head>
      <style type="text/css">
      div table {
        width: 100%;
        margin-bottom: 5px;
      }
      div table th, td {
        word-wrap: break-word;
        border: 1px solid lightgrey;
        text-align: left;
      }
      div {
        border: 1px solid grey;
        margin: 5px;
      }
      </style>
    </head>
    <body>
      <h1>Laboratory Results</h1>
    
      #foreach ($section in $reportData)
      <div>
      <table>
      <tr>
        <th>Panel Name</th><th>DateTime</th><th>Provider</th>
      </tr>
      <tr>
        <td>$section.header.panelName</td>
        <td>$section.header.dateTime</td>
        <td>$section.header.providerName</td>
      </tr>
      </table>
    
      <table>
      <tr>
        <th>Test</th><th>Result</th><th>Unit of Measure</th><th>Range</th><th>Flag</th>
      </tr>
        #foreach ($row in $section.data)
        <tr>
          <td>$row.testName</td>
          <td>$row.result</td>
          <td>$row.unitOfMeasure</td>
          <td>$row.referenceRange</td>
          <td>$row.abnormalFlag</td>
        </tr>
        #end
      </table>
      </div>
      #end
    </body>
    </html>
    

    Results

    results