How to insert OBR segment before each OBX? #6278
-
I would appreciate some assistance with the following issue: I have an HL7 ORU message that has one OBR segment and 3 OBX segments. There may also be NTE segments that belong to each individual OBX. But for purposes of our software, we need those OBX segments to present as 3 separate orders, meaning they each need an OBR segment preceding it. So basically I may have something like this: OBR|1 And I need it to be this: OBR|1 The actual contents of the extra OBR segments don't even matter, they can be a copy of the original OBR segment with _1 added to OBR-4.1. |
Beta Was this translation helpful? Give feedback.
Replies: 4 comments
-
This is an untested copy/paste from the Interop Guru GPT.
Here is a link to the conversation.
https://chatgpt.com/g/g-XG1Dzv32o-interop-guru/c/24148b6d-ef18-4d65-ab3e-a805f98a4a1e
JavaScript Transformer Code:
// Parse the incoming messagevar msg = new XML(connectorMessage.getRawData());
// Get all OBR and OBX segmentsvar obrSegments = msg.OBR;var
obxSegments = msg.OBX;
// Create a new empty HL7 messagevar newMessage = <HL7Message></HL7Message>;
// Iterate over each OBR and OBX segmentfor (var i = 0; i <
obxSegments.size(); i++) {
// Create a copy of the original OBR segment
var newOBR = obrSegments[0].copy();
// Modify OBR-4.1 to append the order number (i+1)
var obr4 = newOBR['OBR.4']['OBR.4.1'].toString();
newOBR['OBR.4']['OBR.4.1'] = obr4 + "_" + (i + 1);
// Add the new OBR segment to the new message
newMessage.appendChild(newOBR);
// Add the corresponding OBX segment
newMessage.appendChild(obxSegments[i].copy());
// Check if there are any NTE segments following this OBX segment
var nextSibling = obxSegments[i].getNextSibling();
while (nextSibling != null && nextSibling.getName() == 'NTE') {
// Add the NTE segment to the new message
newMessage.appendChild(nextSibling.copy());
nextSibling = nextSibling.getNextSibling();
}
}
// Convert the new message back to a stringvar transformedMessage =
SerializerFactory.getHL7Serializer().fromXML(newMessage);
// Return the transformed messagereturn transformedMessage;
Explanation:
1.
*Parsing the Message:*
- The incoming HL7 message is parsed using new
XML(connectorMessage.getRawData()). This converts the raw HL7 data
into an XML format that Mirth can easily manipulate.
2.
*Extracting OBR and OBX Segments:*
- The script extracts all OBR and OBX segments using msg.OBR and msg.OBX,
which return arrays of these segments.
3.
*Creating a New HL7 Message:*
- A new empty HL7 message is initialized using <HL7Message></HL7Message>.
4.
*Iterating Through OBX Segments:*
- The script loops through each OBX segment, and for each OBX:
- It creates a new OBR segment by copying the first OBR segment
using .copy().
- Modifies the OBR.4.1 field to append a unique identifier (e.g.,
"_1", "_2", etc.).
- Appends the new OBR segment to the new message.
- Appends the corresponding OBX segment to the new message.
5.
*Handling NTE Segments:*
- After each OBX segment, the script checks if there are any NTE
segments directly following it using getNextSibling(). If there are,
it appends them to the new message.
6.
*Reconstructing the HL7 Message:*
- The script converts the XML-based new message back to an HL7 string
using SerializerFactory.getHL7Serializer().fromXML(newMessage).
7.
*Returning the Transformed Message:*
- Finally, the transformed HL7 message is returned.
Result:
This script takes advantage of Mirth Connect’s built-in XML manipulation
capabilities, ensuring that each OBX segment is preceded by a separate OBR
segment, and preserves any NTE segments associated with each OBX.
You can add this script to the transformer section in Mirth Connect, and it
will handle the message transformation as described.
|
Beta Was this translation helpful? Give feedback.
-
Thank you so much, I had to tweak some things (our Mirth didn't like the syntax) but now it seems to be doing exactly what I need! Here's what it looks like after the changes:
|
Beta Was this translation helpful? Give feedback.
-
Glad it worked! Thanks for posting your final solution.
…On Fri, Aug 9, 2024 at 5:50 PM et817 ***@***.***> wrote:
Thank you so much, I had to tweak some things (our Mirth didn't like the
syntax) but now it seems to be doing exactly what I need! Here's what it
looks like in my transformer:
// Get all OBR and OBX segments
var obrSegments = msg.OBR;
var obxSegments = msg.OBX;
// Create a new empty HL7 message
var newMessage = ;
// Iterate over each OBR and OBX segment
//for each (obx in msg.OBX) {
for (var i = 0; i < obxSegments.length(); i++) {
var newOBR = obrSegments[0].copy();
// Modify OBR-4.1 to append the order number (i+1)
var obr4 = newOBR['OBR.4']['OBR.4.1'].toString();
newOBR['OBR.4']['OBR.4.1'] = obr4 + "_" + (i + 1);
// Add the new OBR segment to the new message
newMessage.appendChild(newOBR);
// Add the corresponding OBX segment
newMessage.appendChild(obxSegments[i].copy());
for each (nte in getSegmentsAfter(msg, obxSegments[i],'NTE',false,'OBX')) {
newMessage.appendChild(nte.copy());
}
}
// Convert the new message back to a string
msg = SerializerFactory.getHL7Serializer().fromXML(newMessage);
—
Reply to this email directly, view it on GitHub
<#6278 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/APRXWDZY4LHX67JSM4PKVJLZQU2T5AVCNFSM6AAAAABMFLTIUWVHI2DSMVQWIX3LMV43URDJONRXK43TNFXW4Q3PNVWWK3TUHMYTAMRZGE3DMOA>
.
You are receiving this because you commented.Message ID:
***@***.***
com>
--
Best,
Kirby Knight
| 231.735.4650 | ***@***.***
|
Beta Was this translation helpful? Give feedback.
-
I already posted and deleted a similar comment. I realized it had an issue and wanted to fix it. This should work, and I think it's easier to follow than the AI solution. // if an OBX segment is not preceded by an OBR segment, then insert a copy of the first
// OBR segment before the OBX segment
for each (var obx in msg.OBX) {
var preceding = msg.child(obx.childIndex() - 1)
if (preceding.localName() == 'OBR') continue
msg.replace(obx.childIndex(), msg.OBR[0].copy() + obx)
}
// update the OBR-1 and OBR-4.1 values for every OBR segment after the first
var obrSegments = msg.OBR
for (var i = 1; i < obrSegments.length(); i++) {
msg.OBR[i]['OBR.1']['OBR.1.1'] = i + 1
msg.OBR[i]['OBR.4']['OBR.4.1'] = msg.OBR[i]['OBR.4']['OBR.4.1'].toString() + '_' + i
} |
Beta Was this translation helpful? Give feedback.
I already posted and deleted a similar comment. I realized it had an issue and wanted to fix it. This should work, and I think it's easier to follow than the AI solution.