Sub topic 16
Moving existing metadata into packages
This chapter assumes you can already create a package, build a version and install it. It also assumes you understand sfdx-project.json and package dependencies. The question here is how to apply that to an org that has years of unpackaged metadata.
Why you migrate in steps
A production org that has never used packages holds metadata with tangled references. Flows read fields from several objects. Apex classes call each other across business areas. Permission sets and page layouts mention everything.
A package version is built in a clean scratch org that contains only the package contents and its declared dependencies. Every reference to a component outside that set breaks the build. That is why a one-time "package everything" migration usually fails. Migrate in small slices, and make each slice build and install on its own.
Plan the migration
Do these steps before you move any files:
- Inventory. Retrieve the org's metadata into the project and group it by business domain or capability.
- Map the dependencies. Note which groups reference which. Choose the direction that leaves the fewest references pointing back.
- Pick the bottom layer first. Shared objects, utility Apex and custom metadata types go into a base package such as Core. Packages that depend on them come after.
- Keep an unpackaged directory. Anything you have not yet moved stays in a directory with no
packagekey. It is still deployable and does not block progress.
The dependency graph between packages must be acyclic. If two domains reference each other heavily, either merge them into one package or move the shared pieces into a lower package that both depend on.
{
"packageDirectories": [
{
"path": "force-app/core",
"package": "Core",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"default": true
},
{
"path": "force-app/service",
"package": "Service",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"dependencies": [
{ "package": "Core", "versionNumber": "1.0.0.LATEST" }
]
},
{
"path": "force-app/unpackaged"
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}This file lists Core first because it is installed first. The unpackaged directory is a normal source directory. It holds what has not been migrated yet.
Retrieve the metadata
Use a manifest to pull a defined slice from the org. This manifest/core.xml lists one custom object and two Apex classes:
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Region__c</members>
<name>CustomObject</name>
</types>
<types>
<members>RegionService</members>
<members>RegionServiceTest</members>
<name>ApexClass</name>
</types>
<version>61.0</version>
</Package>sf project retrieve start --manifest manifest/core.xml --target-org MyProdOrgThe files land in the default package directory. Move them into the directory of the package that should own them. Do this with git mv so the history follows the files.
The migration loop
Each slice goes through the same cycle.
Build the version with coverage so you see test problems early:
sf package version create --package Core --installation-key-bypass --code-coverage --wait 20 --target-dev-hub MyDevHubDevelopers sometimes use --skip-validation to get a fast build while fixing dependencies. A version built that way skips the coverage check and cannot be promoted. Use it only to iterate, then do a full build.
Fix the errors you will hit
| Symptom | Likely cause | Fix |
|---|---|---|
| "Entity not found" or an unknown field during the build | The component references metadata that is neither in the package nor in a dependency | Move the referenced component into the package, or into a lower package you depend on |
| Circular dependency between packages | Two packages each use the other's components | Extract the shared components into a base package |
| Apex tests fail in the clean scratch org | Tests rely on org data, hardcoded IDs or unpackaged metadata | Create test data inside the tests and remove hardcoded IDs |
| Coverage below 75% | Old classes with weak tests are now your problem | Add tests, or leave those classes unpackaged until you can |
| Profile changes are missing after install | Profiles do not package cleanly | Move access settings into permission sets |
Install into an org that already has the metadata
When you install an unlocked package into the org where the metadata already lives, the existing unpackaged components can be adopted by the package. They are not duplicated. This is what makes in-place migration possible. Rehearse it in a full-copy or developer sandbox first, because the install changes which package owns each component. Then promote the version and 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 20Two limits apply:
- Components that belong to a managed package, or to another package that still claims them, cannot be adopted this way.
- If an install fails partway, check the error list before you retry. The cause is usually a component in the package that differs from the org version in a way the install cannot reconcile.
Moving a component between packages
Sometimes a slice was placed wrongly and a component has to move from package A to package B. The order matters. Add the component to B first, and install that new version of B in every org. Only after that, remove the component from A and release a new version of A. If you remove it from A first and upgrade A, the org can delete the component, and its data with it. Check the current Salesforce documentation on moving metadata between unlocked packages before you do this in production, and rehearse it in a sandbox.
Keep the CI pipeline in step
Each migrated slice should go through your normal pipeline. The pipeline validates the new version in a scratch org, then installs it in a sandbox. Do not let people edit packaged components directly in production. The next version install would overwrite the change. Tell the team which components are now owned by which package, and keep the unpackaged directory shrinking.
Exercise
You move a Core Apex class into a package directory, but the version build fails with an "Entity not found" error for a custom field. The field is still in the unpackaged directory. What are two valid fixes?
Show answer
Move the field into the Core package so the class and its field are built together, or move the field into a lower package that Core declares as a dependency. Leaving the field unpackaged does not work, because the build runs in a clean scratch org that has only the package contents and its declared dependencies.
Exercise
Why should you install the destination package before upgrading the source package when moving a component between two unlocked packages?
Show answer
If the source package is upgraded first with the component removed, the upgrade can delete the component from the org, including its data. Installing the destination package first lets it take ownership of the existing component.