connect
connect copied to clipboard
Unable to access XML nodes by name in a code template when strict parsing is enabled
Describe the bug
Many of our channels extract the patient ID for custom metadata columns and destination mappings. We have recently attempted to move the repeated bit of code which retrieves the id from each source transformer and put it into a code library whereby each source transformer can call and then store in the channel map. However once moving the code (show below) into a code template we are no longer able to get the PID section from the msg object.
var ourid = "";
for (var i = 0; i < getArrayOrXmlLength(hl7_msg['PID']['PID.3']); i++) {
var assigningAuthority = hl7_msg['PID'][i]['PID.3']['CX.4']['HD.1'].toString()
if (assigningAuthority == "MRN")
{
try{
ourid = hl7_msg['PID'][i]['PID.3']['CX.1'].toString()
} catch (e) {
//logger.error(logPrefix + 'Error : ' + e.message);
ourid = "";
}
}
}
logger.info('extracted Ourid : ' + ourid);
return ourid;
I believe the code was taken from the generated JS when using the drag and drop functionality in mirth connect and works fine when run inside the source transformer. You are able to navigate the the XML nodes if you use the .children()[0]
notation however this is unfeasible as a long term solution.
To Reproduce
Create a new channel and set data types to be HL7 v2 with strict parsing checked and save
Add a new code library called Test Library and give it access to the new channel and paste the following code to retrieve the patient ID
function GO() {
var ourid = "";
for (var i = 0; i < getArrayOrXmlLength(msg['PID']['PID.3']); i++) {
var assigningAuthority = pid[i]['PID.3']['CX.4']['HD.1'].toString()
if (assigningAuthority == "MRN")
{
try{
ourid = pid[i]['PID.3']['CX.1'].toString()
} catch (e) {
//logger.error(logPrefix + 'Error : ' + e.message);
ourid = "";
}
}
}
logger.info('extracted OurId : ' + ourid);
return ourid;
}
Go back into the test channel and add the following into the source transformer
var ourIdAt = GO();
logger.info('(CHANNEL NAME) SOURCE TRANSFORMER - Adding OurIdAlt = ' + ourIdAt + ' to channel map');
channelMap.put("ourIdAt", ourIdAt);
In the channel writer destination add the ourIdAt varible so you can see the output.
Steps to reproduce the behavior:
- Deploy the channel
- Clear current dashboard server logs (for visibility)
- Send a HL7 message into the test channel and view the logs/destination.
Expected behavior The patient ID should be mapped to our variable
Actual behavior The PID section is always returned as undefined and you are unable to navigate any of the nodes by name.
Environment (please complete the following information):
- OS: Windows Server 2019
- Java Distribution/Version openjdk 17.0.3
- Connect Version 4.0.1
Workaround(s)
in the channels data types if you check the "strip namespaces" toggle then you are able to access the nodes by name e.g. msg['PID']