Legacy Pipeline Atlas worker-1snapshot 2026-07-15

MDClarity · task primitive

LoadHL7Files

Overview

LoadHL7FilesTask processes HL7 message files from a directory, parsing and importing patient demographics, visits, orders, and charges into database tables. The task reads all HL7 files from a specified folder, validates message types against an allowed list, extracts structured data, and archives processed files to a separate directory.

Use this task when: You need to import HL7 messages (ADT, SIU, ORM, DFT) from external systems like EMRs, practice management systems, or lab interfaces to populate patient, visit, order, and charge data.

Don't use this task when:


Parameters

Parameter Type Required Default Description
MessageFolderPath string Yes - Directory path containing HL7 message files to process. All files except those ending in .part are processed.
MessageProcessedFolderPath string Yes - Directory path where processed files will be moved after successful or failed processing. Files are organized into subdirectories based on processing status.
AllowedMessageTypes List<string> No All supported types List of HL7 message types to process (e.g., ADT^A01, SIU^S12). Messages not in this list are skipped and archived. Default includes all 16 supported message types.
AddTimestampToFileName bool No true When true, appends timestamp to filename when archiving (e.g., message.hl7 becomes message_2025-01-15T14-30-00.hl7). Prevents filename collisions.

Execution Flow

NoYesNoYesYesNoADT^A01/A03/A04/A09/A10ADT^A08/A28/A31SIU^S12/S13/S14/S15/S17/S26ORM^O01DFT^P03YesNoStart LoadHL7FilesTaskGet all files fromMessageFolderPathExclude .part filesInitialize counters: patient,visit, ADT, order, charge,archiveFor each fileRead file textParse HL7 message withHL7ProcessorMessage type allowed?Mark as SkipParse successful?Mark as ErrorSave to HL7Archive tableSave to HL7Message tablewith segmentsError or Skip?Archive to error/skip folderDetermine message typeSave to HL7ADT tableSave to HL7Patient tableSave to HL7Visit tableSave to HL7Order tableSave to HL7Charge tableIncrement ADT counterIncrement Patient counterIncrement Visit counterIncrement Order counterIncrement Charge counterArchive file to processedfolderMore files?Log summary statisticsEnd

Processing Algorithm

The task processes HL7 files using a three-phase approach:

Phase 1: File Discovery and Reading

  1. File Enumeration: Scans MessageFolderPath for all files, excluding .part extension (incomplete transfers)
  2. Progress Logging: Logs progress every 10 files to track batch processing

Phase 2: Message Parsing and Validation

  1. Read File: Extracts raw HL7 message text from file
  2. Parse Message: Uses HL7Processor to parse message structure:
    • Validates MSH segment (message header) is present
    • Extracts message type from MSH-9 field (e.g., ADT^A01)
    • Checks if message type is in AllowedMessageTypes list
    • Parses all segments into structured data dictionary
  3. Error Handling:
    • Missing MSH segment → Mark as Error, log "No MSH segment found"
    • Unknown message type → Mark as Error, log "Unknown message type"
    • Disallowed message type → Mark as Skip, log "Message type not allowed"
    • Incomplete required segments → Mark as Error with specific segment error

Phase 3: Database Import and Archiving

  1. HL7Archive Table: Every message (including errors and skips) is saved with:

    • Unique archive key
    • Original message text
    • Error flag and error message (if applicable)
    • Skip flag (if message type not allowed)
  2. HL7Message Table: Every message is saved with:

    • Parsed segment list (MSH, PID, PV1, etc.)
    • Message metadata
  3. Type-Specific Table: For successfully parsed, allowed messages only:

    • ADT Messages (A01, A03, A04, A09, A10) → HL7ADT table
    • Patient Messages (A08, A28, A31) → HL7Patient table
    • Visit Messages (S12, S13, S14, S15, S17, S26) → HL7Visit table
    • Order Messages (O01) → HL7Order table
    • Charge Messages (P03) → HL7Charge table
  4. File Archiving: Move processed file to MessageProcessedFolderPath:

    • Optionally append timestamp if AddTimestampToFileName = true
    • Organize into subdirectories by status (successful vs. error/skip)

