Sub topic 5
Choosing package boundaries
This chapter assumes you know Salesforce metadata types, scratch orgs and the Salesforce CLI, and that you have read the chapters on package types and sfdx-project.json. Here you decide what goes into each package before you create any.
Why boundaries matter
A package is the unit you version, test, install and depend on. Where you draw the line decides how often a package changes, how long its tests take, and who has to coordinate when it changes. Poor boundaries lead to large packages that nobody can release independently, or to many small packages that depend on each other in circles.
Group by business domain or capability
Put metadata together when it changes for the same reason. Common groupings are:
- A business domain, such as Sales, Service or Billing.
- A shared capability, such as logging, a trigger framework or integration utilities.
- A shared data model, such as the custom objects and fields several teams use.
Avoid grouping by metadata type, such as "all Apex classes" or "all page layouts". A change to one feature would then touch several packages, and no package could stand alone.
Keep dependencies one-directional
Package dependencies must form a tree, not a loop. A package can depend on another only if that other package does not depend back on it. A common layout puts stable, shared metadata at the bottom and feature packages above it.
In this layout, Core must be installed first. This is the order the packageDirectories list in sfdx-project.json expresses: dependencies come before the packages that use them.
Practical rules
- Start with a small number of packages. Splitting a package later is possible but harder than starting coarse.
- Put the most stable metadata in the lowest layer. Everything above it inherits its changes.
- Keep each package deployable on its own with its dependencies. If Sales can't be installed without a manual step, a boundary is probably wrong.
- Match boundaries to ownership. If one team owns a package, that team can release it on its own schedule.
- Watch for hidden references. Apex that calls a class in another package, or a flow that reads a field defined elsewhere, creates a dependency you must declare.
A quick check
Before creating a package, ask these questions:
| Question | Healthy answer |
|---|---|
| Does the package have a clear purpose in one sentence? | Yes |
| Can it be tested without the packages above it? | Yes |
| Do its dependencies point only downward? | Yes |
| Does one team own it? | Yes |
Exercise
A team puts all custom objects in one package and all Apex classes in another. Each class uses objects from the first package, and some objects have triggers whose Apex lives in the second. What is wrong with this boundary, and how would you fix it?
Show answer
It groups by metadata type, and it creates a circular dependency: the Apex package depends on the objects, and the objects depend on the trigger Apex. Regroup by domain or capability so each package holds the objects, Apex and triggers for one area. Move shared objects into a lower-level Core package that feature packages depend on.