Skip to content

How to insert OBR segment before each OBX? #6278

Answered by tonygermano
et817 asked this question in Q&A
Discussion options

You must be logged in to vote

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[

Replies: 4 comments

Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Answer selected by pacmano1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants