Sub topic 13
Visibility and protected types in managed packages
This chapter assumes you can define custom metadata types and records, read them with getInstance() and SOQL, and know that managed packages have a namespace. Visibility only matters when you ship a type to other orgs in a managed package. In your own org or an unmanaged package, every type and record is readable.
Two levels of visibility
You set visibility on the type, and you can also set it on each record.
- Public type: Apex, SOQL, and APIs in the subscriber org can read the type and its records.
- Protected type: Only Apex code in the same managed package (the same namespace) can read the type and its records. Subscriber code and subscriber admins can't see them.
- Protected record: In a public type, you can mark a single record as protected using its Protected Component checkbox. Subscriber code can't see that record, but it can still see the public ones.
| Item | Public | Protected |
|---|---|---|
| Package's own Apex | Reads it | Reads it |
| Subscriber's Apex | Reads it | Can't read it |
| Subscriber admin | Sees records in Setup | Doesn't see them |
| Package upgrade | Can update records | Can update records |
Protection hides data from subscribers, but you can still change it. When you ship a new package version, your protected records are updated in the subscriber org.
Namespaces in code
Inside the package, you refer to Tax_Rate__mdt without a prefix. Code outside the package must add the namespace, so a public type from a package named acme becomes acme__Tax_Rate__mdt.
A common design keeps the type protected and exposes a narrow global method. The package author controls the data, and subscribers get only the result.
global with sharing class TaxRates {
global static Decimal getRate(String regionCode) {
Tax_Rate__mdt config = Tax_Rate__mdt.getInstance(regionCode);
if (config == null) {
throw new IllegalArgumentException('No rate for ' + regionCode);
}
return config.Rate__c;
}
}This class belongs to the package. A subscriber calls acme.TaxRates.getRate('DE'). It can't query acme__Tax_Rate__mdt directly, so the package can change the internal fields later without breaking subscribers.
Choosing a visibility
- Use protected for internal rules, pricing tables, and anything you want to change freely between versions.
- Use public when subscribers must add or edit records, such as a list of their own feature flags or strategy settings.
- Use protected records in a public type to ship default rows that you manage, while subscribers add rows of their own.
For a public type, also check each field's manageability setting. It decides whether a subscriber can edit values after installation or only you can change them through upgrades.
Common errors
- Assuming protection works in unmanaged code. In an unmanaged package or your own org, a protected type doesn't hide anything from your own Apex.
- Forgetting the namespace. Subscriber code that references
Tax_Rate__mdtinstead ofacme__Tax_Rate__mdtfails to compile. - Expecting a hidden record to throw. A caller outside the package doesn't get an access error. The record is simply not visible, so
getInstance()returnsnull. Handle that case, asFeatureFlagsdoes. - Choosing visibility too late. After you release a type, you can't freely move it to a more restrictive setting, because subscriber code may already depend on it. Decide before the first release, and check the current Salesforce documentation for what you can change afterward.
Exercise
Your managed package ships a DiscountRule_mdt type that you want to tune in each release without letting subscribers read or depend on it. Subscribers still need discounted prices. Which visibility do you choose, and how do subscribers get the results?
Show answer
Make the type protected so only your package's Apex can read it. Expose a global method, such as a pricing service that takes an order total and returns the discount. Subscribers call that method through the namespace and never query the type, so you can change its fields in later versions.