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

MDClarity · task primitive

AutoSendLetters

Overview

AutoSendLettersTask generates and sends patient cost estimate letters for visits in the batch table based on configured AutoSendLetterConfig criteria. It evaluates visits against qualification rules, generates personalized letters with cost estimates, and sends them via email or SMS based on patient preferences. This task can also regenerate letters when visit details change according to configured resend criteria.

Use this task when: You need to process cost estimate letters for visits that have been queued in the batch table, either for initial sending or regeneration after visit changes (charges, benefits, facilities, etc.).

Don't use this task when:

Parameters

This task has no configurable properties. All behavior is controlled by AutoSendLetterConfig entities stored in the database, which define:

The task operates on visits in the VisitBatch table, processing all visits that have been queued for letter generation.

Execution Flow

NoYesNoYesNo - AutoGenerateOnlyNo - Has errorsYesStart AutoSendLettersLoad allAutoSendLetterConfigsAny configs found?Exit - Log no configsGenerateAutoSendLetterItems frombatch visitsMatch each visit toqualifying configValidate patients and visitsDetermine letter generationneedShould generate letter?Add errors to send eventGenerate PatientLetterShould send letter?Save letter onlySave letter with errorsSend via email/SMSCreate HL7 outboundmessage if neededSet GFE compliance flagSavePatientLetterSendEventSave VisitChangeEvents ifregeneratedUpdate visitAutoSendLetterConfigKeyEnd

Processing Algorithm

The task uses a multi-phase processing algorithm to ensure correct letter generation and sending:

Phase 1: Configuration Matching

  1. Load all AutoSendLetterConfig records from database
  2. Filter out GFE-only configs if customer doesn't have GFE enabled
  3. For each visit in batch, find the highest-priority matching config based on:
    • Facility, Insurance, Provider matches (empty list = all qualify)
    • Visit type, status, scheduled date range
    • Deposit amount and total patient amount ranges
    • Patient age range
    • Custom property values
    • GFE visit filter state

Phase 2: Letter Generation Decision

For each visit that matched a config, determine if a new letter should be generated:

Phase 3: Letter Sending Decision

For each generated letter, determine if it should be sent:

Example Execution

Given:

Processing:

  1. Visit V-1: FAC-01, 3 days out, $1000 → Matches Config A → First letter → Generate & Send
  2. Visit V-2: FAC-02, Self-pay → Matches Config B → Generate but Don't Send (AutoGenerateOnly)
  3. Visit V-3: FAC-01, already sent letter, charges changed → Matches Config C → Regenerate & Resend
  4. Visit V-4: FAC-01, no patient email/phone → Matches Config A → Generate but Don't Send (error: InvalidEmailAndPhone)
  5. Visit V-5: Not calculated yet → Matches Config A → Don't Generate (error: NotCalculated)

Result: 4 letters generated, 2 sent successfully, 2 saved with errors, 1 skipped

Usage Examples

Example 1: Basic scheduled execution

// Typical scheduled job execution
var task = new AutoSendLettersTask();
task.Execute(customerDataStore);
// Processes all visits in VisitBatch against all active AutoSendLetterConfigs
// Generates and sends letters according to config rules

Example 2: Part of a visit processing pipeline

// After visit calculation, queue for letter sending
var calcTask = new BatchProcessAccountDataTask
{
    // ... calculation parameters
};
calcTask.Execute(customerDataStore);

// Send letters for newly calculated visits
var letterTask = new AutoSendLettersTask();
letterTask.Execute(customerDataStore);

Example 3: After configuration changes

// After updating AutoSendLetterConfig, reprocess affected visits
var reprocessTask = new ReprocessChangedConfigurationsTask
{
    ReprocessVisits = true,
    CustomerKey = "CUST-01"
};
reprocessTask.Execute(customerDataStore);

// Generate letters for newly qualifying visits
var letterTask = new AutoSendLettersTask();
letterTask.Execute(customerDataStore);

Example 4: With visit change tracking for resends

// Update visit charges (creates VisitChangeEvent)
visit.TotalPatientAmount = 1500;
visitRepo.Save(visit, batchMode: true);

// When AutoSendLetters runs, it checks VisitChangeEvents
// If visit has VisitResendCriteria = [ChargesChanged], letter regenerates
var letterTask = new AutoSendLettersTask();
letterTask.Execute(customerDataStore);
// Visit V-123's letter will regenerate because charges changed

Related Tasks Comparison

Task Scope Qualification Logic Custom SQL GFE Support
AutoSendLetters Batch table visits AutoSendLetterConfig criteria No Yes (sets GFE compliance)
AutoSendLettersForFutureVisitsTask All future visits (scheduled > now) Same AutoSendLetterConfig criteria No Yes (sets GFE compliance for future)
SendHL7LettersTask HL7OutboundLetter records No qualification (sends pre-created letters) No N/A (downstream task)

Key Differences:

When to choose which:

Performance Considerations

Database Impact

Optimization Tips

Scenario Recommendation Rationale
Large batch (>10,000 visits) Process in smaller chunks by queuing subsets of visits to batch table Reduces memory usage and transaction duration
Multiple configs with overlapping criteria Order configs by priority to control which config wins for multi-match visits First matching config (by priority) is used
Frequent resends due to visit changes Use targeted VisitResendCriteria (e.g., only ChargesChanged) instead of broad criteria Reduces unnecessary letter regeneration
High letter volume Consider AutoGenerateOnly configs to separate generation from sending Allows review before bulk sending
API rate limits Stagger task execution or implement custom throttling in letter sender Prevents API quota exhaustion

External API Impact