Sub topic 5
Choosing package boundaries
This chapter assumes you know Salesforce metadata (objects, fields, Apex classes, permission sets), the Salesforce CLI, scratch orgs, and Git-based development. It also assumes you have seen the sfdx-project.json structure from the previous chapter. Here you decide what goes into each package before you create any.
Why boundaries matter
A package boundary is the line between two sets of metadata that are versioned and deployed separately. Once a package is released, moving metadata across that line takes planning, so the first split is worth getting right.
Poor boundaries cause predictable problems:
- Too coarse. One package holds everything. Every change rebuilds and revalidates the whole org's metadata, and you are back to one large deployment.
- Too fine. Dozens of tiny packages. Each has its own version, dependency list, and release. Build time and coordination cost grow faster than the benefit.
- Tangled. Packages depend on each other in both directions. This is not allowed, so you cannot build them.
A good boundary lets one team change and release one package without coordinating with others.
Rules the platform enforces
Some constraints are not design choices. They shape what you can do.
- A package version can only reference metadata that exists in the same package, in a package it declares as a dependency, or in a standard Salesforce feature.
- Dependencies must form a directed graph with no cycles. If
Salesdepends onCore, thenCorecannot depend onSales, directly or through another package. - A given piece of metadata belongs to exactly one package.
- Each package version is built from the contents of its package directory only. Code in another directory is not visible unless that directory's package is a declared dependency.
If your Apex in Sales calls a class in Core, then Core must be a declared dependency of Sales and the build will fail without it. The next section shows a layered design that follows from these rules.
A layered model
Most successful designs arrange packages in layers. Lower layers are stable and shared. Upper layers change often and depend on the layers below.
A typical layering looks like this:
| Layer | Contents | Change rate |
|---|---|---|
| Utilities | Logging, trigger framework, shared Apex helpers | Low |
| Core | Shared objects (such as Account extensions), common permission sets, shared custom metadata | Low to medium |
| Domain | Sales, Service, Marketing features | High |
| Integration or UI | Connectors, Lightning pages, apps that combine domains | High |
Dependencies point down the table. A domain package never depends on another domain package if you can avoid it. If two domains need to share something, move that shared item down into Core.
Ways to split metadata
There are three common strategies. Pick one as the main rule and apply it consistently.
By business domain
Each package maps to a business area such as Sales, Service, or Billing. This matches how teams are organised, so ownership is clear. It is the most common starting point.
By capability or feature
Each package delivers one reusable capability, such as document generation or a scheduling engine. This suits capabilities that several domains consume.
By technical layer
One package for data model, one for Apex logic, one for UI. This looks tidy but usually causes trouble. A single feature change touches three packages, so you must version and release all three together. Avoid it unless you have a specific reason.
| Strategy | Strength | Weakness |
|---|---|---|
| Business domain | Clear team ownership | Shared objects may be hard to place |
| Capability | High reuse | Needs good interface design |
| Technical layer | Looks simple | One feature spans several packages |
Where does shared metadata go?
Custom objects and fields shared by several domains are the hardest decision. Consider this example. Both Sales and Service use a custom object Contract_Term__c.
- Find every package that references the object.
- If more than one does, move it to a package that all of them can depend on, usually Core.
- Keep fields that only one domain needs in that domain's package, as long as the object is in a lower layer. A package can add fields to an object that a dependency owns.
This works because a package may extend an object defined in a package it depends on. It cannot do this the other way round.
Permission sets follow the same rule. Put a permission set in the package that owns the features it grants. A permission set in Sales cannot grant access to a field that is defined in a package Sales does not depend on.
Checking the design against your project file
Your boundaries appear in sfdx-project.json as package directories. The order matters, because dependencies must be installed first. This example reflects the Core and Sales layout.
{
"packageDirectories": [
{
"path": "force-app/core",
"package": "Core",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"default": true
},
{
"path": "force-app/sales",
"package": "Sales",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"dependencies": [
{ "package": "Core", "versionNumber": "1.0.0.LATEST" }
]
}
],
"namespace": "",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}Core appears first and has no dependencies. Sales lists Core and so must come after it. Directory structure on disk should mirror this: force-app/core and force-app/sales hold separate, non-overlapping files.
Common errors
- Metadata in two directories. The same file path or component name appears under two package directories. Keep each component in exactly one.
- Hidden reference to another package. An Apex class in
Salesuses a class that is still in an undeclared directory. The version build fails with a missing-component error. Either add the dependency or move the class. - Circular dependency. Core uses something in Sales. Move that item down into Core, or introduce an interface in Core that Sales implements.
- Unpackageable metadata. Some metadata types cannot be put in a package. Check the Metadata Coverage report before planning a boundary around a type.
- Splitting too early. A new project with unclear domains should start with few packages. Merging packages later is harder than splitting one.
Practical checks before you commit
Use these questions before you create the package.
- Can one team own this package and release it without asking another team?
- Do all dependencies point downward in the layer table?
- Would a change to one feature normally touch only one package?
- Is the number of packages small enough that you can name each one's purpose in one sentence?
- Can each package be tested on its own in a scratch org that has only its dependencies installed?
If you answer no to any of them, adjust the boundary before building versions.
Exercise
Exercise
Your Service package has an Apex class that reads fields from a custom object Entitlement_Plan__c. The same object is also used by the Sales package, which is defined in a different directory. Currently the object lives in Sales, and Service does not declare any dependency. Name the problem and give a design that fixes it.
Show answer
Service references metadata from Sales, but Service does not depend on Sales, so the Service version build fails. Making Service depend on Sales would couple two domain packages, and if Sales ever needs anything from Service you get a circular dependency. The better design is to move Entitlement_Plan__c into Core (or another lower-layer package), then make both Sales and Service depend on Core. Fields used only by one domain can stay in that domain's package.
Exercise
A colleague proposes three packages: DataModel, ApexLogic and UI, so that each metadata type is separated. Give two reasons this is usually a poor boundary.
Show answer
A single feature change usually touches the object, the Apex, and the UI, so all three packages need new versions and a coordinated release. Also, the packages depend on each other tightly (Apex needs the data model, UI needs the Apex), so teams cannot work or release independently. A split by business domain or capability avoids both problems.