Now inviting automation leaders: publish your playbook as a skill, under your own name. Publish yours →
Automation Skills
All skills/Sf flow/Subflow-library
Reference · Sf flow

Subflow-library

Part of Sf flow. Install the skill and your agent reads this page when it needs the depth.

<!-- Parent: sf-flow/SKILL.md -->

Reusable Subflow Library

Overview

The Subflow Library provides pre-built, production-ready subflows that accelerate flow development and enforce best practices. Instead of recreating common patterns, use these standardized components to build flows faster while maintaining consistency.

Benefits:


Available Subflows

1. Sub_LogError

Purpose: Structured error logging for fault paths File: assets/subflows/subflow-error-logger.xml

When to Use:

Input Variables:

Output Variables: None

Example:

<subflows>
    <name>Log_Update_Error</name>
    <flowName>Sub_LogError</flowName>
    <inputAssignments>
        <name>varFlowName</name>
        <value>
            <stringValue>RTF_Account_UpdateIndustry</stringValue>
        </value>
    </inputAssignments>
    <inputAssignments>
        <name>varRecordId</name>
        <value>
            <elementReference>$Record.Id</elementReference>
        </value>
    </inputAssignments>
    <inputAssignments>
        <name>varErrorMessage</name>
        <value>
            <elementReference>$Flow.FaultMessage</elementReference>
        </value>
    </inputAssignments>
</subflows>

Prerequisites: Create Flow_Error_Log__c custom object with fields:

Related: Error Logging Example


2. Sub_SendEmailAlert

Purpose: Standard email notifications File: assets/subflows/subflow-email-alert.xml

When to Use:

Input Variables:

Output Variables: None

Example:

<subflows>
    <name>Notify_Manager</name>
    <flowName>Sub_SendEmailAlert</flowName>
    <inputAssignments>
        <name>varEmailAddresses</name>
        <value>
            <elementReference>$Record.Manager.Email</elementReference>
        </value>
    </inputAssignments>
    <inputAssignments>
        <name>varEmailSubject</name>
        <value>
            <stringValue>High-Value Opportunity Created</stringValue>
        </value>
    </inputAssignments>
    <inputAssignments>
        <name>varEmailBody</name>
        <value>
            <stringValue>A new opportunity worth {!$Record.Amount} has been created.</stringValue>
        </value>
    </inputAssignments>
</subflows>

Best Practices:


3. Sub_ValidateRecord

Purpose: Common validation patterns File: assets/subflows/subflow-record-validator.xml

When to Use:

Input Variables:

Output Variables:

Example:

<subflows>
    <name>Validate_Industry</name>
    <flowName>Sub_ValidateRecord</flowName>
    <inputAssignments>
        <name>varFieldValue</name>
        <value>
            <elementReference>$Record.Industry</elementReference>
        </value>
    </inputAssignments>
    <storeOutputAutomatically>true</storeOutputAutomatically>
</subflows>

<!-- Decision based on validation result -->
<decisions>
    <name>Check_Validation</name>
    <rules>
        <name>Valid</name>
        <conditions>
            <leftValueReference>Validate_Industry.varIsValid</leftValueReference>
            <operator>EqualTo</operator>
            <rightValue>
                <booleanValue>true</booleanValue>
            </rightValue>
        </conditions>
        <connector>
            <targetReference>Proceed_With_Update</targetReference>
        </connector>
    </rules>
    <defaultConnector>
        <targetReference>Show_Error_Screen</targetReference>
    </defaultConnector>
</decisions>

Extension: Customize the validation logic in the subflow for your specific needs:


4. Sub_UpdateRelatedRecords

Purpose: Bulk update pattern with error handling File: assets/subflows/subflow-bulk-updater.xml

When to Use:

Input Variables:

Output Variables: None

Example:

<!-- First, collect records in a loop -->
<loops>
    <name>Loop_Through_Contacts</name>
    <collectionReference>Get_Related_Contacts</collectionReference>
    <iterationOrder>Asc</iterationOrder>
    <nextValueConnector>
        <targetReference>Add_To_Collection</targetReference>
    </nextValueConnector>
    <noMoreValuesConnector>
        <targetReference>Call_Bulk_Updater</targetReference>
    </noMoreValuesConnector>
