Sub topic 11
Ancestry, Upgrades, and What You Can Change Between Versions
This chapter assumes you can create and promote a package version with the sf CLI. It also assumes you can read sfdx-project.json and know the difference between beta and released versions. Here you will learn how a package version is linked to the version it upgrades, and how that link limits what you can change.
The working model
An upgrade replaces an installed package version with a newer one in the same org. Salesforce has to decide two things: whether the new version is a legitimate successor of the installed one, and what to do with components that changed or disappeared.
For second-generation managed packages, the first decision is made through ancestry. Each package version records one ancestor, the released version it builds on. The result is a tree of versions. A subscriber can upgrade along any path from their installed version to a descendant.
Ancestry applies to managed packages. Unlocked packages do not use ancestors. Their upgrades are controlled by version numbers and by the upgrade type you choose at install time, covered later in this chapter.
In this tree, a subscriber on 1.0.0 can upgrade to 1.1.0, 1.2.0, or 2.0.0. A subscriber on 1.2.0 can move forward only to versions that descend from 1.2.0. The 2.0.0 branch is not on their path, so Salesforce will not treat it as an upgrade for them.
Declaring an ancestor
You declare the ancestor in the package's entry in sfdx-project.json. Use ancestorVersion to name it by version number, or ancestorId to name it by alias or ID. Use one of the two, not both.
{
"packageDirectories": [
{
"path": "app",
"package": "Sales App",
"versionName": "Sales App 2.1",
"versionNumber": "2.1.0.NEXT",
"ancestorVersion": "HIGHEST",
"default": true
}
],
"namespace": "acme",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"Sales App": "0Ho000000000002AAA"
}
}The value HIGHEST means "the highest released version of this package". It is convenient for a linear history. If you branch, name the ancestor explicitly, for example "ancestorVersion": "2.0.0". Give the major, minor, and patch numbers. The build number is not part of the value.
Some rules to keep in mind:
- The ancestor must be a released version. A beta version cannot be an ancestor.
- The new version number must be higher than its ancestor's.
- The first version of a package has no ancestor. When you create it, pass
--skip-ancestor-checktosf package version create. - Ancestry is recorded when you create the version. You cannot edit it afterwards, so a wrong ancestor means creating a new version.
Inspecting ancestry
Before you release, check that the tree looks the way you intended.
sf package version displayancestry --package "Sales App" --target-dev-hub myDevHub
sf package version create --package "Sales App" --installation-key-bypass --wait 20 --code-coverage --target-dev-hub myDevHub
sf package version promote --package "Sales [email protected]" --target-dev-hub myDevHubThe displayancestry command prints the tree. Add --dot-code if you want Graphviz output you can render as an image.
Run it before promotion, because promotion is permanent. A released version stays in the ancestry tree. A mistaken ancestor on a released version cannot be repaired.
What you can change between versions
The freedom you have depends on the package type and on whether the version you are changing has been released. Beta versions are disposable. Released versions are a contract with your subscribers.
| Package type | Ancestry | Removing a released component | Changing the public surface |
|---|---|---|---|
| Unlocked | Not used | Allowed. The component is deleted or deprecated in the org on upgrade, depending on the upgrade type. | Free, because subscribers own the org and can adapt. |
| Second-generation managed | Required for upgrade, via `ancestorVersion` or `ancestorId` | Restricted. Deprecate instead of deleting. | Restricted. Global Apex signatures must stay compatible. |
| First-generation managed | Linear, managed through patch and upgrade versions | Restricted | Restricted |
For a managed package, the practical rules are:
- Keep every
globalApex class, method, and interface member that you released. Changing a signature or removing one breaks code in subscriber orgs that calls it. - Mark things you no longer want with
@deprecated. A deprecated item still exists, but new subscriber code is discouraged from using it. - Don't change the data type of a released custom field. Add a new field and migrate instead.
Which component types can be removed differs by type and changes between releases. Check the current packaging documentation for the specific type before you rely on removal.
Upgrading unlocked packages
Because unlocked packages have no ancestry, the install command decides how removals are handled. The --upgrade-type flag has three values:
| Value | Behavior for components removed from the new version |
|---|---|
| `Mixed` (default) | Deletes components that can be deleted and deprecates the rest. |
| `DeprecateOnly` | Marks removed components as deprecated and leaves them in the org. |
| `Delete` | Deletes removed components. |
sf package install --package "[email protected]" --upgrade-type DeprecateOnly --target-org myOrg --wait 10 --no-promptDeprecateOnly is the safer choice when you are not sure whether anything in the org still uses the removed components. Deleting a field also deletes its data, so use Delete only when you have checked.
Treat an installed version as something you move forward from. Don't plan on rolling back by installing an older version. If a release is bad, ship a new, higher version that fixes it.
Version-aware Apex and upgrades
A subscriber's code is saved against the package version that was installed at the time. This is the packageVersions element in a component's -meta.xml file. After an upgrade, that code keeps seeing the old version of your package's behavior unless the subscriber updates the reference.
In your own managed package, you can use System.requestVersion() to keep the old behavior for older callers. This lets you change behavior in a new version without breaking an existing caller. The earlier chapter on version-aware Apex covers the details. The point for upgrades is that behavior can change between versions even when the public signatures stay the same.
Common errors and what they mean
| Symptom | Likely cause | Fix |
|---|---|---|
| Version creation fails because the ancestor was not found or is not released | The named ancestor is a beta, or the number is wrong | Promote the ancestor, or correct `ancestorVersion` |
| Version creation fails because the new number is not higher | `versionNumber` is not above the ancestor's | Raise the major, minor, or patch number |
| First version of a managed package fails an ancestor check | No ancestor exists yet | Pass `--skip-ancestor-check` for the first version only |
| An upgrade is not offered to a subscriber | The installed version is not an ancestor of the new version | Run `displayancestry`. Choose an ancestor on the subscriber's path next time |
| Unlocked upgrade removed something you wanted to keep | Default `Mixed` deleted a removed component | Use `DeprecateOnly`, or keep the component in the new version |
<br>
Decision criteria
- Use
ancestorVersion: "HIGHEST"when each release builds on the last one. Name the ancestor explicitly when you maintain two lines, such as a 1.x line that gets fixes while 2.x is the new work. - Prefer deprecation to removal in managed packages, even when removal is allowed. Subscribers get time to move.
- For unlocked packages, choose the upgrade type per environment. For example, use
Deletein scratch orgs andDeprecateOnlyin production.
Exercise
Your managed package has released versions 1.0.0, 1.1.0, and 2.0.0. You now need a bug-fix release for customers who are still on the 1.x line. You create version 1.1.1 with "ancestorVersion": "HIGHEST". What is wrong, and what should you set instead?
Show answer
HIGHEST resolves to the highest released version, which is 2.0.0. The fix release would descend from 2.0.0 and would not be on the upgrade path of 1.x subscribers. Set "ancestorVersion": "1.1.0" so that 1.1.1 descends from 1.1.0. The version number must also be higher than the ancestor's, and 1.1.1 is.
Exercise
You upgrade an unlocked package and the new version no longer contains a custom field that exists in the target org. You want the field and its data to stay in the org for now. Which --upgrade-type should you pass?
Show answer
DeprecateOnly. It marks the removed field as deprecated and leaves it in the org. Delete would remove the field and its data. The default Mixed deletes components that can be deleted, so it could also remove the field.