javascriptmirthmirth-connect

How do I transform an HL7 message with two segments in Mirth


I have an issue with the trandformation og an HL7 message to XML in Mrth.

The problem I have is that the message segment has two instances in it which I now need to to seperate into two xml tage.

so the HL7 segement looks like this:

PID|1|16^^^MEDAVIS^PI|||MyTest^TEST 2^T^^MISS~Test2^^^^^^B|00MemberCode|19921106|M|||MANNING ROAD^^DURBAN^^4001^ZA||0313017352~072363395^^CP~^NET^Internet^TEST@GMAIL.COM|0313017352~072363395^^CP~^NET^Internet^TEST@GMAIL.COM|||||123456PatDepCode|||||||ZA||||N

So the issue is with the PID segment where there are multiple tags in segement 5 (name) and then also in segment

The transformation I have been using with a different vendor feeding information to our system only used one tage in the segment and looked like this:

tmp['Patient']['Name']= msg['PID']['PID.5']['PID.5.2'].toString();
tmp['Patient']['Surname'] =  msg['PID']['PID.5']['PID.5.1'].toString();

The problem is that with the two segments in the code I end up with this mess

        <Name>&lt;PID.5.2&gt;TEST 2&lt;/PID.5.2&gt;&lt;PID.5.2/&gt;</Name>
    <Surname>&lt;PID.5.1&gt;TEST G4M Nachname&lt;/PID.5.1&gt;&lt;PID.5.1&gt;NameOfBirth&lt;/PID.5.1&gt;</Surname>

How do I alter the ransform so the forst segment would be something likename 1 and surname1 and the second segment name2 and surname2

UPDATE: I have updated the transform code and it gives the right result but their is still an error message, despite it producing the reuslt using the code with the error.

    <PID.5>
        <PID.5.1>TEST G4M Nachname</PID.5.1>
        <PID.5.2>TEST 2</PID.5.2>
        <PID.5.3>T</PID.5.3>
        <PID.5.4/>
        <PID.5.5>MISS</PID.5.5>
    </PID.5>
    <PID.5>
        <PID.5.1>NameOfBirth</PID.5.1>
        <PID.5.2/>
        <PID.5.3/>
        <PID.5.4/>
        <PID.5.5/>
        <PID.5.6/>
        <PID.5.7>B</PID.5.7>
    </PID.5>

So the adjusted transform looks like:

/*patient name seperation*/
var segmentlength = msg['PID']['PID.5']['PID.5.2'].toString().length;
var tildeappears = msg['PID']['PID.5']['PID.5.2'].toString().indexof('~');
    tmp['Patient']['Name'] = msg['PID']['PID.5']['PID.5.1'].toString().substring(0,tildeappears-1);
    tmp['Patient']['Name1']= msg['PID']['PID.5']['PID.5.1'].toString().substring(tildeappears+1,segmentlength)

But when run Mirth returns this error:

Transformer error ERROR MESSAGE: Error evaluating transformer com.mirth.connect.server.MirthJavascriptTransformerException: CHANNEL: AGFA_DFT_PayloadBuilderV3 CONNECTOR: OutputLog SCRIPT SOURCE: TRANSFORMER SOURCE CODE: 604: tmp['Patient']['MRN'] = msg['PID']['PID.3']['PID.3.1'].toString(); 605: 606: /patient name seperation/ 607: 608: var segmentlength = msg['PID']['PID.5']['PID.5.2'].toString().length; 609: tildeappears = msg['PID']['PID.5']['PID.5.2'].toString().indexof('~'); 610: tmp['Patient']['Name'] = msg['PID']['PID.5']['PID.5.2'].toString().substring(0,tildeappears-1); 611: tmp['Patient']['Name1']= msg['PID']['PID.5']['PID.5.2'].toString().substring(tildeappears+1,segmentlength); 612: tmp['Patient']['Surname'] = msg['PID']['PID.5']['PID.5.1'].toString(); 613: LINE NUMBER: 609 DETAILS: TypeError: Cannot find function indexof in object TEST 2. at 40699b8f-7c07-4eaf-8d54-e6f423be853b:609 (doTransform) at 40699b8f-7c07-4eaf-8d54-e6f423be853b:792 (doScript) at 40699b8f-7c07-4eaf-8d54-e6f423be853b:794 at com.mirth.connect.server.transformers.JavaScriptFilterTransformer$FilterTransformerTask.doCall(JavaScriptFilterTransformer.java:154) at com.mirth.connect.server.transformers.JavaScriptFilterTransformer$FilterTransformerTask.doCall(JavaScriptFilterTransformer.java:119) at com.mirth.connect.server.util.javascript.JavaScriptTask.call(JavaScriptTask.java:113) at java.util.concurrent.FutureTask.run(Unknown Source) at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) at java.lang.Thread.run(Unknown Source)


Solution

  • So after researching and trying various solutions I came up with a very easy solution:

    tmp['Patient']['Name'] = msg['PID']['PID.5'][0]['PID.5.2'].toString();
    tmp['Patient']['Name1'] = msg['PID']['PID.5'][1]['PID.5.2'].toString();
    tmp.Patient.Surname = msg['PID']['PID.5'][0]['PID.5.1'].toString();
    tmp.Patient.Surname1 = msg['PID']['PID.5'][1]['PID.5.1'].toString();
    

    The only trick to it is to remember that the index starts from 0. The formatting of the string can eb done either of the two ways above, I included an example of each of the formatting ways I tried and found worked.