Sub topic 16
Moving existing metadata into packages
This chapter assumes you can read sfdx-project.json, have a Dev Hub, and have created a package version. It also assumes you know how to run Apex tests and read coverage results.
Why migrate in slices
Most orgs hold years of metadata with no boundaries. Moving all of it into packages in one step fails for a predictable reason. Packages cannot have circular dependencies, and old code is full of hidden references. A safer approach is incremental. You extract one cohesive slice at a time, ordered from the least-depended-upon metadata to the most.
Step 1: Find a slice
Start with shared, low-level metadata such as utility Apex classes, custom metadata types and common objects. These have few dependencies on other code, so they become your base package (for example, Core). Domain packages such as Sales come later and depend on it.
Step 2: Move the files
Keep the history by using git mv, then register the directory in sfdx-project.json.
mkdir -p force-app/core
git mv force-app/main/default/classes/StringUtils.cls force-app/core/classes/StringUtils.cls
git mv force-app/main/default/classes/StringUtils.cls-meta.xml force-app/core/classes/StringUtils.cls-meta.xml
sf package create --name Core --package-type Unlocked --path force-app/core --no-namespace --target-dev-hub MyDevHubCreate the classes folder under force-app/core first if it doesn't exist. Move each class together with its -meta.xml file.
Step 3: Build and fix
Run sf package version create with --code-coverage. The first build usually fails. Typical causes are:
- A moved class refers to a field or class still in the old directory. Move it too, or put it in a package that this one depends on.
- Two packages would each need something from the other. Move the shared piece into a third, lower-level package.
- Tests that relied on org data or on metadata that is not in the package. Make them create their own data.
- Coverage below 75% because tests stayed behind in the old directory. Move the tests with the code they cover.
Step 4: Install over existing metadata
When you install the package in production, components with the same API names that are already in the org are adopted by the package. They are not duplicated. From then on, change them only through new package versions.
Practical rules
- Keep each migration step small enough to review in one pull request.
- Leave unmigrated metadata in the original directory. It remains deployable as before.
- Order
packageDirectoriesso that dependencies come first. - Test the install in a sandbox that mirrors production before you promote anything.
Exercise
Your Sales classes call a utility class that is still in the old directory. The Sales package version fails to build. What are two ways to fix this, and which one fits a long-term design better?
Show answer
You can move the utility class into the Sales package, or move it into a Core package that Sales declares as a dependency. Core is usually better when other domains also use the utility, because it avoids duplication and keeps the dependency direction one-way.