Stuart Byrne · Jun 4, 2020

How to Count the Number of OBX groups in a message using objectscript alone

Dear All, 

I am currently working on a project to record ORU^R01 into a global for a particular set of tests.

I have multiple OBX repeats that need to go into a specific fields withing one row/ record on the global.

I am having trouble finding a way to either count or loop though the OBX's correctly in ObjectScript often with the counter being undefined.

I have done this this way, as I am building upon an existing class to populate this global.

The latest iteration of my obx looping code is below:


// handle repeating obx's by setting count of value    
            set segcnt = pMsg.SegCountGet()
            ///count no of obx repeats
            for segmentIndex = 1:1:segcnt{
               IF pMsg.FindSegment("OBX",segmentIndex,.sc)='"",$$$ISOK(sc) {
                    set obxcnt = obxcnt+1}
            for obxIndex=1:1:obxcnt {
                    // Check for OBX-3.1 = "CodeA" f
                    IF GetValueAt("OBX("_obxIndex_"):3.1") = "SARS" {
                        set CovRes.Ward = "CodeA"
                        set tResultAB = $ZSTRIP(GetValueAt("OBX("_obxIndex_"):5(1)"),"<>W")
                      IF tResultAB = "Positive" {
                            set CovRes.TotalAbPositive = 1
                       elseif tResultAB = "Negative" {
                            set CovRes.TotalAbPositive = 0
                    // Check for OBX-3.1 = "CodeB" 
                    elseIF GetValueAt("OBX("_obxndex_"):3.1") = "CodeB" {
                        set tResultAB = $ZSTRIP(GetValueAt("OBX("_obxIndex_"):5(1)"),"<>W")
                        If tResultAB = "Positive" {
                            set CovRes.IgGPositive = 1

 Please could someone assist.



0 784
Discussion (8)3
Log in or sign up to continue

Hello Stuart,

Your elseif appears to have a typo of obxIndex:

elseIF GetValueAt("OBX("_obxndex_"):3.1") = "SARD" {

Besides that, you can use * to count fields rather than loop through and manually count them.

Hope that helps.

Thanks Vic,

I fixed the mistake, but am still having issues.

I tried set obxcnt = pMSg.GetValueAt("OBX(*)"), but the counter doesn't get populated.

How exactly is this looping code being implemented?

If I try msg.GetValueAt("OBX(*)") on an ADT_A01 for example I am able to count the OBXs. If your counter is not getting populated it suggests to me that the message's doctype may not be set or you might be using an HL7 schema that doesn't match the virtual property path.

Thanks Vic,

The incoming messages have these set already:

  DocTypeCategory 2.4
  DocTypeSecondary ORU_R01


pMsg is set to EnsLib.HL7.Message, which  information should be populated when the method is called right?



Disclaimer:  I'm by far not an expert in Ensemble/Cache

Since your inbound is set to DocType is 2.4:ORU_R01, below is the segment path for OBX

Schema Category: 2.4
Message Structure: ORU_R01
Segment Structure: OBX

Path you followed to get to this Segment Structure:  PIDgrpgrp().ORCgrp().OBXgrp().OBX

You will need to set the index on the PIDgrpgrp and ORCgrp or if you always receive 1 PID segment and 1 ORC segment, then those values can be set to 1.  I ran the following code snippet using 2.4 ORU DocType and obxCounter returned the OBX segment count.

 set obxCounter=source.GetValueAt("PIDgrpgrp("_(1)_").ORCgrp("_(1)_").OBXgrp("_("*")_")")

Hi Kevin,

I had gone down your route in the end. 

I had been using the FindSegment method from the EnsLib.HL7.Message, which uses the 2nd input variable, however I was having issues implementing the key byref.

Creating my own counter using the same as you suggested with   

"set obxCounter=source.GetValueAt("PIDgrpgrp("_(1)_").ORCgrp("_(1)_").OBXgrp("_("*")_")") "  and then using the full HL7 segment structure to retrieve the fields removed all woes.

Thank you Vic for your help too, I think my use of the FindSegment method was causing my issues.

Glad you were able to figure this out.  As an additional note, the syntax Kevin suggested is a bit unusual as it is concatenating parenthetical sections. It shouldn't be necessary to do that - when you try to concatenate "(1)" you are just concatenating "1".

The following both work identically:



Good to know...  I just base my objectscript off the DTL Intermediate Routine code generated by Ensemble.