Sub topic 11
Ancestry, Upgrades, and What You Can Change Between Versions
This chapter assumes you can read sfdx-project.json, create a package version with the sf CLI, and tell a beta version from a released one. It covers how Salesforce decides which version can upgrade which, and which changes it blocks along the way.
What ancestry is
Every package version has an ancestor, the earlier version it builds on. Following the ancestor links back gives the package's version lineage. Salesforce uses this lineage to decide whether an install is an upgrade, and to compare your changes with what subscribers already have.
A subscriber is an org that has installed your package. Once a subscriber has a released version, only a version descended from it can upgrade that org.
Setting the ancestor
You set the ancestor in sfdx-project.json with ancestorVersion or ancestorId. Use ancestorVersion for a version number, or HIGHEST to pick the highest released version.
{
"packageDirectories": [
{
"path": "util",
"package": "Utils",
"versionName": "Utils 1.2",
"versionNumber": "1.2.0.NEXT",
"ancestorVersion": "1.1.0",
"default": true
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"Utils": "0Ho000000000001AAA"
}
}In this file, the new 1.2.0 version names 1.1.0 as its ancestor. Use ancestorId instead if you want to name the ancestor by its 04t ID or alias.
Ancestry is mainly a managed package concern. Unlocked packages can upgrade from any earlier version, so they do not need ancestors. For a managed package, a version with no ancestor is a new branch that cannot upgrade existing subscribers.
What you can change between versions
In a released managed package, some components are protected so that subscribers' code and data keep working. These changes are the usual ones to hit:
- You cannot delete a global Apex class, method, or interface member once it has shipped in a released version.
- You cannot remove or rename a custom field or object that was in a released version.
- You cannot change the data type of a released custom field.
- You cannot add an abstract method to a global class that subscribers may extend.
Beta versions are not restricted this way. Unreleased changes can be removed freely, which is why you should treat a version as a draft until you promote it.
If you need to retire a global Apex member, mark it @deprecated. Deprecated members stay in the package but are hidden from new subscribers' code.
global with sharing class Greeter {
@deprecated
global static String greet() {
return 'Hello';
}
}Checking before you promote
Because released versions are permanent, check the lineage before you promote. Create the version, then confirm that the ancestor is the one you intended. The sf package version create command fails if your changes break the ancestor's protected components. Fix those errors while the version is still a beta.
sf package version create --package Utils --installation-key-bypass --wait 20 --code-coverage --target-dev-hub myDevHub
sf package version report --package "[email protected]" --target-dev-hub myDevHubThe report shows the ancestor of the version. If it shows the wrong one, change ancestorVersion and build a new version.
Common upgrade errors
| Symptom | Likely cause | Fix |
|---|---|---|
| Install fails as a downgrade | The target version number is lower than the installed one | Install a higher version |
| Version cannot upgrade a subscriber | The subscriber's version is not in the new version's lineage | Set the ancestor correctly and rebuild |
| Create fails on a removed component | A released global component was deleted | Restore it, or mark it deprecated |
Exercise
Your managed package's released version 1.1 has a global Apex method calculate(). You want to remove it in 1.2. What should you do, and why?
Show answer
Do not delete it. Released global members cannot be removed, so mark the method @deprecated in 1.2. It stays in the package but is hidden from new subscribers' code, and existing subscribers keep working.
Exercise
A new version builds successfully but cannot upgrade an org that has version 1.1 installed. Which setting in sfdx-project.json do you check first?
Show answer
Check ancestorVersion (or ancestorId). The new version must descend from the subscriber's installed version, so the ancestor must be set to 1.1.0 or a version that descends from it.