Supported Message Types

Category Message Types Table Description
ADT (Admission/Discharge/Transfer) ADT^A01, ADT^A03, ADT^A04, ADT^A09, ADT^A10 HL7ADT Patient admission, discharge, registration
Patient ADT^A08, ADT^A28, ADT^A31 HL7Patient Patient demographics updates
Visit SIU^S12, SIU^S13, SIU^S14, SIU^S15, SIU^S17, SIU^S26 HL7Visit Appointment scheduling
Order ORM^O01 HL7Order Clinical orders
Charge DFT^P03 HL7Charge Detailed financial transactions

Usage Examples

Example 1: Process all supported message types with archiving

var task = new LoadHL7FilesTask
{
    MessageFolderPath = @"C:\HL7\Incoming",
    MessageProcessedFolderPath = @"C:\HL7\Archive",
    // AllowedMessageTypes omitted - defaults to all 16 supported types
    // AddTimestampToFileName omitted - defaults to true
};

Example 2: Process only patient and visit messages

var task = new LoadHL7FilesTask
{
    MessageFolderPath = @"C:\HL7\Incoming",
    MessageProcessedFolderPath = @"C:\HL7\Archive",
    AllowedMessageTypes = new List<string>
    {
        "ADT^A08",  // Patient update
        "ADT^A28",  // Add patient
        "ADT^A31",  // Update patient
        "SIU^S12",  // Appointment notification
        "SIU^S13",  // Appointment rescheduling
        "SIU^S15"   // Appointment cancellation
    },
    AddTimestampToFileName = true
};

Example 3: Process only charge messages without timestamp

var task = new LoadHL7FilesTask
{
    MessageFolderPath = @"\\server\share\HL7\DFT",
    MessageProcessedFolderPath = @"\\server\share\HL7\Processed",
    AllowedMessageTypes = new List<string> { "DFT^P03" },
    AddTimestampToFileName = false // Use original filenames in archive
};

Example 4: Import from network share with full message type filtering

var task = new LoadHL7FilesTask
{
    MessageFolderPath = @"\\EMR-Server\HL7Export\Outbound",
    MessageProcessedFolderPath = @"\\EMR-Server\HL7Export\Archived",
    AllowedMessageTypes = new List<string>
    {
        // ADT messages
        "ADT^A01", "ADT^A03", "ADT^A04",
        // Patient updates
        "ADT^A08", "ADT^A28",
        // Orders
        "ORM^O01",
        // Charges
        "DFT^P03"
    },
    AddTimestampToFileName = true
};

Related Tasks Comparison

Task Use Case Message Format File Handling Tables Updated
LoadHL7FilesTask Import HL7 messages from files HL7 v2.x standard Batch processing from folder, moves files after processing Multiple HL7 tables (Archive, Message, ADT, Patient, Visit, Order, Charge)
LoadDataFileTask Import delimited data files CSV, pipe-delimited, etc. Single file import with optional archiving Single target table defined in DataFileSpec
ProcessDataFileTask Import and merge data files CSV, delimited Scans file structure and performs merge Import table + source table

When to use LoadHL7FilesTask:

When to use alternatives:


Performance Considerations

Database Impact

File System Impact

Optimization Tips

Scenario Recommendation Rationale
Large file batches (1000+ files) Split into multiple smaller batches or run during off-hours Task processes files sequentially; progress logged every 10 files
Network share processing Copy files to local folder first, then process Reduces network I/O overhead during parsing
High error rate Filter AllowedMessageTypes to expected types only Skips unwanted message types early in processing
Archive storage concerns Disable timestamp (AddTimestampToFileName = false) or implement archive cleanup job Prevents archive folder growth from duplicate filenames