Splitting One Message into Many¶
Some interfaces receive a single message that bundles several logical records, an ORU or ORM carrying multiple ORC groups, for example. This recipe takes one inbound HL7 message and produces one record per ORC group. Which delivery strategy you use depends on what the downstream system expects:
- Separate messages: each record leaves this channel as its own message (its own MLLP frame or file), using
qie.spawnNewMessage. - One BHS/BTS batch: the records travel together in a single HL7 batch message that the downstream system unbundles.
Both start the same way: capture the shared header once and pull each ORC group into an array. MSH[group=!ORC] returns the leading segments (MSH, EVN, PID, PV1, …) up to but not including the first ORC; ORC[group=!ORC] selects an ORC together with everything that follows it up to the next ORC, and getAllNodes returns one array element per group. See HL7 - Node Path Syntax (HPath) for the group= filter.
Build a new message for each group, then hand each one to qie.spawnNewMessage. A spawned message is queued to the next node(s) and is not processed again in the current node, so the original working message must be discarded once every group has been spawned.
var header = source.getNode('MSH[group=!ORC]');
var orcGroups = source.getAllNodes('ORC[group=!ORC]');
for (var i = 0; i < orcGroups.length; i++) {
var splitMessage = qie.parseHL7String(header + '\r' + orcGroups[i]);
// Give each message a unique MSH-10 so the receiver treats them as distinct.
splitMessage.setNode('MSH-10', source.getNode('MSH-10') + '-' + (i + 1));
qie.spawnNewMessage(splitMessage);
}
message.discard();
See Spawn New Message for the binding reference.
Warning
Place this in a mapping node whose pass connection leads to the destination, and always call message.discard() after the loop. The spawned messages flow to the next node(s); the original combined message would otherwise also reach the destination, producing one extra, unsplit message.
In the Test window, spawned messages are listed separately from the primary message. Step or play the sample and inspect the spawned-message view to confirm each group became its own message. At runtime each spawned message is queued, processed, and delivered independently, so a failure in one does not stop the others.
Keep the records in one message and wrap them in a Batch Header Segment (BHS) and Batch Trailer Segment (BTS). This is the step the Level 1 training material stops short of. It builds the multi-MSH body but leaves off the batch framing that actually sends it. QIE does not add the framing on send, so the script must.
Build the BHS from the inbound MSH, append each record, and close with a BTS whose first field is the message count. Because every header + ORC group block begins with its own MSH, the batch contains one complete HL7 message per record.
var header = source.getNode('MSH[group=!ORC]');
var orcGroups = source.getAllNodes('ORC[group=!ORC]');
// Batch header. BHS-3..BHS-6: sending/receiving application and facility; BHS-7: creation date/time.
var bhs = 'BHS|^~\\&|' +
source.getNode('MSH-3') + '|' + source.getNode('MSH-4') + '|' +
source.getNode('MSH-5') + '|' + source.getNode('MSH-6') + '|' +
qie.formatDate('yyyyMMddHHmmss');
// Each record is a complete message: shared header + one ORC group.
var body = '';
for (var i = 0; i < orcGroups.length; i++) {
body += header + '\r' + orcGroups[i] + '\r';
}
// Batch trailer. BTS-1: number of messages in the batch.
var bts = 'BTS|' + orcGroups.length;
message = qie.createHL7Message();
message.setNode('/', bhs + '\r' + body + bts);
// Give each message in the batch a unique MSH-10.
for (var j = 1; j <= message.getCount('MSH'); j++) {
message.setNode('MSH[' + j + ']-10', message.getNode('MSH[' + j + ']-10') + '-' + j);
}
The batch goes to the destination as one message, and the downstream system unbundles it into individual messages. If that next hop is itself a QIE channel, configure its source node with Group By = Message. It splits on each MSH and ignores the BHS/BTS framing.