Search code examples
javascripthl7mirth

Can't seem to move segment into repeating field


I have a piece of code that I'm trying to get to work on an interface. Basically we take some fields and drop into other segments. The problem seems to be that it leaves the data where it is instead of moving it to the indexed PID segment. Also the CP variable is returning 'undefined' for some reason.

var i = msg['PID']['PID.13'].length(); 
var homeNum;
var netNum;
var cpNum;

while(i--)
{
if (msg['PID']['PID.13'][i]['PID.13.2'].toString() == "PRN")
{
    homeNum = msg['PID']['PID.13'][i]['PID.13.9'];
}

if (msg['PID']['PID.13'][i]['PID.13.2'].toString() == "NET")
{
    netNum = msg['PID']['PID.13'][i]['PID.13.4'];
}

if (msg['PID']['PID.13'][i]['PID.13.2'].toString() == "CP")
{
    cpNum = msg['PID']['PID.13'][i]['PID.13.9'];
}

msg['PID']['PID.13'][i] = "";
}

msg['PID']['PID.13'][0]['PID.13.1'] = homeNum;
msg['PID']['PID.13'][0]['PID.13.4'] = netNum;
msg['PID']['PID.13'][1]['PID.13.1'] = cpNum;

Sample HL7 msg I am using before transforms (from our test system, NOT live data)

It should resemble this instead:

|9999999999^^^test@test.com~99999999999~~~|

Any ideas/pointers on why it's not moving?


Solution

  • You are missing a toString() when you set the variables. A typical Mirth thing, because you get the E4X object back in the variable instead of the value you expected.

    In addition to this, you should check the variables for undefined values before setting them on the new structure because otherwise you end up with "undefined" in the fields.

    This is a working solution:

    var i = msg['PID']['PID.13'].length(); 
    var homeNum;
    var netNum;
    var cpNum;
    
    while(i--)
    {
    	if (msg['PID']['PID.13'][i]['PID.13.2'].toString() == "PRN")
    	{
    	    homeNum = msg['PID']['PID.13'][i]['PID.13.9'].toString();
    	}
    
    	if (msg['PID']['PID.13'][i]['PID.13.2'].toString() == "NET")
    	{
    	    netNum = msg['PID']['PID.13'][i]['PID.13.4'].toString();
    	}
    
    	if (msg['PID']['PID.13'][i]['PID.13.2'].toString() == "CP")
    	{
    	    cpNum = msg['PID']['PID.13'][i]['PID.13.9'].toString();
    	}
    
    	msg['PID']['PID.13'][i] = "";
    }
    
    if(homeNum != null) msg['PID']['PID.13'][0]['PID.13.1'] = homeNum;
    if(netNum != null) msg['PID']['PID.13'][0]['PID.13.4'] = netNum;
    if(cpNum != null) msg['PID']['PID.13'][1]['PID.13.1'] = cpNum;