Question
· Oct 6, 2020

Programmatically define new message and segment without %Clone

Hi

 

I would like to create a new message of a particular type and append various segments programmatically.  I have started with the following snippet.  What I am currently finding difficult is to define a segment of a particular segment structure.  I would like to append a pid segment of 2.3.1 to my message.  I know I could you the importfromstring but I wanted to know whether there was a better way of achieving this.

 

Set tOutputMessage = ##class(EnsLib.HL7.Message).%New()
Set target.DocType="2.3.1:DFT_P03"

#Dim tPID As EnsLib.HL7.Segment

tPID.SegTypeName ="PID"

Any help would be much appreciated.

 

Kind Regards,

 

Salma

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

Just an update I have tried the following however, the MSH segment seems to be getting overwritten, any ideas why?

s tMsg = ##class(EnsLib.HL7.Message).%New()
s tMsg.DocType ="2.3.1:DFT_P03"
s tSeg = ##class(EnsLib.HL7.Segment).%New()
set tSeg.DocType="2.3.1:PID"
s tsc = tMsg.SetValueAt("abc","MSH:9")
s tsc = tSeg.SetValueAt("PID","1")
s tsc = tSeg.SetValueAt("TEST","3.1")
set tsc=tMsg.SetSegmentAt(tSeg,2)
w tMsg.OutputToString()

I have also tried set tsc=  tMsg.AppendSegment(tSeg) which seems to be overwriting the MSH segment.

Any help would be great.

You need to have the inbound message sent to this function.  Let's call it pRequest

#dim tMshSeg As EnsLib.HL7.Segment
#dim tPidSeg As EnsLib.HL7.Segment
Set tNewMsg = ##class(EnsLib.HL7.Message).%New()
Set tNewMsg.DocType = "2.3.1:DFT_P03"
Set tMshSeg = pRequest.getSegmentByPath("MSH", .tStatus) $$$ThrowOnError(tStatus)
Set tPidSeg = pRequest.getSegmentByPath("PID", .tStatus) $$$ThrowOnError(tStatus)
Set tStatus = tNewMsg.setSegmentByPath(tMshSeg, "MSH") $$$ThrowOnError(tStatus)
Set tStatus = tNewMsg.setSegmentByPath(tPidSeg, "PID") $$$ThrowOnError(tStatus)