Sub topic 13
Org-dependent unlocked packages
This chapter covers the org-dependent variant of the unlocked package. You should already know how unlocked packages are created, how version numbers work, and how dependencies are declared in sfdx-project.json. You should also be comfortable with the sf package commands for creating and installing versions.
What the variant is
A standard unlocked package is self-contained. Every component it references must be in the package, in a package it declares as a dependency, or in a platform feature that the package version's scratch org can enable. Salesforce checks this when you create a package version. If a custom field referenced by a Flow is missing, version creation fails.
An org-dependent unlocked package drops that rule. It can reference metadata that exists only in the org where it is installed. Typical examples:
- A Flow that reads a custom field created by an older, unpackaged customization.
- An Apex class that uses a record type you never moved into source control.
- A page layout that includes a related list for an object defined outside the package.
The package is no longer portable. It installs only into orgs that already contain the metadata it expects, usually a production org and the sandboxes copied from it.
Why it exists
Many production orgs hold years of unpackaged metadata. Untangling it all before adopting packages is a large project. Org-dependent packages let a team start packaging the metadata they own now. The tangled metadata stays in the org until it can be modularized.
The trade-off is that you give up the guarantee that a package version is self-describing. Version creation can no longer prove the package will install, so you must test the install in an org that mirrors the target.
Creating an org-dependent package
The type is chosen when the package record is created, with the --org-dependent flag. You can't convert an existing standard unlocked package to org-dependent, or the reverse. If you pick the wrong type, create a new package.
sf package create \
--name "Legacy Sales Extensions" \
--package-type Unlocked \
--path legacy-ext \
--no-namespace \
--org-dependent \
--target-dev-hub myDevHubThe command writes an entry for the new package ID into the packageAliases section of sfdx-project.json. The packageDirectories entry looks like any other:
{
"packageDirectories": [
{
"path": "legacy-ext",
"package": "Legacy Sales Extensions",
"versionName": "Legacy Ext 1.0",
"versionNumber": "1.0.0.NEXT",
"default": true
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"Legacy Sales Extensions": "0Ho000000000003AAA"
}
}Nothing in the file says the package is org-dependent. The setting lives on the package record in the Dev Hub. To check it, query the Package2 object with sf data query --use-tooling-api or run sf package list --verbose. Do this before you build on the assumption.
Creating and promoting versions
Versions are created with the same command as for any unlocked package:
sf package version create \
--package "Legacy Sales Extensions" \
--installation-key-bypass \
--wait 20 \
--target-dev-hub myDevHubThree behaviors differ from the standard flow.
- Dependencies on org metadata aren't validated. The scratch org used to build the version doesn't contain your production metadata, so Salesforce can't confirm those references resolve. A reference to a missing field becomes an install-time failure.
- Test results from version creation say little. Apex tests that rely on org-specific data or metadata can't run meaningfully in the build org. Run the package's tests in a sandbox that mirrors production.
- Promotion is easier. Standard unlocked versions need the code coverage threshold (75 percent) before promotion. Org-dependent versions have no such requirement. Check the current Salesforce documentation for the exact rule in your release, because this has changed over time.
Promotion works as before:
sf package version promote \
--package "Legacy Sales [email protected]" \
--target-dev-hub myDevHubA beta version can still be installed into sandboxes and scratch orgs only. Only a released version can be installed into production. That rule is the same for all unlocked packages.
Installing an org-dependent version
Installation uses the same command as any other package:
sf package install \
--package "Legacy Sales [email protected]" \
--target-org myProdSandbox \
--wait 10 \
--publish-wait 5Install-time validation is where missing metadata surfaces. A failed install reports the component and the reference it couldn't resolve, for example In field: field - no CustomField named Account.Region__c found. The usual fixes are:
- The metadata is missing in the target org. Install into an org that has it, or deploy the missing metadata first.
- The metadata exists but under a different API name. Sandboxes that have drifted from production are a common cause. Refresh the sandbox or align the names.
- The metadata should be in the package. Move it into a package directory and create a new version.
Because the package's correctness depends on the target, the same version can install into one sandbox and fail in another. That's the defining risk of the type.
Mixing with other packages
An org-dependent package can still declare dependencies on other packages in sfdx-project.json, and the earlier rules apply. List dependencies before the packages that use them, and decide between a pinned version and LATEST as you would for any dependency.
A reasonable layout for a modernizing org has three layers:
| Layer | Package type | Contents |
|---|---|---|
| Shared base | Standard unlocked | Self-contained utilities, no org assumptions |
| Domain packages | Standard unlocked | Components that have been fully untangled |
| Legacy bridge | Org-dependent unlocked | Components still tied to unpackaged metadata |
Keep the legacy bridge at the end of the dependency chain. Other packages shouldn't rely on it. Check the Salesforce documentation for whether your release allows other packages to depend on an org-dependent package. Avoiding that design is safer either way, because it ties every dependent package to one org's metadata.
Moving off the org-dependent type
The goal is usually to shrink the org dependency over time. A workable approach:
- Identify the unpackaged metadata each component references.
- Move that metadata into a standard unlocked package, or into the same package if it should be owned together.
- Create a new standard unlocked package with the untangled components and deploy it to a scratch org to confirm it builds.
- Retire the components from the org-dependent package in a later version. Check what upgrading removes from the org, because unlocked packages can delete components that disappear from a later version.
Moving a component between packages doesn't happen automatically. Plan the change so the component is never absent from the org or defined twice. Salesforce supports moving metadata between unlocked packages under defined rules, so check the current guidance before you start.
Failure modes
| Symptom | Likely cause | Fix |
|---|---|---|
| Install fails with a missing field or object | Target org lacks the referenced metadata | Deploy that metadata first, or pick a matching org |
| Install passes in one sandbox and fails in another | The orgs have drifted apart | Refresh the failing sandbox from production |
| Version create succeeds but tests fail on install | Tests assume org data not present in the build org | Run and fix the tests in a mirrored sandbox |
| Can't switch an existing package to org-dependent | The type is fixed at package creation | Create a new package with `--org-dependent` |
| A scratch-org install fails | The scratch org has none of the legacy metadata | Seed the scratch org with it, or use a sandbox |
Exercise
A team's standard unlocked package version fails to build because a Flow references a custom field that exists only in production. They decide to recreate the same package as org-dependent. Name two things they must do differently, and one risk they take on.
Show answer
First, they must create a new package with sf package create --org-dependent, because the type can't be changed on the existing package record. Second, they must test the install in a sandbox that mirrors production, because version creation no longer validates references to org metadata. The risk is that the package installs only into orgs that contain the field, so an install into a drifted sandbox, or a scratch org without the field, can fail.
Exercise
Where in sfdx-project.json do you record that a package is org-dependent?
Show answer
Nowhere. The setting lives on the package record in the Dev Hub and is set once with --org-dependent at creation. The project file looks the same as for a standard unlocked package.