JSON Field Storage Pattern
Problem Context
In Salesforce development, we often encounter scenarios where we need to store structured, supplementary data that:
Is primarily for display or informational purposes
Won't be used in SOQL queries, workflows, or process automation
Varies in structure between records
Would otherwise require creating numerous custom fields or related objects
Needs to maintain flexibility for future changes without schema modifications
Traditional approaches like creating custom objects or fields for every piece of data can lead to:
Object proliferation and complexity
Hitting Salesforce limits (custom field limits, object limits)
Maintenance overhead for rarely-used fields
Performance impacts from excessive joins
Schema rigidity that hampers rapid iteration
Solution: JSON Storage in LongTextArea Fields
The pattern leverages Salesforce's LongTextArea fields to store JSON-serialized data, providing a flexible, schema-less approach for non-transactional information.
Field Definition Pattern
Define a generic Metadata__c field on objects that need flexible data storage:
Key Configuration Decisions:
Field Type:
LongTextAreaprovides up to 131,072 characters (sufficient for complex JSON structures)History Tracking: Generally disabled for performance (JSON diffs are not user-friendly)
Field Naming: Use consistent naming like
Metadata__cor domain-specific names likeReviewData__c
Implementation Patterns
1. Configuration Storage Pattern
Store variable configuration mappings that drive application behavior:
Example JSON Structure:
2. Dynamic Options Storage Pattern
Store dynamic form options that vary by record:
Example JSON Structure:
3. Document Metadata Pattern
Store supplementary document information from external systems:
Display Pattern in Lightning Web Components
Parsing and Flattening JSON for Display
The metadataView LWC demonstrates how to parse and display JSON data:
Display Template Pattern
Benefits of This Approach
1. Schema Flexibility
Add new data attributes without deploying metadata changes
Adapt to changing requirements without modifying object schema
Support varying data structures across records of the same type
2. Performance Optimization
Reduce custom field count (helps with query selectivity)
Minimize object relationships for non-queryable data
Single field retrieval for all supplementary data
3. Development Velocity
Rapid iteration on data structures
No deployment dependencies for data model changes
Easier integration with external systems that provide JSON
4. Maintenance Simplicity
Centralized storage pattern
Consistent parsing approach
Clear separation between transactional and display data
When to Use This Pattern
Ideal Use Cases
External System Data: Information from APIs that won't be queried
Configuration Storage: Variable settings that differ by context
Document Metadata: File properties, processing results, audit information
Form Responses: Dynamic questionnaire answers, survey data
Integration Payloads: Preserving original data from external sources
Audit/History Data: Snapshots of record states for reference
When NOT to Use This Pattern
Queryable Data: Any field that needs SOQL filtering
Workflow Criteria: Data used in automation rules
Reporting Fields: Information needed in reports/dashboards
Frequently Updated Data: High-volume transactional updates
Regulated Data: Information requiring field-level security
Best Practices and Considerations
1. Error Handling
Always implement robust error handling for JSON parsing:
2. Validation
Consider implementing JSON schema validation for critical data:
3. Size Management
Monitor field usage to avoid hitting the 131KB limit:
4. Documentation
Always document the expected JSON structure:
5. Type Safety
Create wrapper classes for complex structures:
Common Pitfalls to Avoid
Using JSON fields for queryable data - This defeats the purpose and creates maintenance nightmares
Storing sensitive data - JSON fields bypass field-level security
Neglecting error handling - Invalid JSON will break your application
Over-nesting structures - Keep JSON reasonably flat for maintainability
Missing data migration strategy - Plan for structure evolution
Ignoring governor limits - Large JSON processing can hit CPU limits
Conclusion
The JSON field storage pattern provides a powerful solution for managing non-transactional, display-oriented data in Salesforce. By understanding when and how to apply this pattern, development teams can build more flexible, maintainable applications while avoiding the overhead of excessive custom fields and objects.
Remember: This pattern complements, not replaces, proper data modeling. Use it judiciously for the right use cases to maximize its benefits while maintaining system integrity and performance.
Last updated
Was this helpful?