Background
I'm starting to get serious about breaking my org up into a collection of unlocked packages. I've made a few unlocked packages, but have encountered a bit of a roadblock when it comes to some particular setup objects.
In the code for a package I'm trying to create
- I'm executing a query on
Group
to get aQueue
- I'm then creating a
Case
and assigning the previously queriedQueue
This sends out emails and is part of our exception notification strategy.
After a round of research, I was reminded that Group
is a setup object. We can use sfdx to pull the metadata for Group records (as well as for Queues and Roles). Since the Queue is being used elsewhere (it wasn't created for the project I'm packaging), I know that I need to do one of two things:
- Include the metadata in a designated "unpackaged" directory1, and make sure there are no compile-time references to it
- Include the metadata in another unlocked package, that my current project would then have a dependency on
Shoutout to David Reed for his article on unpackaged metadata in 2GP, which also pinned down the order of operations (unpackaged metadata is deployed after the packaged metadata, but before tests are run).
My unpackaged directory structure looks like
unpackaged ->
queues ->
IT_Case_Queue.queue-meta.xml
The Question
I'd much prefer to keep this package free of explicit dependencies, so I've included my target Queue and all of the Role
s that it mentions as unpackaged metadata.
Since I don't have any compile-time references to that specific Queue, just a query (Group itQueue = [SELECT Id FROM Group WHERE Name = 'IT' AND Type = 'Queue'];
), the code in my package is able to be compiled.
When it comes to running my tests, though, I'm running into a query exception
System.QueryException: List has no rows for assignment to SObject
This only happens when I try to run tests when creating a package version with the -c
flag
sfdx force:package:version:create -x -p "Project Name here" -f .\config\project-scratch-def.json -c -w 30
.
When the tests are run when I sfdx force:source:deploy
the same code to a sandbox, the query does return my expected 1 row of data (so the filters are correct).
Why am I getting this error when I'm almost 100% certain that the queue should exist prior to my tests being run? Am I missing something here, or is it just Salesforce being Salesforce (and releasing half-baked features)?
1: This took me a while to realize that in addition to including the
"unpackagedMetadata":{"path": "unpackaged"}
in my default package directory entry in sfdx-project.json, I also needed to create another, separate package directory entry {"path": "unpackaged", "default": false}
. I'll tell you, the error that sfdx spits out (The package root directory is not a source directory) is extremely unhelpful. The documentation also does a poor job of conveying this requirement.