</loops>

<assignments>
    <name>Add_To_Collection</name>
    <assignmentItems>
        <assignToReference>colContactsToUpdate</assignToReference>
        <operator>Add</operator>
        <value>
            <elementReference>Loop_Through_Contacts</elementReference>
        </value>
    </assignmentItems>
    <connector>
        <targetReference>Loop_Through_Contacts</targetReference>
    </connector>
</assignments>

<!-- Then, call subflow OUTSIDE the loop -->
<subflows>
    <name>Call_Bulk_Updater</name>
    <flowName>Sub_UpdateRelatedRecords</flowName>
    <inputAssignments>
        <name>colRecordsToUpdate</name>
        <value>
            <elementReference>colContactsToUpdate</elementReference>
        </value>
    </inputAssignments>
    <inputAssignments>
        <name>varNewValue</name>
        <value>
            <elementReference>$Record.Industry</elementReference>
        </value>
    </inputAssignments>
</subflows>

Key Pattern: βœ… Correct: Loop β†’ Add to Collection β†’ (Outside Loop) β†’ Call Subflow with Collection ❌ Incorrect: Loop β†’ Call Subflow β†’ (DML in loop!)


5. Sub_QueryRecordsWithRetry

Purpose: Query with built-in error handling File: assets/subflows/subflow-query-with-retry.xml

When to Use:

Input Variables:

Output Variables:

Example:

<subflows>
    <name>Get_Related_Contacts</name>
    <flowName>Sub_QueryRecordsWithRetry</flowName>
    <inputAssignments>
        <name>varAccountId</name>
        <value>
            <elementReference>$Record.Id</elementReference>
        </value>
    </inputAssignments>
    <storeOutputAutomatically>true</storeOutputAutomatically>
</subflows>

<!-- Access query results -->
<decisions>
    <name>Check_Results</name>
    <rules>
        <name>Contacts_Found</name>
        <conditions>
            <leftValueReference>Get_Related_Contacts</leftValueReference>
            <operator>IsNull</operator>
            <rightValue>
                <booleanValue>false</booleanValue>
            </rightValue>
        </conditions>
        <connector>
            <targetReference>Process_Contacts</targetReference>
        </connector>
    </rules>
    <defaultConnector>
        <targetReference>No_Contacts_Path</targetReference>
    </defaultConnector>
</decisions>

Customization: Modify the query filters in the template for your specific object and criteria.


Deployment Guide

Step 1: Deploy Subflows to Your Org

# Deploy all subflows at once
sf project deploy start \
  --source-dir assets/subflows/ \
  --target-org myorg

# Or deploy individually
sf project deploy start \
  --source-dir assets/subflows/subflow-error-logger.xml \
  --target-org myorg

Step 2: Activate Subflows

  1. Navigate to Setup β†’ Flows
  2. Find each subflow (Sub_LogError, Sub_SendEmailAlert, etc.)
  3. Click Activate

⚠️ Important: Deploy Sub_LogError first if other subflows use it for error handling.

Step 3: Create Required Custom Objects

For Sub_LogError, create the Flow_Error_Log__c object:

# Using Salesforce CLI
sf data create record \
  --sobject CustomObject \
  --values "FullName=Flow_Error_Log__c Label='Flow Error Log' PluralLabel='Flow Error Logs'"

Or manually in Setup β†’ Object Manager β†’ Create β†’ Custom Object.


Usage Patterns

Pattern 1: Orchestrated Error Handling

Use Sub_LogError consistently across all flows:

Parent Flow
β”œβ”€β”€ DML Operation 1 β†’ [Fault] β†’ Sub_LogError
β”œβ”€β”€ DML Operation 2 β†’ [Fault] β†’ Sub_LogError
└── Subflow Call β†’ [Fault] β†’ Sub_LogError

Pattern 2: Modular Notifications

Centralize all email logic in Sub_SendEmailAlert:

