Salesforce Developer – Custom Settings & Metadata Questions
Custom Settings and Metadata provide developers with efficient ways to store and manage configuration data in Salesforce. These questions cover the differences between List and Hierarchy Custom Settings, Metadata API usage, deployment strategies, and best practices for implementing flexible, maintainable applications. Understanding these concepts is essential for any Salesforce Developer looking to build configurable and scalable solutions.
Custom Settings & Metadata - Q&A
- Q1. What are Custom Settings and how do they differ from Custom Objects?
Ans: Custom Settings are: - Cached in memory for fast access - Available in both Setup and non-Setup contexts - Two types: List and Hierarchy Key differences from Custom Objects: * No storage limits (excluded from data storage) * Faster retrieval (cached) * Specialized for configuration data * No triggers or validation rules - Q2. Explain the two types of Custom Settings and their use cases.
Ans: Types: - List Custom Settings: * Store named collections of data * Global settings (e.g., API endpoints) * No user/organization hierarchy - Hierarchy Custom Settings: * User/profile-based overrides * Organization defaults with user exceptions * Profile-level settings with user overrides - Q3. How do you access Custom Settings in Apex?
Ans: Access patterns:// List Custom Setting MyListSetting__c setting = MyListSetting__c.getInstance('SettingName'); String value = setting.Field__c; // Hierarchy Custom Setting MyHierarchySetting__c userSetting = MyHierarchySetting__c.getOrgDefaults(); String orgValue = userSetting.Field__c; // With user context MyHierarchySetting__c userSetting = MyHierarchySetting__c.getInstance(userId); String userValue = userSetting.Field__c;
- Q4. What are the limitations of Custom Settings?
Ans: Key constraints: - 1,000 List records per org - 300 Hierarchy records per org - 300 fields per setting - No triggers or validation rules - Cannot be used in SOQL WHERE clauses - Limited to 32KB per record - Q5. How do Custom Metadata Types differ from Custom Settings?
Ans: Key differences: - Custom Metadata: * Deployable via Change Sets * Supports relationships * Can be used in formulas * Protected in managed packages * Supports custom fields - Custom Settings: * Faster runtime access * Hierarchy support * No deployment support * Limited field types - Q6. What is the Metadata API and when would you use it?
Ans: The Metadata API: - Programmatic access to Salesforce metadata - CRUD operations on components (objects, fields, etc.) - Deployment automation - Package creation Common use cases: * CI/CD pipelines * Dynamic configuration * Org-to-org migrations - Q7. How do you implement Custom Settings for multi-currency scenarios?
Ans: Implementation pattern:public class CurrencySettings { public static Decimal getExchangeRate(String currencyCode) { CurrencySetting__c setting = CurrencySetting__c.getInstance(currencyCode); return setting != null ? setting.Rate__c : 1.0; } }
Considerations: - Cache frequently accessed values - Handle missing settings gracefully - Implement update mechanisms - Q8. What are the security considerations for Custom Settings?
Ans: Security aspects: - No FLS enforcement (use with caution) - Visible to all users with object access - No sharing rules - Audit sensitive configuration data - Protect in managed packages - Consider encryption for PII - Q9. How do you test Custom Settings in unit tests?
Ans: Testing approach:@isTest private class CustomSettingsTest { @isTest static void testHierarchySetting() { // Create test setting MySetting__c orgDefault = new MySetting__c( SetupOwnerId = UserInfo.getOrganizationId(), Field__c = 'TestValue' ); insert orgDefault; // Test access MySetting__c retrieved = MySetting__c.getOrgDefaults(); System.assertEquals('TestValue', retrieved.Field__c); } }
- Q10. How do you handle Custom Settings in managed packages?
Ans: Packaging considerations: - Use Protected settings for internal use - Public settings for subscriber customization - Document all settings clearly - Provide upgrade paths - Consider namespace prefixes - Test with and without namespace - Q11. What are the performance implications of Custom Settings?
Ans: Performance factors: - Cached in memory (fast access) - No SOQL queries required - Minimal impact on governor limits - Consider bulk operations - Monitor record count limits - Use selective getInstance() calls - Q12. How do you migrate Custom Settings between orgs?
Ans: Migration approaches: - Data Loader for List settings - Ant Migration Tool for metadata - Custom Apex scripts - Salesforce DX for version control - Manual creation for Hierarchy settings - Consider dependencies and references - Q13. What are the best practices for Custom Settings architecture?
Ans: Recommended patterns: - Separate configuration from business logic - Use constants for field names - Implement caching layers - Validate setting values - Document all settings - Handle missing settings gracefully - Monitor usage and cleanup - Q14. How do you implement feature flags using Custom Settings?
Ans: Feature flag pattern:public class FeatureManager { public static Boolean isFeatureEnabled(String featureName) { FeatureFlag__c flag = FeatureFlag__c.getInstance(featureName); return flag != null ? flag.Enabled__c : false; } }
Benefits: - Gradual rollout control - Environment-specific features - Quick rollback capability - A/B testing support - Q15. Explain real-world Custom Settings scenarios.
Ans: Practical applications: - API endpoint configuration - Email template settings - Integration credentials (encrypted) - Business process parameters - User preference storage - Org-wide defaults with overrides