4

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 a Queue
  • I'm then creating a Case and assigning the previously queried Queue

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 Roles 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.

11
  • 1
    Are you able to replicate the problem in a scratch org created from the same definition file (rather than a sandbox)?
    – David Reed
    Jun 9, 2022 at 3:26
  • 1
    And if so, can you contrive an MVR with only a Queue and an Apex test?
    – David Reed
    Jun 9, 2022 at 3:29
  • 1
    Can you put a small repro repository if it is not too much to ask. So we can take this to the Product and open an investigation? Alternatively you can reach to the developer support. Jun 9, 2022 at 16:05
  • 1
    @MohithShrivastava In trying to create the repository to reproduce this, the issue appears to have magically resolved itself. Might be an issue of creating the unlocked package using sfdx 7.147 and then trying to create a version using sfdx 7.152
    – Derek F
    Jun 9, 2022 at 19:20
  • 1
    It's entirely believable that it was a bug in the CLI around deploying unpackaged metadata into the build org.
    – David Reed
    Jun 9, 2022 at 19:36

1 Answer 1

1

The good news is that the issue has been resolved.

The bad news is that I don't know what changed, and I can't reproduce the issue.

My best guesses at what the problem might have been are:

  • Perhaps it's because I didn't use the -e | --no-namespace flag when running sfdx force:package:create -t Unlocked -n <package name> (even though sfdx-package.json had `"namespace": "")
  • Perhaps it was because I created the package using sfdx version 7.147, and was trying to create a package version using sfdx version 7.152
  • Perhaps something happened when I had great difficulty updating to sfdx 7.152 (and I forced the update to quit in the middle of its work several times over 2.5 days before the download finally completed)

The complete list of symptoms I was encountering is:

  • The Queue I was providing as unpackaged metadata was unable to be queried in tests
  • A RecordType I was providing as unpackaged metadata was unable to be queried in tests, did not show up in getRecordTypeInfosByName() in tests, and the RecordTypeId field was not available in tests
  • ContentVersion data created in a test (by the default test user) was unable to be queried (by the default test user), despite my project-scratch-def.json have content enabled being included in the sfdx force:package:version:create command via the -f flag

So for now, the best advice I can give
Is to make sure your sfdx is updated, and try to create a new unlocked package after it is updated.

You must log in to answer this question.

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