Creating an unlocked package version:

sfdx force:package:version:create -p MyPackage --apiversion 48.0 -d force-app -x --wait 10

with classes that depent on Territory2 objects returns the following error(s) for every class that uses an object from the Territory2 "family":

MyClassTest: Variable does not exist: territories,MyClass: Invalid type: Territory2Model

The classes compile, test and run fine on a scratch org with Enterprise Territory Management enabled.

Here is my sfdx-project.json:

  "packageDirectories": [
      "path": "force-app",
      "package": "MyPackage",
      "versionName": "ver 0.1",
      "versionNumber": "0.1.0.NEXT",
      "default": true
  "namespace": "",
  "sfdcLoginUrl": "https://login.salesforce.com",
  "sourceApiVersion": "48.0",
  "packageAliases": {
    "IgusREngine": "0Hoxxxxxxxxxxxx"

I found this here regarding unlocked package support and Territory2: Salesforce Spring ’20 Release Notes

BEHAVIOR CHANGE: For unlocked packaging, the Territory2 type now requires packages without a namespace in all API versions Territory2 depends on Territory2Type, which requires packages without a namespace. This change makes the behavior of Territory2 and Territory2Type consistent, so you can now use Territory2 in unlocked packages.

It looks like it is supported, just not with a namespace and we don't use one. The related DevHub also has Enterprise Territory Management enabled.

1 Answer 1


You need to specify this dependency in a configuration file. The file would look like this:

  "orgName": "Sample Org",
  "edition": "developer",
  "settings": {
    "territory2Settings": {
      "enableTerritoryManagement2": true

It must be specified as part of your sfdx-project.json, or specified on the command-line. I recommend putting it in the project file:

  "packageDirectories": [
      "default": true,
      "definitionFile": "configs/default-config.json",
      "package": "package-name",
      "path": "force-app/package-name",
      "versionName": "ver 1.0 (hopefully)",
      "versionNumber": "1.0.0.NEXT"
    }, ...
  ], ...

You can check all the available metadata types in the Metadata Coverage Report, as well as sample definition files for each metadata you would like to package.

  • I pointed it to my scratch org def with enableTerritoryManagement2 = true and it worked, thanks! Is it actually building with that environment or why is that necessary? I always though it builds with the settings of the dev hub.
    – Frank
    Apr 1, 2020 at 13:39
  • @Frank A package needs to know all of its dependencies. On installation, those dependencies are verified before moving forward. This ensures that the package will install successfully once verification completes.
    – sfdcfox
    Apr 1, 2020 at 13:44

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .