Sub topic 2
Unlocked, unmanaged and managed packages compared
This chapter assumes you know basic Salesforce metadata (objects, fields, Apex classes, permission sets), the Salesforce CLI, scratch orgs, and Apex tests with code coverage. It compares the three package types and the older change-set approach, so you can pick the right one before you create anything.
Why the type matters
A package is a container of metadata with a defined boundary. The type you choose decides who can see the code, whether the contents can be edited after installation, whether you can upgrade in place, and how you remove it. Changing type later is expensive. You usually have to rebuild the package, so decide early.
Salesforce has these ways to move metadata between orgs:
- Change sets are point-and-click deployments between related orgs. They have no version history and no dependency tracking.
- Unmanaged packages are a one-time copy of metadata. After installation the components belong to the target org and the package link is gone.
- Unlocked packages are versioned, upgradeable units that you build from source control. The components stay editable in the target org.
- Managed packages are versioned, upgradeable units, mainly for distribution. They use a namespace and can hide the Apex source from subscribers.
Change sets and unmanaged packages
A change set sends selected components from one org to another. You choose each component by hand, and Salesforce records no version. If a component depends on another one you forgot to add, the deployment fails, or the target ends up in an inconsistent state. Change sets also only work between orgs that are connected by a deployment connection, usually a sandbox and its production org.
An unmanaged package is a bundle that you can install anywhere with a URL. It is useful for sharing templates and samples. After installation:
- The components are ordinary org metadata. Nothing tells you they came from the package.
- You cannot upgrade it. Installing a newer copy creates duplicates or overwrites components, depending on the situation.
- You cannot uninstall it as a single unit. You delete the components one at a time.
Because there is no lifecycle, unmanaged packages do not help with organising a long-lived org.
Unlocked packages
An unlocked package is created from source in a Salesforce DX project and built with a Dev Hub. Each build produces a package version, an immutable snapshot with a version number. Key properties:
- Upgradeable. You install a newer version over an older one. Salesforce works out what was added, changed, or removed.
- Editable in the target org. A subscriber, including your own admins, can change the installed components directly. The next upgrade overwrites those changes for components that the package owns. This is why teams treat source control as the single source of truth.
- Source is visible. Apex code in the package is readable in the target org.
- Optional namespace. You can create the package with a namespace or with no namespace. A no-namespace package is common for internal use because API names stay the same as the existing org metadata.
- Dependencies. One package can depend on specific versions of others.
- Removable. You can uninstall the whole package, which deletes the components it owns.
The package is built in a Dev Hub org, but it is installed into any org you target, such as a sandbox, a scratch org, or production. Unlocked packages are meant for internal organisation and delivery of your own metadata.
Managed packages
Managed packages are designed for ISVs (independent software vendors) who sell or distribute apps through a platform such as AppExchange. They share the version and upgrade model with unlocked packages, with these extra characteristics:
- A namespace is required. Every component name gets the prefix, for example
acme__Invoice__c. - Intellectual property protection. Apex code is hidden from subscribers. Many components are locked against edits.
- Controlled changes. Once a version is released, some changes are not allowed, such as deleting a global Apex method or deleting a custom field that subscribers may use. This protects subscribers who built on top of your package.
- License management. You can control who may use the package.
- Security review. To list on AppExchange, the package must pass Salesforce's security review.
The cost of this protection is rigidity. Because released components are hard to remove, a design mistake can stay with you for years. For an internal team that owns every org, you rarely need this.
Comparison
| Feature | Change set | Unmanaged package | Unlocked package | Managed package |
|---|---|---|---|---|
| Main purpose | One-off deployment | Share a template | Organise and deliver your own metadata | Distribute a product |
| Versioned | No | No | Yes | Yes |
| Upgrade in place | No | No | Yes | Yes |
| Components editable after install | Yes | Yes | Yes | Mostly no |
| Apex source visible | Yes | Yes | Yes | Hidden |
| Namespace | No | No | Optional | Required |
| Dependency tracking | No | No | Yes | Yes |
| Uninstall as a unit | No | No | Yes | Yes |
| Source of truth | The org | The org | Source control | Source control |
Choosing a type
The diagram shows the decision in order.
Use this as a rule of thumb:
- If you sell or distribute to other companies, use a managed package.
- If your own team maintains the metadata over time, use unlocked packages.
- If you only need to hand over a sample once, use an unmanaged package.
- If you must move a single urgent fix and have no package yet, a change set works, but treat it as a stopgap.
Common errors and misunderstandings
- "Unlocked means anyone can change it without consequence." Edits made directly in the org are not tracked in the package. The next upgrade can overwrite them. Make changes in source, then build a new version.
- "Unlocked packages are just unmanaged packages with versions." Unlocked packages track which components they own, so Salesforce can upgrade, remove, and report on them. Unmanaged packages cannot.
- Choosing managed for internal code. Teams sometimes pick a managed package for the code protection, then find they cannot delete released components. If nobody outside your company installs the package, the protection has no benefit.
- Assuming a namespace is required for unlocked packages. It is optional. Pick it once, because you cannot change an existing package from one to the other.
- Creating packages in an org that is not a Dev Hub. Package creation commands need a Dev Hub target. The next chapter covers that setup.
Where this fits
This tutorial focuses on unlocked packages. They give an internal team versioning, dependencies, and clean upgrades without the restrictions of a managed package. The following chapters set up a Dev Hub, define packages in sfdx-project.json, and build the first version.
Exercise
Your company builds an internal sales tool. Four admins and developers maintain it, and it is installed in several sandboxes and in production. Which package type fits best, and give two reasons why a managed package would be a poor choice.
Show answer
An unlocked package. It gives versioned upgrades, dependencies, and removal as a unit, and the source stays visible and editable. A managed package would be a poor choice because it requires a namespace and restricts deleting released components, which makes internal refactoring hard, and its Apex source hiding and license control only help when you distribute to outside customers.
Exercise
A developer fixes a bug directly in a production org on an installed unlocked package's Apex class, and does not update source control. What is likely to happen at the next package upgrade, and what should the developer have done?
Show answer
The upgrade will overwrite the manual change for the component the package owns, so the fix is lost. The developer should have made the fix in source, built a new package version, and installed it.