On this page

core.hl7message

Home > @medplum/core > Hl7Message

Hl7Message class​

The Hl7Message class represents one HL7 message. A message is a collection of segments.

Signature:

export declare class Hl7Message 

Constructors​

Constructor

Modifiers

Description

(constructor)(segments, context)

Creates a new HL7 message.

Segment strings are not parsed until they are accessed via Hl7Message.getSegment(), Hl7Message.getAllSegments(), Hl7Message.header, or the Hl7Message.segments getter.

Properties​

Property

Modifiers

Type

Description

context

readonly

Hl7Context

header

readonly

Hl7Segment

Returns the HL7 message header.

segments

readonly

Hl7Segment[]

Returns all HL7 segments, parsing any unparsed segment strings on first access.

Prefer Hl7Message.getSegment() or Hl7Message.getAllSegments() when you only need a subset of segments; those methods avoid parsing unrelated segments.

Methods​

Method

Modifiers

Description

buildAck(options)

Returns an HL7 "ACK" (acknowledgement) message for this message.

get(index)

Returns an HL7 segment by index or by name.

getAckType()

Returns this message's acknowledgment code (MSA-1), if it is a recognized HL7 ACK code.

Useful on an ACK message to decide the outcome of the acknowledged message — e.g. AA/CA (accepted) vs AR/CR (rejected) vs AE/CE (error). The value is upper-cased before matching. Returns undefined when there is no MSA segment, MSA-1 is empty, or the value is not a known AckCode.

getAll(name)

Returns all HL7 segments of a given name.

getAllSegments(name)

Returns all HL7 segments of a given name.

Only the segments that match the requested name are parsed, which avoids parsing unrelated segments when scanning a message.

getSegment(index)

Returns an HL7 segment by index or by name.

When using a numeric index, the first segment (usually the MSH header segment) is at index 0.

When using a string index, this method returns the first segment with the specified name.

Segments are lazily parsed; the requested segment is parsed and cached on demand.

parse(text)

static

Parses an HL7 message string into an Hl7Message object.

setSegment(index, segment)

Sets or replaces a segment at the specified index. Only allows MSH header to be replaced as first segment. If index is a number and is larger than the length of the segments array, it will be appended as the last segment. If the index is a string, replaces the first segment with that name.

toString()

Returns the HL7 message as a string.

Unparsed segments are emitted directly from their original string form, which preserves the source text without forcing a parse.