Sub topic 14
Pitfalls and design guidelines
This chapter assumes you can read and write Apex, have used getInstance(), getAll() and SOQL on custom metadata, and know how to write an @IsTest class. It collects the mistakes that show up most often and the habits that prevent them.
Pitfall 1: Hardcoded record names
getInstance('New_Checkout') fails silently if someone renames the record or if the record is missing in a sandbox. The call returns null, and code that dereferences the result throws a NullPointerException.
Two habits help:
- Keep each developer name in one constant, not scattered through the code.
- Decide what a missing record means and write that decision into the code.
public with sharing class CheckoutFlags {
public static final String NEW_CHECKOUT = 'New_Checkout';
public static Boolean isNewCheckoutEnabled() {
Feature_Flag__mdt flag = Feature_Flag__mdt.getInstance(NEW_CHECKOUT);
return flag != null && flag.Is_Enabled__c;
}
}A missing flag is treated as off. For required configuration, such as a tax strategy, throw a clear exception instead.
Pitfall 2: Expecting DML
Apex cannot insert, update or delete custom metadata records with DML. Records are metadata, so changes travel through deployments, packages, or the Metadata API. If code must change a record, use Metadata.Operations.enqueueDeployment().
That call is asynchronous. A read in the same transaction still returns the old value, and the deployment can fail after your code has moved on. Always supply a Metadata.DeployCallback and handle the failure case.
Pitfall 3: Tests that depend on org data
A test cannot create custom metadata records. If the test relies on a record that exists in your sandbox, it can fail in a scratch org or in production. Put the lookup behind a small class and add a @TestVisible override, as shown in the testing chapter. Test the "record missing" path as well as the "enabled" path.
Pitfall 4: Using the wrong tool
Custom metadata fits configuration that developers or admins change rarely and that should move with deployments. It fits poorly in these cases:
- Data that changes per user or at runtime. Use a custom object or a hierarchy custom setting.
- Large or fast-growing data sets. Use a custom object.
- Text shown to users that needs translation. Use custom labels.
Pitfall 5: Unsafe dynamic class loading
When a record names an Apex class for Type.forName(), a typo in the record becomes a runtime failure. Check for null after forName(). Cast to an interface, not to a concrete class. Keep the class name in a text field that only trusted admins can edit.
Design guidelines
- Wrap every custom metadata read in one accessor class, so callers never touch the
__mdttype directly. - Choose
getInstance()orgetAll()for lookups by name, and SOQL only when you need filtering or sorting. - Define defaults in code for every setting, so a missing record never breaks the feature.
- Keep records small and focused. One type per concern is easier to deploy and review than one large type.
- Treat configuration changes like code changes: put them in source control and deploy them through the same pipeline.
Exercise
A trigger reads a custom metadata record named Bulk_Limit with getInstance('Bulk_Limit') and uses .Max_Rows__c directly. It works in your sandbox but throws a NullPointerException in a new scratch org. What are two fixes?
Show answer
The record does not exist in the scratch org, so getInstance() returns null. First, check for null and fall back to a default value in code. Second, make sure the record is included in the deployment or source so it exists in every org. Also keep the name in a constant and move the lookup into an accessor class that tests can override.