Use HL7 group processing so each OBR group is handled separately. In QIE, the documented pattern is to get all OBR groups with message.getAllNodes('OBR[grp=!OBR]'), parse each group as its own HL7 message, count that group’s OBX segments, renumber OBX-1 from 1..n, and then write the updated group back into the original message. That exact OBR-group pattern is shown in the docs and the tutorial also shows renumbering OBX-1 inside a loop with message.setNode('OBX-1', ...) .
Example mapping script:
var obrGroups = message.getAllNodes('OBR[group=!OBR]');
for (var i = 0; i < obrGroups.length; i++) {
var obrGroup = qie.parseHL7String(obrGroups[i]);
var obxCount = obrGroup.getCount('OBX');
for (var j = 1; j <= obxCount; j++) {
obrGroup.setNode('OBX-1', j+'', j);
}
message.setNode('OBR[grp=!OBR]', obrGroup, (i + 1));
}How it works;
- OBR[group=!OBR] returns each OBR plus its child segments until the next OBR, so each array element is one OBR group
- qie.parseHL7String(...) lets you treat that group as an HL7 message and access its local OBX segments
- obrGroup.getCount('OBX') gets the number of OBX segments in that OBR group
- obrGroup.setNode('OBX-1', j+'', j) sets OBX-1 to 1, 2, 3, etc. within that group
- message.setNode('OBR[grp=!OBR]', obrGroup, i + 1) puts the modified group back in the correct OBR-group position