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

MDClarity · task primitive

SendHL7Letters

Overview

SendHL7LettersTask sends pending HL7 outbound messages by generating PDF files from patient letters and writing both the PDF and HL7 message text to disk. The task retrieves all unsent HL7 outbound letters from the database, formats them with timestamp and file path information, generates PDF attachments, and marks the letters as sent.

Use this task when: You need to export patient letters as HL7 ORU^R01 messages with PDF attachments for transmission to external systems (labs, EMRs, billing systems, etc.) via file-based integration.

Don't use this task when:


Parameters

Parameter Type Required Default Description
OutputFilePath string Yes "C:\\HL7" Local directory path where both HL7 message (.txt) and PDF files will be written
PublishDirectory string Yes "C:\\HL7" Directory path referenced in the HL7 message OBX segment (where receiving system should find the PDF)
SendingApplication string Yes "MDClarity" HL7 MSH-3: Sending Application identifier
SendingFacility string Yes "MDClarity" HL7 MSH-4: Sending Facility identifier
ReceivingApplication string Yes "MDClarity" HL7 MSH-5: Receiving Application identifier (target system)
ReceivingFacility string Yes "MDClarity" HL7 MSH-6: Receiving Facility identifier (target location)
ProcessingID string Yes "P" HL7 MSH-11: Processing ID ("P" = Production, "T" = Test, "D" = Debug)
VersionID string Yes "2.5" HL7 MSH-12: HL7 version (typically "2.3", "2.4", "2.5", or "2.6")

Important: All parameters are required. The task validates that no property is empty and will throw an exception if any are missing.


Execution Flow

Validation failsValidation passesNoYesYesNoStart SendHL7LettersTaskValidate all Properties arenon-emptyThrow Exception: Propertycannot be emptyGet all unsent letters fromHL7OutboundLetterRepoAny unsent letters?Exit - Nothing to processLoop through eachHL7OutboundLetterGet current timestampRetrieve PatientLetterHTML from databaseGenerate PDF from HTMLcontentSave PDF asPatientLetter_Key_Timestamp.pdfFormat HL7 message textReplace FilePathplaceholder with escapedpublish pathReplace MessageTimeplaceholder with HL7timestampReplace MessageIDplaceholder with letter keyReplace HL7 header fieldswith Properties valuesSave HL7 message asHL7Message_Key_Timestamp.txtUpdateHL7OutboundLetter:SentDate and FileLocationSave updated letter todatabaseMore letters?Complete

Processing Algorithm

The task processes each unsent letter through the following steps:

  1. Letter Retrieval: Queries HL7OutboundLetter table for all records where SentDate IS NULL
  2. PDF Generation: For each letter, retrieves the PatientLetter.Html and generates a PDF file
  3. Message Formatting: Replaces placeholders in the HL7 message template:
    • {FilePath} → Escaped path to PDF (e.g., D:\\HL7\\PatientLetter_HG-123_[id].pdf)
    • {MessageTime} → HL7 formatted timestamp (e.g., [id])
    • {MessageID} → Letter key (e.g., HG-123)
    • {SendingApplication}, {SendingFacility}, etc. → Values from Properties
  4. File Writing: Writes two files per letter:
    • HL7 message text file: HL7Message_{Key}_{Timestamp}.txt
    • PDF attachment: PatientLetter_{Key}_{Timestamp}.pdf
  5. Status Update: Sets SentDate and FileLocation on HL7OutboundLetter and saves to database

Example Execution

Scenario: Processing 3 pending HL7 outbound letters at 2025-01-28 12:00:00

Input:

Execution Steps:

  1. Validate all 8 Properties are non-empty ✓
  2. Retrieve 3 unsent letters from database
  3. For letter HG-001:
    • Get PatientLetter L-001 HTML: <html><body>Your estimate is $150...</body></html>
    • Generate PDF → Save to C:\HL7Output\PatientLetter_HG-001_[id].pdf
    • Format message: Replace {FilePath} with D:\\HL7Publish\\PatientLetter_HG-001_[id].pdf
    • Format message: Replace {MessageTime} with [id]
    • Format message: Replace {MessageID} with HG-001
    • Format message: Replace header fields with Properties values
    • Write HL7 message → Save to C:\HL7Output\HL7Message_HG-001_[id].txt
    • Update HL7OutboundLetter: SentDate = 2025-01-28 12:00:00, FileLocation = C:\HL7Output
  4. Repeat for HG-002 and HG-003
  5. Complete

