How are Custom Metadata Types used in advanced Salesforce Field Service implementations, and why are they better than hardcoding values?
Answer
Custom Metadata Types store configuration as deployable metadata, so behaviour can change without code edits.
Why they suit Field Service
- Scheduling Policies and Work Rules are data and need data migration, but custom metadata deploys with change sets and the CLI, and the records are testable in Apex.
- Admins can change values without a code release.
Typical uses
- Escalation times and thresholds per territory or work type.
- Status mappings and reason codes.
- Names of the Scheduling Policy to use per region.
- Integration endpoints and feature switches.
apex
public with sharing class FsConfig {
public static Integer escalationMinutes(String territoryKey) {
FS_Config__mdt cfg = FS_Config__mdt.getInstance(territoryKey);
return cfg == null ? 60 : Integer.valueOf(cfg.Escalation_Minutes__c);
}
}
Benefits
getInstance()reads cached metadata and does not count against SOQL limits.- Behaviour stays consistent across sandboxes.
- Avoids hardcoded values in Apex and Flows.
Custom MetadataApexConfigurationDeployment