Record-Triggered Flow
β”œβ”€β”€ Decision: High Value?
β”‚   β”œβ”€β”€ Yes β†’ Sub_SendEmailAlert(Manager)
β”‚   └── No β†’ End
└── Decision: Overdue?
    β”œβ”€β”€ Yes β†’ Sub_SendEmailAlert(Owner)
    └── No β†’ End

Pattern 3: Validation Pipeline

Chain validation subflows before DML:

Screen Flow
β”œβ”€β”€ Sub_ValidateRecord(Required Fields)
β”‚   └── Invalid? β†’ Show Error
β”œβ”€β”€ Sub_ValidateRecord(Business Rules)
β”‚   └── Invalid? β†’ Show Error
└── All Valid β†’ Create Record

Best Practices

βœ… DO:

  1. Deploy Once, Reference Everywhere: Activate subflows in your org, then reference them in multiple parent flows
  2. Use Naming Conventions: Start subflow names with Sub_ for easy identification
  3. Add Fault Paths: Connect all DML operations in subflows to error handlers
  4. Document Inputs/Outputs: Use clear variable names (varFieldName, colRecordCollection)
  5. Version Control: Track subflow changes and test before updating active versions

❌ DON'T:

  1. Don't Copy-Paste Subflows: Reference the deployed subflow instead of duplicating logic
  2. Don't Skip Error Handling: All subflows should handle their own errors gracefully
  3. Don't Hardcode Values: Use input variables for flexibility
  4. Don't Create DML in Loops: Use Sub_UpdateRelatedRecords pattern for bulk operations
  5. Don't Forget Testing: Test subflows independently before using in parent flows

Testing Your Subflows

Unit Testing Individual Subflows

  1. Create a test flow that calls the subflow
  2. Pass various input combinations (valid, invalid, null)
  3. Verify output variables and behavior
  4. Check error logs if using Sub_LogError
# Example: Test Sub_LogError
sf data query \
  --query "SELECT Flow_Name__c, Error_Message__c FROM Flow_Error_Log__c ORDER BY CreatedDate DESC LIMIT 5" \
  --target-org myorg

Integration Testing in Parent Flows

  1. Use subflows in record-triggered flows
  2. Test with bulk data (200+ records)
  3. Verify subflow doesn't cause governor limit errors
  4. Check execution time in debug logs

Customization Guide

Extending Subflows

All subflows are templatesβ€”customize for your needs:

  1. Clone the subflow: Create a copy (e.g., Sub_LogError_WithEmail)
  2. Add custom logic: Extend functionality while keeping core pattern
  3. Maintain naming convention: Keep Sub_ prefix for discoverability

Example: Enhanced Error Logger

Extend Sub_LogError to send Platform Events:

<!-- Add to subflow after Create_Error_Log -->
<recordCreates>
    <name>Publish_Error_Event</name>
    <object>Flow_Error__e</object>
    <inputAssignments>
        <field>Flow_Name__c</field>
        <value>
            <elementReference>varFlowName</elementReference>
        </value>
    </inputAssignments>
    <!-- Real-time error monitoring -->
</recordCreates>

Performance Considerations

Governor Limits

Optimization Tips

  1. Batch Operations: Use Sub_UpdateRelatedRecords for bulk updates
  2. Minimize Subflow Calls: Call once with collections vs. multiple times with single records
  3. Cache Results: Store subflow outputs in variables to avoid repeated calls

Troubleshooting

"Subflow not found" Error

"Input variable not found" Error

Performance Issues


Version History

Version Date Changes
1.0 2024-11-30 Initial library: 5 subflows (LogError, EmailAlert, Validator, BulkUpdater, QueryWithRetry)

Related Documentation


Support

For issues or questions:

  1. Check subflow XML for correct variable names and types
  2. Test subflow independently before using in parent flow
  3. Review error logs in Flow_Error_Log__c (if using Sub_LogError)
  4. Check debug logs for detailed execution information

Happy flow building! πŸš€

Publish your skill

Copy this prompt into Claude Code, Codex or any agent. It walks you through your first skill in about 20 minutes, with you as the author.

Click to copy.