Sub topic 2
What makes a package managed: namespace, IP protection, and upgrade rules
This chapter assumes you know basic Salesforce concepts (orgs, metadata, Apex) and Salesforce DX basics such as scratch orgs and source format. It explains the rules that make a package managed. You will need them before you configure a Dev Hub and a project.
The three properties of a managed package
A managed package is a package that you build in a Dev Hub org and distribute to other orgs, called subscriber orgs. Three properties set it apart from an unlocked package.
- Namespace. A namespace is a prefix of 1 to 15 alphanumeric characters that Salesforce assigns to your package components. Custom objects, fields, and Apex classes get the prefix and a double underscore, such as
acme__Invoice__c. The prefix prevents name collisions with the subscriber's own metadata. - IP protection. Subscribers cannot see or edit most of your Apex code. Apex classes and triggers in a released package are hidden. Subscribers can still use anything you mark
global. - Upgrade rules. After a version is released, you can change only some things. These limits keep the subscriber's customizations and integrations working when they upgrade.
Namespace in practice
Every component in the package carries the namespace. Inside the package, you write Invoice__c. In a subscriber org, the same field appears as acme__Invoice__c.
public with sharing class InvoiceService {
public static Integer countOpenInvoices() {
return [SELECT COUNT() FROM Invoice__c WHERE Status__c = 'Open'];
}
}In a subscriber org, other code reaches the class as acme.InvoiceService. That works only if the class and method are declared global. Here the class is public, so it is usable only by code inside your package.
Global versus public
The access modifier decides what becomes your public API.
| Modifier | Visible to subscriber code | Can change after release |
|---|---|---|
| `global` | Yes | Very limited. You cannot remove or change the signature of a global method. |
| `public` | No, only your package | Yes. You can refactor or delete it. |
Treat global as a contract. Expose the smallest surface you can.
Upgrade rules after release
Once a version is released, these rules protect subscribers.
- You cannot delete a released component in the same way you can in a development org. Most deletions are blocked or have a special process.
- You cannot remove or change the signature of
globalApex classes, methods, and interfaces. - You cannot change the data type of a custom field.
- You can add new components, add new
globalmembers, and change the logic inside methods.
A beta version has none of these limits. You can still change it freely, but you cannot upgrade subscribers from it. Promotion to released is the point where the rules begin to apply, and a later chapter covers it.
How the rules fit together
Exercise
A released managed package contains a global method acme.InvoiceService.total(Id accountId). You want to change its parameter to a list of Ids. Can you edit the method signature in the next version? What should you do instead?
Show answer
No. You cannot change the signature of a global method after release. Add a new global method that takes the list of Ids. Keep the old method, optionally marking it deprecated. Subscriber code that calls the old method keeps working.