Sub topic 13
Namespaces and when to use them
This chapter assumes you can already create an unlocked package, edit sfdx-project.json, and build a package version with the Salesforce CLI. It also assumes you know what a Dev Hub is and have used scratch orgs.
What a namespace is
A namespace is a short identifier, 1 to 15 alphanumeric characters, that Salesforce prefixes to the names of the components in a package. It must begin with a letter. It cannot end with an underscore or contain two consecutive underscores. Salesforce reserves some values, and each namespace is globally unique across all of Salesforce.
When a package has a namespace, its components get names like these:
- A custom object
Invoice__cbecomesacme__Invoice__c. - A custom field
Total__con that object becomesacme__Total__c. - An Apex class
InvoiceServiceis referenced asacme.InvoiceService.
A package without a namespace is called a no-namespace package. Its components keep their plain names, exactly as in an org that has no packages at all.
Registering and linking a namespace
You register a namespace in a Developer Edition org, which is often called the namespace org. You then link that org to your Dev Hub. The steps are:
- Create a Developer Edition org. It is not a scratch org and not a sandbox.
- In Setup, open Package Manager and choose Edit in the Namespace Settings section. Enter and check the namespace, then save.
- Log in to your Dev Hub. Open the Namespace Registries tab, choose Link Namespace, and log in to the namespace org.
Each namespace belongs to one namespace org. One Dev Hub can link many namespaces. The same namespace cannot be reused for a second, unrelated product line later, so choose it with care.
Declaring the namespace in the project
The namespace is declared in the project file, not in the package record. The top-level namespace property applies to every package directory in the project.
{
"packageDirectories": [
{
"path": "force-app/core",
"package": "Core",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"default": true
}
],
"namespace": "acme",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}When you create the package, you tell the CLI whether it is namespaced. Use --no-namespace for a no-namespace package. Otherwise the package takes the namespace from sfdx-project.json.
sf package create --name Core --package-type Unlocked --path force-app/core --target-dev-hub MyDevHubThe namespace property must match a namespace linked to your Dev Hub. If it does not, sf package create fails.
The key limit: one namespace per project
A single sfdx-project.json has one namespace value. Every package in that project either uses it or has no namespace. You cannot give Core the namespace acme and Sales the namespace acmesales in the same project file.
This matters for how you structure repositories, as the next diagram shows.
No-namespace versus namespaced packages
The choice changes how you write code and how the packages behave in an org.
| Aspect | No namespace | Namespaced |
|---|---|---|
| Component names | Plain names | Prefixed with `ns__` |
| Name clashes | Clash with existing org components of the same name | Isolated by the prefix |
| Moving existing metadata in | Straightforward, names stay the same | Existing references must change to the prefixed names |
| Typical use | Internal org organisation | Distributing to other orgs, or strict separation |
| Namespace org required | No | Yes |
| Can be moved to another namespace later | Not applicable | No |
Why most internal teams choose no namespace
If the goal is organising your own org's metadata, a namespace usually adds cost without benefit. Existing Apex, flows, formulas, reports, and integrations refer to components by their plain names. Adding a namespace means all of those references need updating. External systems that call your REST endpoints or query fields through the API would also see new names.
For migration of existing metadata, no-namespace packages are the common choice because they let you move components into packages without renaming anything.
When a namespace is the better choice
- You distribute the package to customers or to orgs your company does not control.
- You need to avoid name collisions with metadata from other vendors or other teams.
- You want components to be clearly identifiable as belonging to your package in the subscriber org.
- You plan a managed package later. Managed packages require a namespace.
Namespaces in scratch orgs
A scratch org can carry the project's namespace, or it can leave it out. This is set in the scratch org definition file with the noNamespace option.
{
"orgName": "Acme dev scratch",
"edition": "Developer",
"noNamespace": true
}By default, a scratch org created from a project that has a namespace adopts that namespace. This lets you develop and test code exactly as it will appear when the package is installed. Setting noNamespace to true creates the scratch org without the namespace.
Be careful with this setting. Code written in a namespaced scratch org may rely on behavior that differs from an org with no namespace. A common case is Apex code that builds field or class names as strings. Test in an org that matches your production target.
Cross-namespace references
Packages in different namespaces can depend on each other, but access is restricted by Apex visibility:
- A
globalclass or method in a namespaced package can be called from other namespaces. - A
publicclass or method is visible only inside the same namespace. - In an unlocked package,
publiccode is visible to all code in the same namespace, which includes other packages sharing it.
If Sales depends on Core and both share the namespace acme, Sales can call public methods in Core. If they are in different namespaces, Core must expose what Sales needs through global.
Because the namespace property is project-wide, packages that share one namespace live in one project. Packages with different namespaces live in different projects and depend on each other by package version ID or alias.
{
"package": "04tXXXXXXXXXXXXXXX",
"versionNumber": "1.0.0.LATEST"
}In practice you declare such a dependency with the subscriber package version ID, or with a packageAliases entry for it, in the dependencies array of the dependent package.
Common errors
- Namespace not linked to the Dev Hub.
sf package createorsf package version createreports that the namespace is not valid for the Dev Hub. Link the namespace org in the Namespace Registries tab. - Package created with the wrong choice. A package created with
--no-namespacestays no-namespace. A package created with a namespace stays namespaced. You cannot convert one to the other. You create a new package and migrate the metadata. - Hard-coded namespace prefixes. Code that contains
acme__literals breaks in a scratch org withnoNamespaceset. Avoid literals. UseSchemadescribe calls or theUserInfoandTypeclasses to resolve names where you must build them dynamically. - Component names exceed limits. The namespace prefix counts toward name length limits on some metadata. A long namespace on a long field name can fail to deploy.
- Mixing namespaces in one project. Only one
namespacevalue exists persfdx-project.json. Splitting into separate projects is the supported approach.
Decision checklist
Ask these questions in order:
- Will anyone outside your organisation install this package? If yes, use a namespace.
- Do you intend to publish a managed package from the same code? If yes, use a namespace.
- Are you reorganising the metadata of your own org? If yes, default to no namespace.
- Do you have two product lines that must not share names? If yes, use separate projects, each with its own namespace.
Exercise
Your team is moving the existing metadata of its production org into unlocked packages. The org's Apex, flows and reports refer to components such as Invoice__c by plain names. Should you use a namespace, and why?
Show answer
Use no namespace. A namespace would rename every component to a prefixed form such as acme__Invoice__c, so all existing Apex, flows, reports and integrations would need updating. A no-namespace package keeps the names unchanged, so the metadata can move into packages without rewriting references.
Exercise
A single sfdx-project.json defines the packages Core and Sales. Can Core use the namespace acme while Sales uses acmesales? If not, what do you do?
Show answer
No. The namespace property is set once at the top level of the project file, and every package in that project either uses it or has none. To use two namespaces, put the packages in separate projects, each with its own namespace, and declare the dependency from one package to the other through its package version ID or alias.