Sub topic 1
Package Versions and Dependencies in Salesforce
Prerequisites
Assume you understand Salesforce orgs, metadata, Apex, Salesforce DX projects, and basic sf CLI commands. You should also know that a Dev Hub creates and manages second-generation package versions.
Package types: unlocked, managed, and unmanaged
Salesforce has three common package models:
- Unlocked packages are designed for internal applications and modular deployments. Their metadata can usually be changed in the subscriber org. They support package versions and dependencies, but they do not provide the same namespace and subscriber protection as managed packages.
- Second-generation managed packages (2GP) provide namespaces, protected components, and controlled upgrades. Salesforce creates versions from source in a Dev Hub. This is the preferred managed model for new development.
- First-generation managed packages (1GP) are the older managed model. They are created and versioned through the packaging org and have different upgrade and dependency rules.
- Unmanaged packages are delivery bundles, not upgradeable products. Installing one copies metadata into an org. Later package uploads do not provide a managed upgrade path.
The package type affects what subscribers can edit, how upgrades work, and whether Salesforce protects implementation details.
Reading and choosing version numbers
A package version uses MAJOR.MINOR.PATCH.BUILD, such as 2.1.0.3.
- Increase MAJOR for incompatible changes.
- Increase MINOR for compatible features.
- Increase PATCH for compatible fixes.
- BUILD identifies a particular build of that package version.
In sfdx-project.json, NEXT asks Salesforce to calculate the next build number. LATEST selects the latest available version that matches the requested major, minor, and patch values.
> callout tip > Use explicit versions for reproducible releases. Use LATEST when a development package should follow the newest compatible dependency.
Describing packages in `sfdx-project.json`
Each package directory names its package, source path, version, and optional dependencies:
{
"packageDirectories": [
{
"path": "util",
"package": "Utils",
"versionNumber": "1.1.0.NEXT",
"default": true
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}The package alias lets CLI commands refer to Utils instead of its Salesforce ID.
Creating a package version
Create the package, then build a version in the Dev Hub:
sf package create --name "Utils" --package-type Unlocked --path util --no-namespace --target-dev-hub myDevHub
sf package version create --package Utils --installation-key-bypass --wait 20 --target-dev-hub myDevHubThe first command registers the package. The second creates a version, validates it, and returns a version identifier. A newly created version is normally a beta until you promote it for release.
Exercise
Which package type provides controlled subscriber upgrades and namespace protection?
Show answer
A managed package, especially a second-generation managed package for new development.