ED Coder · Aug 30, 2019

How to get number of segments in a hl7 message and then pull a field value from the last segment

Hi, Is there a way to count the number of segments in a HL7 Mesage? I tried the examples from the other answers but nothing works for me.

I am writing a function to get the last OBX segment field value 5. Below is a sample screenshot of what I want.

The number of obx segments can change, so I want to count the last obx segment and then get the field 5 value.

Would appreciate some guidance on this.

0 1,890
Discussion (10)1
Log in or sign up to continue

Have a look at this documentation on Virtual Property Path syntax.

Essentially, if you specify an asterisk "*" inside the parentheses for a repeating segment it will return a count of how many of those segments exist in the document.

<assign value='source.{PID:3("*")}' property='pid3Count'/>

In this example, the number of repetitions that exist in PID:3 will be stored in the variable pid3Count.

You could then use source.{PID:3(pid3Count)} to refer to the last item in PID:3.

Hi Marc, thank you so much. Actually I had to edit my question. My aim is to get the 5th field value from the last OBX segment in my HL7 message. Because I can have varying number of OBX segments but I know that the value I want is in the last OBX segment. 

So I thought if I could get the number of segments in a message, and then index that to get the last segment I could get the value I want.

Is there a better way to do that?

Sorry for the confusion. Appreciate your help

Yes, you can do this using the same approach.

You can set a temp variable:
lastOBX = source.{OBX("*")}

With your sample message, lastOBX would now contain the number 2. Then you can use the variable lastOBX to refer to the last OBX segment.

To access OBX:5 in the last OBX it would look like this:

Thank You Marc, I tried that in my function, and its giving me the following error

No such function found




Have you tried the getSegmentCount method?

You can pass me the file if you wish, can set up a simple code for you

This will return the 5'th field of the last OBX segment using the GetValueAt method as Marc stated. set value = pRequest.GetValueAt("OBX("_pRequest.GetValueAt("OBX(*)")_"):5")

Thank You Guys, I found the method, and used your advice to getValueat(), this solved my question.

Hi, solved this by using the following function:

pRequest.SegCountGet() -> This gave me the number of segments in the HL7 Message, and because I knew that my required value is in the last segment in the 5th field, I could retrive it then using GetValueat()

As long as you can count on the message spec never changing, this works fine.

If the originating system ever adds an NTE segment after the OBX, you'll have an issue. The message would still be valid from an HL7 standards perspective, though.