Sub topic 8
Dependencies on metadata, features, and org settings
What this chapter covers
The earlier chapters in this tutorial showed how one package depends on another package with a dependencies entry in sfdx-project.json. A package can also depend on things that are not packages:
- Metadata that lives in the target org but is not in any package.
- Org features such as Multi-Currency.
- Org settings (preferences) that must be on.
This chapter assumes you can read sfdx-project.json and a scratch org definition file, and that you have built a package version with sf package version create. It explains how to declare each kind of dependency, when Salesforce checks it, and what happens when the check fails.
Three kinds of non-package dependency
| Kind | Example | Declared in | Checked when |
|---|---|---|---|
| Metadata outside the package | A custom field on an object that is not in your package | `unpackagedMetadata` in `sfdx-project.json` | Package version creation and install |
| Org feature | Multi-Currency, Person Accounts | `features` in the definition file | Package version creation and install |
| Org setting | Opportunity Teams enabled | `settings` in the definition file | Package version creation and install |
Standard objects, standard fields, and standard metadata types do not need to be declared. Every org has them. The three kinds above matter only when your code or configuration refers to something that is not guaranteed to exist in every subscriber org.
Where the check happens
The platform validates a package version when you create it. It builds the version in a temporary org, compiles the components, and fails the build if a reference cannot be resolved. At install time, Salesforce checks again against the real target org.
flowchart TDReplace the placeholder above with the real diagram below.
Dependencies on metadata that is not in a package
Suppose your Sales App package contains a validation rule that refers to a custom field Region__c on Account. That field was created directly in your production org and belongs to no package. When Salesforce builds a package version in a clean org, Region__c does not exist, so the build fails with a reference error.
You have three options:
- Move
Region__cinto a package and declare a package dependency on it. This is the cleanest option, because the dependency is explicit and versioned. - Put
Region__cin a separate folder and list it asunpackagedMetadata. The platform deploys it to the build org so that compilation succeeds, but it is not added to your package. - Make the package org-dependent (see below), which skips the validation.
Option 2 looks like this:
{
"packageDirectories": [
{
"path": "app",
"package": "Sales App",
"versionName": "Sales App 2.0",
"versionNumber": "2.0.0.NEXT",
"default": true,
"unpackagedMetadata": {
"path": "unpackaged-prereqs"
}
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0",
"packageAliases": {
"Sales App": "0Ho000000000002AAA"
}
}The unpackaged-prereqs folder holds the source for Region__c, in the usual source format (for example unpackaged-prereqs/objects/Account/fields/Region__c.field-meta.xml).
Why option 1 is usually better
An unpackaged prerequisite has no version number. Nothing prevents someone from deleting or renaming it in the target org, and the package cannot tell you which version of it you tested against. A package dependency carries a version, shows up in the install order, and blocks uninstall of the parent while the child still depends on it.
Use unpackagedMetadata when the prerequisite really cannot be packaged. An example is a component type that the metadata coverage report lists as unsupported in packages.
Dependencies on org features
Features are capabilities that an org edition or a support request turns on. Your package may reference objects or fields that exist only when the feature is on. For example, a currency field with CurrencyIsoCode logic needs Multi-Currency.
You declare features in the definition file that you pass when you create the package version. It uses the same format as a scratch org definition file:
{
"orgName": "Sales App package build",
"edition": "Developer",
"features": ["MultiCurrency"],
"settings": {
"opportunitySettings": {
"enableOpportunityTeam": true
}
}
}Save it as config/package-build-def.json and point the build at it:
sf package version create --package "Sales App" --definition-file config/package-build-def.json --installation-key-bypass --wait 20 --code-coverage --target-dev-hub myDevHubThe build org is created with Multi-Currency and Opportunity Teams on. Components that reference currency or team objects now compile.
Dependencies on org settings
A setting works the same way as a feature, but it is a preference that an admin can usually toggle. The settings block in the definition file maps to Metadata API settings types, such as opportunitySettings or lightningExperienceSettings.
Two practical points:
- Settings are for the build only. The package does not turn settings on in a subscriber org. Installation fails, or components misbehave, if the subscriber org has the setting off.
- Some features cannot be turned off after they are enabled. Multi-Currency is one of them. A subscriber org that enables it to install your package keeps it.
What the target org must provide
When you run sf package install, the platform compares the package version's requirements with the org. A typical failure message names the missing item, for example a feature that is not enabled or a field that cannot be found.
sf package install --package "Sales [email protected]" --target-org myOrg --wait 10If it fails, fix the org first, then retry. Do not edit the package to remove the reference unless the reference is a mistake.
Org-dependent unlocked packages
An org-dependent unlocked package skips the check for undeclared metadata dependencies. You create it with the --org-dependent flag:
sf package create --name "Sales App" --package-type Unlocked --path app --org-dependent --no-namespace --target-dev-hub myDevHubUse this when the package is built for one specific org or a family of orgs that share the same unpackaged metadata, such as a sandbox pipeline for a single production org. The trade-off is that the package version does not prove it will install into a clean org. A later chapter covers this package type in detail.
Common mistakes
- Declaring
unpackagedMetadataand assuming it ships. It does not. Subscriber orgs still need the metadata. - Forgetting
--definition-file. Features and settings in a definition file are ignored for the package build unless you pass the file tosf package version create. - Listing a setting without its feature. Some settings only exist when a feature is on. The build org fails to create.
- Using a different definition file for build and for scratch orgs. A scratch org that has more features than the build org hides missing declarations until a customer installs.
- Relying on Dev Hub org features. The Dev Hub's own features do not carry into the build org. Only the definition file counts.
Checklist before you create a version
- Search your source for references to custom metadata that is not in the package. Package it, or add it to
unpackagedMetadata. - List every feature and setting your components require in the definition file.
- Build the version with
--definition-file. - Install the version into a clean test org that has only the documented prerequisites.
- Write the prerequisites in the install notes.
Exercise
Your package contains a validation rule that references a custom field Region_c that exists only in your production org and is in no package. The package version build fails. You add Region_c to a folder and list it under unpackagedMetadata. The build now passes, but installation into a customer's org fails with a missing field error. Why, and what are two ways to fix it?
Show answer
unpackagedMetadata is deployed only to the build org so that the version compiles. It is not part of the package, so subscriber orgs never receive Region_c. Fixes: (1) move Regionc into a package and declare a package dependency on it so that it installs in order, or (2) make sure Region_c is deployed to each target org before the install, and document that prerequisite.
Exercise
A component in your package needs Multi-Currency. Where do you declare this so the version build succeeds, and does that declaration turn on Multi-Currency in the subscriber org?
Show answer
Add "MultiCurrency" to the features array of a definition file and pass the file with --definition-file to sf package version create. The declaration only configures the build org. It does not enable Multi-Currency in a subscriber org, so the subscriber org must already have the feature, and you should document this requirement.