Sub topic 2
What Makes a Package Managed: Namespace, IP Protection, and Upgrade Rules
This chapter assumes you know Salesforce orgs, metadata, and Apex, and that you have used scratch orgs and sfdx-project.json. It covers the three properties that make a managed package different from a plain bundle of metadata: a namespace, hidden code, and rules that keep upgrades safe.
Why "managed" is a different kind of package
A managed package is built for distribution to orgs you don't control. Those orgs are called subscriber orgs. Your customers customize them, write code against them, and depend on your components. Once a version is released, you can't see or fix those orgs. The managed-package rules exist so that a later version can't break them.
Three mechanisms provide this:
- A namespace that marks every component as yours.
- Code and metadata protection, so subscribers can't read or copy your implementation.
- Restrictions on what you may change or remove once a version is released.
The namespace
A namespace is a short identifier of 1 to 15 alphanumeric characters. It is attached to every component in your package. Salesforce adds it as a prefix, so a custom object Invoice__c in the namespace acme becomes acme__Invoice__c. Apex classes use a dot: acme.InvoiceService.
This prefix prevents name collisions. A subscriber can have its own Invoice__c object, and yours stays separate. It also tells anyone looking at an org which components came from your package.
Some points to remember:
- A namespace is permanent. You can't rename it after you release a package that uses it.
- You register it in a Developer Edition org, then link it to your Dev Hub. The next chapter walks through this.
- One namespace can be shared by several packages that you own.
- Inside your own namespace, you write unprefixed names in source. Salesforce adds the prefix when it builds the package version.
IP protection
Subscribers can install and use your package, but they can't read your implementation. In a subscriber org, Apex classes and triggers from the package are hidden. Their source doesn't appear in Setup or the Developer Console. Debug logs from your code are also limited, so subscribers can't use them to reverse-engineer your logic.
What subscribers can see is the interface you chose to expose. That interface is controlled by Apex access modifiers.
| Modifier | Visible to | Can you change it after release? |
|---|---|---|
| `private` | The class itself | Yes |
| `public` | Apex inside your namespace only | Yes |
| `global` | Any Apex, including subscriber code and other namespaces | Restricted (see below) |
| `@NamespaceAccessible` on `public` | Apex in any package with the same namespace | Yes, with some limits |
Use public for everything that isn't meant to be called from outside. Use global only for deliberate extension points, such as an API that customers script against. Every global item becomes a promise.
// Namespace: acme
global with sharing class InvoiceService {
// Part of the supported API. Subscribers can call it.
// Its signature is locked once released.
global static Decimal calculateTotal(List<Decimal> lineAmounts) {
return sumAmounts(lineAmounts);
}
// Internal helper. Hidden from subscribers and free to change.
private static Decimal sumAmounts(List<Decimal> amounts) {
Decimal total = 0;
for (Decimal amount : amounts) {
total += amount;
}
return total;
}
// Visible to other packages that share the "acme" namespace,
// but not to subscribers.
@NamespaceAccessible
public static String formatInvoiceNumber(Integer sequence) {
return 'INV-' + String.valueOf(sequence).leftPad(6, '0');
}
}A subscriber's code can call acme.InvoiceService.calculateTotal(...). It can't call sumAmounts, and it can't read the method body. Protection varies by component type. Apex is hidden, but other types, such as page layouts and many declarative components, remain visible and editable within limits. Check the packaging documentation for the type you ship.
Upgrade rules
Subscribers upgrade by installing a newer version over the one they have. The platform has to guarantee that anything the customer built on the old version still works. The rules follow from that.
What is locked after release
Once a version is promoted to released, the parts of it that subscribers can depend on become fixed:
globalApex classes, methods, interfaces, and enums can't be removed. Their signatures can't change.- You can add new
globalmethods to a class. Adding a method to aglobalinterface is restricted, because it would break every class that implements the interface. - Many field and object properties that other metadata depends on are locked. An example is the data type of a custom field.
- Components subscribers have referenced, such as custom fields used in their reports, flows, or code, can't simply disappear.
What stays flexible
publicandprivatecode can change freely, including removal.- You can add new components, new fields, and new
globalmethods on existing classes. - Some items can be marked
@Deprecated. This tells subscribers to stop using them without breaking those who still do. - Beta versions aren't bound by these rules. You can change anything until you promote.
global class LegacyPricing {
// Keeps working for existing callers, but is flagged for removal planning.
@Deprecated
global static Decimal oldPrice(Id productId) {
return newPrice(productId, 'USD');
}
global static Decimal newPrice(Id productId, String currencyCode) {
return 0; // Real pricing logic omitted.
}
}How removal works in 2GP
Second-generation packaging is more flexible than first-generation packaging. In some cases you can delete a component from a later version after a release. Subscriber orgs keep the component, marked as deprecated, until it is safe to drop. Which component types can be removed, and under which conditions, changes between releases. Look up the current list before you plan a removal.
The ancestor you set in sfdx-project.json decides which earlier version the platform compares your new version against. That is why ancestry belongs to the upgrade rules. A later chapter covers it.
Checking the rules in practice
You don't have to memorize the rules. Package version creation validates your source against the ancestor and fails when the new version would break compatibility. The error appears when you run sf package version create, not after customers install. Treat that check as a safety net, not a design tool. Decide what is global before you write the code.
A practical checklist before each promotion:
- List the
globalitems added in this version. Each one should be a deliberate API. - Confirm that no
globalitem from the ancestor was changed or removed. - Mark replaced items
@Deprecatedinstead of deleting them. - Keep helper classes
publicorprivate.
Exercise
A released version of your package contains global static Boolean isValid(String code). In the next version you want to change the parameter to Id recordId. What should you do?
Show answer
Don't change the signature. Add a new global overload, such as isValid(Id recordId), and mark the old method @Deprecated. Global signatures are locked after release, so the old method must keep working for subscribers who call it.
Exercise
Which of these can you remove in a new released version without breaking the managed-package rules: a private helper method, a public class used only inside your package, or a global interface?
Show answer
You can remove the private helper method and the public class. Subscribers can't reference them. The global interface is part of the supported API and is locked, so it can't be removed.