Sub topic 15
Org-dependent unlocked packages
This chapter assumes you have read the earlier chapters on package types, sfdx-project.json, package creation, and package version creation. You should be comfortable with sf package create, sf package version create, and the difference between beta and released versions.
The problem standard packages cannot solve
A standard unlocked package must be self-contained. When you build a package version, Salesforce checks it in a clean environment that contains only the package and its declared dependencies. If an Apex class references a custom field that lives outside the package, the build fails.
That rule is hard to meet in an established org. Some metadata is shaped by the org itself, such as record types on standard objects, settings that depend on enabled features, and objects created years ago by hand. You cannot always move that metadata into a package, and you cannot always recreate it in a scratch org.
An org-dependent unlocked package relaxes the rule. It can reference metadata that exists in the target org but not in the package. Salesforce does not check those references at build time, so the target org must supply them when you install.
What changes compared with a standard unlocked package
| Aspect | Standard unlocked package | Org-dependent unlocked package |
|---|---|---|
| Self-contained | Yes, validated at version creation | No, may reference unpackaged org metadata |
| Dependency check on unpackaged metadata | At version creation | At install time, in the target org |
| Portable across orgs | Yes | Only to orgs that contain the required metadata |
| Typical home | Greenfield or well-modularised code | Legacy orgs, early migration steps |
Version numbers, NEXT, promotion, and installation commands work as they do for any other unlocked package.
Creating an org-dependent package
You choose this behaviour when you create the package record, using the --org-dependent flag.
sf package create --name LegacyReports --package-type Unlocked --path force-app/legacy-reports --no-namespace --org-dependent --target-dev-hub MyDevHubThe command adds a packageAliases entry to sfdx-project.json and a record in the Dev Hub, as it does for any package. The packageDirectories entry looks the same as for a standard package:
{
"packageDirectories": [
{
"path": "force-app/legacy-reports",
"package": "LegacyReports",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"default": true
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}Nothing in this file marks the package as org-dependent. The setting lives on the package record in the Dev Hub.
Building and installing a version
You build a version with the same command as before:
sf package version create --package LegacyReports --installation-key-bypass --wait 20 --target-dev-hub MyDevHubThe build succeeds even though the source mentions metadata that is not in the package. That is the main benefit, and also the main risk. A missing reference no longer shows up when you build, so it shows up when you install.
Before you install into an org, confirm that the org already contains everything the package references. A sandbox that is a recent copy of production is the usual place to test this.
sf package install --package "[email protected]" --target-org MySandbox --wait 20If the referenced metadata is missing, the install fails and reports the unresolved component. Add the metadata to the org, or add it to the package or to a dependency, and install again.
Promotion and production installs
Promotion works the same way. You promote a beta version to released before you install it in production:
sf package version promote --package "[email protected]" --target-dev-hub MyDevHub
sf package install --package "[email protected]" --target-org MyProdOrg --wait 20Test the version in a sandbox that matches production first. Because the build does not catch missing references, a green build tells you little about whether production will accept the package.
When to use an org-dependent package
Use one when the alternative is not packaging the metadata at all.
- Migrating a legacy org. You can package the code that references old unpackaged objects now, then move those objects into packages later.
- Org-shaping features and settings. Some metadata depends on features or settings that you cannot reproduce in a scratch org, so a standard build would fail.
- Org-specific configuration. The package holds metadata that only makes sense in one org or a family of related orgs, such as one production org and its sandboxes.
Prefer a standard package when you can. A standard package can be installed in any org, and its build proves that it is complete. Treat an org-dependent package as a stepping stone and plan to remove the dependency on outside metadata over time.
Gotchas and failure modes
- Late failures. Missing references appear at install time, which can be in the middle of a release. Run the install in a sandbox first.
- Hidden coupling. Nothing in the package says which outside metadata it needs. Document those requirements, for example in the package's README.
- Scratch orgs. A scratch org you create from a definition file may not contain the outside metadata, so tests that need it can fail there. You may have to deploy that metadata to the scratch org yourself, or run those tests in a sandbox.
- Dependencies between packages. A standard package that depends on an org-dependent package inherits the org dependency in practice, because installing it needs the same outside metadata. Keep org-dependent packages at the edges of your dependency graph where you can.
- No portability. Do not install an org-dependent package into an org you have not checked. Different orgs have different custom fields, record types, and settings.
Moving from org-dependent to standard
A common path looks like this:
- Create an org-dependent package that holds the code and metadata you can package today.
- Identify the outside metadata it references, such as objects, fields, and record types.
- Create standard packages for that metadata and move it in, one domain at a time.
- Declare those packages as dependencies of the original package.
- When nothing outside the package graph is referenced, create a new standard package for the same metadata and retire the org-dependent one.
Step 5 requires a new package, because the org-dependent setting cannot be turned off.
Exercise
A team builds a version of an org-dependent package, and the build passes. The install into production then fails with an unresolved reference to a custom field. Why did the build not catch this, and what should the team do before the next release?
Show answer
Org-dependent packages skip the build-time check of references to metadata outside the package, so the check happens at install time against the target org. The custom field does not exist in production, or is not available there. Before the next release, the team should add the field to production (or package it and declare a dependency), then install the version into a sandbox that mirrors production to confirm it works before promoting and installing in production.
Exercise
You created a package as standard and now need it to reference unpackaged metadata. Can you switch it to org-dependent with a flag on the next version create?
Show answer
No. The org-dependent setting is chosen when you run sf package create and cannot be changed afterwards. You must create a new package with --org-dependent and move the metadata into it.