function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Marcel SantosMarcel Santos 

Create a DX 2nd generation unlocked package depending on a managed package

We have a production organization with several dependencies on managed packages, both developed internally and from AppExchange.

We have several classes, triggers, formulas and validation rules using fields from internal managed packages. To test Salesforce DX I created a trial DevHub org with a few classes, custom objects, validations rules, etc. To test it in a scenario close to what we have in production, I installed our internal managed package in a scratch org and created classes with dependencies on it. As an example, we have a managed package DummyPackageX with an object DummyPackageX__User__c.

I can create scratch orgs, install the DummyPackageX depency on them and write my code without problems. When I finished my development and tried to package the code into a 2nd generation package to install it in a DevHub organization or in a Sandbox to test it I got an error:
 
> sfdx force:package2:version:create --directory force-app --wait 10
Request in progress. Sleeping 30 seconds. Will wait a total of 600 more seconds before timing out. Current Status='InProgress'
Request in progress. Sleeping 30 seconds. Will wait a total of 570 more seconds before timing out. Current Status='InProgress'
ERROR:  MyDummyClass: Invalid type: DummyPackageX__User__c,MyDummyClass: Invalid type: DummyPackageX__User__c,MyDummyClass: Variable does not exist: dummyuser.
It looks like when I try to pack my code using the command above, sfdx can't find the dependency for DummyPackageX. I tried to include the dependency into sfdx-project.json:
 
{
  "packageDirectories": [
    {
      "path": "force-app",
      "default": true,
      "id": "0Ho6A0000008OJHSA2",
      "versionName": "Apr 18 2018",
      "versionNumber": "1.0.0.0"
    }
  ],
  "namespace": "",
  "sfdcLoginUrl": "https://login.salesforce.com",
  "sourceApiVersion": "42.0",
  "dependencies": ["04t0H0000015lgeQAA","04t30000001DUvrAAG","04t6A0000038AJeQAM"]
}

That doesn't seem to be working. Is it possible to have a 2nd generation package depending on a managed package not created with sfdx?

One of the limitations I see for 2nd generation packages in SFDX is:
Second-Generation Packages Cannot Depend on Packages Requiring an Installation Key

Does that mean I can't depend on a managed package from a Salesforce DX project?

Thank you.
Marcel SantosMarcel Santos
I just figured out what was wrong. The dependencies tag should be part of the packageDirectories object, like this:
 
{
  "packageDirectories": [
    {
      "path": "force-app",
      "default": true,
      "id": "0Ho6A0000008OJXXX2",
      "versionName": "May 1 2018",
      "versionNumber": "1.0.0.0",
      "dependencies": [{"subscriberPackageVersionId": "04t0H0000015xyzQAA"}]
    }
  ],
  "namespace": "",
  "sfdcLoginUrl": "https://test.salesforce.com",
  "sourceApiVersion": "42.0"
}

After I fixed that, the package version creation started working.
Negro DevNegro Dev
Hi.
I'm facing the same issue. I already solve the dependency. I have 3 manages packages, and a lot of metadata that depends on those. 
I start with a small test, by only adding the objects, and fields that goes into objects of those packages. But for know I cannot create the package, I'm having an issue like 
ERROR running force:package:version:create:  Ocorreu um erro inesperado. Entre em contato com o suporte do Salesforce e informe o seguinte código de erro: 597678538-17448 (862547712)

Any idea of what could happen or how to debug or know about this issue???
Thanks