Sub topic 8
Dependencies on metadata, features, and org settings
This chapter assumes you can read sfdx-project.json, you know how to declare a package-to-package dependency, and you have created a package version with the sf CLI. Package dependencies are only one kind. A package can also depend on metadata outside it, on Salesforce features, and on org settings.
What counts as a dependency
A component depends on anything it references. Examples are a Flow that reads a custom field, an Apex class that calls another class, or a page layout that shows a field from another package. Salesforce checks these references when you create a package version.
Each reference must resolve in one of three places:
| Where the target lives | What you do |
|---|---|
| The same package | Nothing. Keep both components in the package directory. |
| Another package | Declare a package dependency. |
| The org or a feature | Enable it in the build org, or use an org-dependent package. |
Metadata that is not in a package
Suppose your Flow uses a custom field that exists only in your Dev Hub's source, outside any package. Version creation fails with an error that the field cannot be found. Package version creation runs in a clean build org, so unpackaged metadata is not there.
You have three fixes:
- Move the field into the package.
- Move it into another package and add a dependency on that package.
- Make the package org-dependent, so it can rely on metadata that already exists in the target org. Org-dependent unlocked packages are covered in a later chapter.
Standard objects and standard fields are always present, so referencing them needs no declaration.
Features and org settings
Some components work only when an org feature or setting is on. Multiple currencies and Experience Cloud are examples. The build org is created from a scratch org definition file, so you declare features and settings there.
{
"orgName": "Utils build org",
"edition": "Developer",
"features": ["MultiCurrency"],
"settings": {
"lightningExperienceSettings": { "enableS1DesktopEnabled": true }
}
}Point the package directory at that file with definitionFile:
{
"path": "util",
"package": "Utils",
"versionNumber": "1.1.0.NEXT",
"definitionFile": "config/utils-scratch-def.json"
}You can also pass the file for a single build:
sf package version create --package Utils --definition-file config/utils-scratch-def.json --installation-key-bypass --wait 20 --target-dev-hub myDevHubThe build org then has the feature, so the components validate. The same file is a useful record of what a subscriber org needs.
What happens at install time
The definition file configures only the build org. It does not change the org you install into. If the target org lacks a required feature, the install fails or the affected components fail to deploy. Document the required features and settings for each package version, and check them before installing.
Keeping dependencies explicit
- Prefer package dependencies over org-dependent packages. They are checked at build time and are easier to reproduce.
- Do not rely on a setting you toggled by hand in the build org. Put it in the definition file, so every build is repeatable.
- When a version creation error names a missing component, first decide which package should own it. Then choose the fix.
Exercise
Your Flow in the Utils package references a custom field that lives only in unpackaged source. Version creation fails. Name two ways to fix this without making the package org-dependent.
Show answer
Move the custom field into the Utils package. Or move it into a separate package and declare that package as a dependency of Utils. The build org is clean, so the field must come from the package or one of its dependencies.
Exercise
Utils uses multiple currencies. You add MultiCurrency to the definition file and the version builds. Why might installation into a customer org still fail?
Show answer
The definition file configures only the build org. If the customer org does not have multiple currencies enabled, the components that need it cannot be installed there.