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.