· Oct 13, 2020

Advice about HL7 Message creations for repeating loop items


I am trying to create a HL7 message (REF_I12 message) using variables extracted from a Dynamic Object and need some advice about segment creation issues that I am having. Some background info: I receive a JSON response from an API call and used %FromJSON to convert the response to an Dynamic Object. Some of the items in the response are repeated such as NOKName, NOKrelationship.

Using the methods suggested in this post (, I have been able to extract the values from the response and set them to be variables. For example like so:

set NOKName = object.%Get(i).referral.nextOfKin.%Get(k).name

As part of the loop to extract the value, a command has been entered to create a new NTE segment like so:

d PBTRefMessage.SetValueAt("1", "NTE():SetIDNTE")
d PBTRefMessage.SetValueAt("P", "NTE():SourceofComment")
d PBTRefMessage.SetValueAt("NOKName", "NTE():Comment(1)")

As the NOKname has multiple values from the response, the segment in the message created only shows the last NOKName value in the loop and does not produce an NTE segment/value for each NOKName which is what I would like it to do. I have tried a few different things with the NTE ID substituted and still not able to get a different answer. The final output only has one of the NOKName values.

The schema for the REF_I12 message is as follows:

Raw Definition


Any suggestions on how I can get each NOKName value to have it's own NTE segment created/or added as an additional value in the existing segment as an second(or third etc) comment in the same segment?

Discussion (3)2
Log in or sign up to continue

Hi Vic,

Thank you for this. I used the code that Eduard had referenced for the Iterate method and was able to use a Trace to see all the relevant values and keys. The code I have used so far is as follows including the traces are:

Method Iterate(object As %DynamicAbstractObject, level = 0, path = "object")
{     set iterator = object.%GetIterator()
        set indent = $j("", level * 4)
    #dim iterator As %Iterator.Array
    set iterator = object.%GetIterator()
    while iterator.%GetNext(.key, .value) {
        set type = object.%GetTypeOf(key)
        set key=key
        $$$TRACE("key: "_key)
        set type=type
        $$$TRACE("type: "_type)
        if $classname(object) = "%Library.DynamicArray" {
            set newPath = path _ ".%GetAt(" _ key _ ")"
        else {
            if $zname(key, 6) = 1 {
                set newPath = path _ "." _ key
            else {
                set newPath = path _ ".""" _ key _ """"
        set path1=newPath
        $$$TRACE("path: "_path1)
        if $isObject(value) {
            do ..Iterate(value, level + 1, newPath)
        else {
            set value=value
            $$$TRACE("Value: "_value)

I have come across some problems with the HL7 message generation within the iterate method. I am unsure as to where to place the code to generate message and how to code the relevant keys/value in the relevant segments.  I tried to place the code as below :

while iterator.%GetNext(.key, .value) {

 Set RefMess = ##class(EnsLib.HL7.Message).%New()
Set RefMess.DocType="2.4Epic:REF_I12"

but this code caused for multiple messages to be created and I was unsure what the trigger was. I have tried to place the code outside the while loop and it still produced less than expected messages as I would expect a message to be triggered for each patient within my response. Do you have any suggestions on where the message generation code should go and also how to set the value from the response into a particular segment?