Sub topic 13
Visibility and protected types in managed packages
This chapter assumes you can define a custom metadata type and its records, read them with getInstance() and SOQL, and that you know what a namespace and a managed package are. It covers what happens to your custom metadata after you ship it in a managed package. Who can read it? Who can change it? What does a package upgrade do to it?
Why visibility matters
In your own org, custom metadata is open to every admin and every piece of Apex. A managed package is different. Your code runs in other companies' orgs, and the metadata you ship lands there too. Some of it is meant for the customer to edit, such as a list of discount tiers. Some is internal, such as an endpoint URL, a license rule, or a mapping that your logic depends on.
Visibility settings control which is which. There are three levers:
- The visibility of the type.
- The protected component flag on individual records.
- The field manageability of each field.
Visibility only has an effect for components that are installed from a managed package. In an unpackaged org or an unmanaged package, a "protected" type is readable like any other.
Type visibility
When you create or edit a custom metadata type, you choose its Visibility. The setup labels vary slightly by release, but there are two behaviors.
- Public: Apex, APIs, and admins in the subscriber org can see the type and its records. This is the default.
- Protected (labelled as visible only to the package): Only Apex code in the same namespace can see the type, its fields, and its records. Subscribers cannot see the type in Setup, query it, or create records of it.
Use a protected type for configuration the customer should never touch or inspect. Use a public type when the customer is expected to add or edit records.
What a subscriber sees for a protected type
- The type does not appear in Setup under Custom Metadata Types.
- Subscriber Apex that references the type does not compile. Direct SOQL and
getInstance()calls fail. - Your package's Apex still reads it normally, because it runs in the same namespace.
Here is a package class that keeps its configuration private and exposes only an answer:
global with sharing class LicenseGate {
global static Boolean isFeatureOn(String featureName) {
// Pkg_Feature__mdt is a protected type in this package.
Pkg_Feature__mdt feature = Pkg_Feature__mdt.getInstance(featureName);
return feature != null && feature.Is_Enabled__c;
}
}The class is global so subscriber code can call it. The type stays hidden. A subscriber org with the package installed (namespace acme) uses it like this:
if (acme.LicenseGate.isFeatureOn('Export')) {
System.debug('Export is available');
}Subscriber code gets a Boolean and never sees the records behind it. This is the usual pattern: protect the data and expose a small global API. The API is a contract you must keep stable across versions, so keep it small.
Changing visibility after release
Visibility changes are limited once a package version is released, because subscribers may already depend on the type. A protected type can be loosened to public in a later version. A public type that has shipped cannot be made protected again, because subscriber code and records may already reference it. Treat the first release as a commitment. If you are unsure, start protected, since you can open it later but cannot close it. Check the current Salesforce documentation for the exact rules in your release.
Protected records in a public type
A type can be public while individual records are protected. On each record, the Protected Component checkbox controls this. This suits a type where you ship default rows and also let customers add their own.
- Protected records are readable only by code in your namespace.
- Subscriber SOQL and
getAll()do not return them. They are left out without any error. - Subscribers can still create their own records of the public type.
The silent omission is a trap for your own tests. If you query a type from a subscriber org and see fewer rows than you shipped, protected records are the likely cause.
// Run in a subscriber org against a public type from package "acme".
// Protected records shipped by the package are not returned here.
List<acme__Discount_Rule__mdt> visible = [
SELECT DeveloperName, Min_Amount__c, Percent__c
FROM acme__Discount_Rule__mdt
];
System.debug('Subscriber sees ' + visible.size() + ' rules');Code inside the package that runs the same query sees all of the rows, protected or not.
Namespace prefixes
A packaged type installed in a subscriber org has the namespace prefix in its API name, such as acme__Discount_Rule__mdt. Rules for using the prefix:
- Code inside the package never writes its own prefix. It uses
Discount_Rule__mdt, as in your dev org. - Subscriber code, and code in other packages, must include the prefix.
- Custom fields on a packaged type are prefixed too, for example
acme__Percent__c, when the field belongs to the package. - A record's developer name is prefixed in the same way, so
getInstance('Standard')in subscriber code does not find the packaged recordacme__Standard. In subscriber code, look up packaged records by their prefixed name.
Hardcoding a prefix inside package code is a bug that appears only after packaging. Your dev org has no prefix to remind you.
Field manageability: who controls the value after install
Visibility says who can see a record. Field manageability says who can change a field's value and what an upgrade does to it. You set it per field on the type:
| Setting | Subscriber can edit | Effect of a package upgrade |
|---|---|---|
| Locked after release | No | Upgrade overwrites the value with the developer's version |
| Subscriber editable | Yes | Upgrade keeps the subscriber's value |
Pick based on who owns the value:
- A rule your logic depends on, such as the class name for a strategy, should be locked. This stops a customer from breaking it.
- A threshold or toggle that customers tune should be subscriber editable. Otherwise every upgrade resets the customer's work.
Records you ship follow the same rule. A subscriber-editable field in a record you ship gets its initial value on install. After that, the subscriber's edits survive upgrades. Records that subscribers create themselves are not touched by upgrades.
How access is decided
Comparison of the protection options
| Option | Scope | Subscriber can see | Typical use |
|---|---|---|---|
| Protected type | Whole type | Nothing | Internal rules, endpoints, license logic |
| Protected record | One record | Other records of the type | Shipped defaults the customer must not edit |
| Locked field | One field | The value, read-only | Values your logic depends on |
| Subscriber-editable field | One field | The value, editable | Tuning knobs |
Design guidance
- Default to protected for anything the customer has no reason to touch. Opening a type later is possible, and closing it is not.
- Expose behavior, not data. A
globalmethod such asLicenseGate.isFeatureOn()lets you change the underlying type later without breaking subscribers. A public type becomes part of your API surface. - Do not rely on protection to hide secrets. Protection keeps ordinary users and subscriber code away from the data. Do not store credentials in custom metadata. Use named credentials or protected custom settings designed for secrets.
- Test as a subscriber. A scratch org or sandbox with your package installed shows the real namespace prefixes and the real visibility behavior. Running only in your dev org hides both.
- Mark editability on purpose. Review every field and ask who owns the value. The default is easy to leave unchanged by accident.
Exercise
Your managed package (namespace acme) ships a public type Region_Tax__mdt with default rows for US and CA. You want customers to add their own regions but not edit or read your two defaults. Also, the Calculator_Class__c field must always match your shipped value after upgrades, while the Rate__c field on customer-created rows must be editable. What settings do you use, and how does subscriber code see the data?
Show answer
Keep the type public so customers can create rows. Mark the US and CA records as Protected Component, so subscribers cannot read or edit them and their queries omit them. Set Calculator_Class__c to Locked after release so upgrades overwrite it. Set Rate__c to Subscriber editable so customers can change it and upgrades keep their values. Subscriber code queries acme__Region_Tax__mdt with the prefix and gets only the rows the customer created. Code inside the package sees all rows, including the protected defaults, and uses the unprefixed name Region_Tax__mdt.