Why Hardcoding in Flow is a Risk
When building complex Salesforce Flows, developers often resort to hardcoding IDs, record type names, or status strings directly into decision nodes or assignment elements. This creates "magic strings" that break when moving between environments (Sandboxes to Production), as record IDs change or configuration names drift.
The Better Approach: Custom Metadata Types
Custom Metadata Types (CMT) provide a highly efficient way to store configuration data. Unlike Custom Settings, CMT records are deployable via Change Sets or DevOps pipelines (SFDX), making them ideal for environment-specific configuration.
Implementation Steps
- Define the Metadata Object: Create a new Custom Metadata Type (e.g.,
Flow_Configuration__mdt). - Add Fields: Add fields for the data you frequently change, such as email addresses, user IDs, or environment-specific thresholds.
- Query in Flow: Use the "Get Records" element to fetch your metadata. Since CMT is cached, this is highly performant and does not count against your SOQL governor limits in the same way as standard object queries.
Example: Dynamic Email Routing
Instead of hardcoding a support email in your Flow's "Send Email" action, store it in your metadata record.
- Create record: Create a record in
Flow_Configuration__mdtnamedSupport_Email_Config. - Get Records: Add a Get Records element to your Flow:
- Object:
Flow_Configuration__mdt - Filter:
DeveloperName Equals Support_Email_Config
- Object:
- Usage: Map the field value from the record variable directly into the "Recipient Address" field of your action.
Performance Considerations
- Caching: Salesforce caches Custom Metadata records at the application level. This means your Flow runtime retrieves these values with minimal latency.
- Governor Limits: Because CMT queries are treated differently than standard record queries, they are an excellent way to keep your automation within execution limits while maintaining clean, configuration-driven logic.
Key Takeaways
- Decouple Configuration: Move hardcoded strings, IDs, and parameters into Custom Metadata Types to improve maintainability.
- Environment Agnostic: Custom Metadata is deployable metadata, ensuring that your Flow configuration travels seamlessly through your CI/CD pipeline.
- Governor Limit Efficiency: Leveraging CMT reduces the need for constant SOQL queries on heavy configuration objects, keeping your Flow execution efficient.
- Scalability: Updating a value in a metadata record takes seconds and requires zero changes to the Flow itself, reducing the risk of introducing bugs during minor configuration updates.
Leave a Comment