Output:


Usage Examples

Example 1: Basic production HL7 output

var task = new SendHL7LettersTask
{
    Properties = new HL7SendProperties
    {
        OutputFilePath = @"C:\HL7Output",
        PublishDirectory = @"D:\HL7Publish",  // May be different if files are moved after creation
        SendingApplication = "MDClarity",
        SendingFacility = "MDClarityHospital",
        ReceivingApplication = "LabSystem",
        ReceivingFacility = "LabCorp",
        ProcessingID = "P",  // Production
        VersionID = "2.5"
    }
};

Example 2: Test environment with debug processing

// Send HL7 messages in test mode with debug processing ID
var task = new SendHL7LettersTask
{
    Properties = new HL7SendProperties
    {
        OutputFilePath = @"C:\TestHL7",
        PublishDirectory = @"C:\TestHL7",  // Same directory for test
        SendingApplication = "MDClarity_Test",
        SendingFacility = "TestFacility",
        ReceivingApplication = "TestEMR",
        ReceivingFacility = "TestSite",
        ProcessingID = "D",  // Debug mode
        VersionID = "2.5"
    }
};

Example 3: Integration with EMR using HL7 2.3

// Send patient letters to EMR that only supports HL7 v2.3
var task = new SendHL7LettersTask
{
    Properties = new HL7SendProperties
    {
        OutputFilePath = @"\\NetworkShare\HL7\Outbound",  // Network share monitored by EMR
        PublishDirectory = @"\\NetworkShare\HL7\PDF",
        SendingApplication = "MDCLARITY",
        SendingFacility = "HOSPITAL1",
        ReceivingApplication = "EPICEMR",  // Target EMR system
        ReceivingFacility = "MAINSITE",
        ProcessingID = "P",
        VersionID = "2.3"  // Older HL7 version for compatibility
    }
};

Example 4: Part of a patient letter pipeline

// Step 1: Generate patient letters (creates HL7OutboundLetter records)
var autoSendTask = new AutoSendLettersTask();
autoSendTask.Execute(dataStore);

// Step 2: Send HL7 messages with PDF attachments to external system
var sendHL7Task = new SendHL7LettersTask
{
    Properties = new HL7SendProperties
    {
        OutputFilePath = @"C:\HL7\Outbound",
        PublishDirectory = @"C:\HL7\PDF",
        SendingApplication = "MDClarity",
        SendingFacility = "Facility001",
        ReceivingApplication = "BillingSystem",
        ReceivingFacility = "BillingSite",
        ProcessingID = "P",
        VersionID = "2.5"
    }
};
sendHL7Task.Execute(dataStore);

// Step 3: Upload HL7 files to remote SFTP server
var uploadTask = new UploadTask
{
    LocalDirectory = @"C:\HL7\Outbound",
    RemoteDirectory = "/inbound/hl7",
    // ... other upload properties
};
uploadTask.Execute(dataStore);

Related Tasks Comparison

Task Use Case Direction Message Type File Generation
SendHL7LettersTask Export patient letters as HL7 messages Outbound (MDClarity → External) ORU^R01 (with PDF) Generates both .txt and .pdf files
LoadHL7FilesTask Import patient data from HL7 feeds Inbound (External → MDClarity) ADT, ORM, DFT, etc. Reads existing HL7 files
AutoSendLettersTask Send patient letters via email/SMS Outbound (MDClarity → Patients) Email/SMS Creates HL7OutboundLetter records (used by SendHL7LettersTask)
Upload Transfer files to remote servers Outbound (Local → Remote) Any file type No file generation (transfers existing files)

Key Differences:

Typical Pipeline:

AutoSendLettersTask → SendHL7LettersTask → Upload (to SFTP/FTP)

Performance Considerations

Database Impact

File System Impact

Optimization Tips

Scenario Recommendation Rationale
High volume (1000s of letters) Run during off-hours; ensure adequate disk space PDF generation can be CPU-intensive and files consume disk space
Network share output Use local path then transfer via Upload task Direct network writes are slower; local-then-upload is faster
Frequent small batches Run every 5-15 minutes Reduces latency for letter delivery to external systems
Large letter HTML Monitor PDF generation time Complex HTML with images can take 1-5 seconds per PDF