Second-Generation Managed Packages

Second-Generation Managed Packages

Managed 2nd Generation Packaging (Managed 2GP) introduces a fresh approach for AppExchange partners to craft, disseminate, and oversee their applications and metadata. Leveraging Managed 2GP packaging enables you to structure your source efficiently, construct compact modular packages, seamlessly integrate with your version control system, and optimize the utilization of your custom Apex code.


Step 1 -> Create NameSpace (Skip if already registered)

  • An Developer Edition org is required, but it's not enabled as a Dev Hub.
  • If Dev Hub is enabled, you cannot create a namespace directly. In this case, you'll need to create a developer org instead.
  • If Dev Hub is disabled, then navigate to Setup > Package Manager > Edit.

  • Now, enter the necessary details and create a namespace.


Step 2 -> Link the namespace with the Dev Hub org.

  • Login to your Dev Hub org, which could be another organization or a different org. If you don't have one, create a developer org.
  • To check if Dev Hub is enabled, navigate to Setup > Dev Hub and verify whether Dev Hub is enabled or not. If it's not enabled, you'll need to enable it.

  • Scroll down and enable Unlocked Packages and Second-Generation Managed Packages.

  • Now, search for the Namespace Registry.

  • Proceed by clicking on the "Link Namespace" option.

  • Now, enter the credentials of your namespace-registered org(Step 1 Org).
  • Afterwards, you should be able to see all the linked namespaces.


Step 3 -> Launch VS Code with all the metadata (Apex Class, Layout, LWC, etc.) you need for packaging.

  • Log in to your Dev Hub (Step 2 org) Org using the command provided below in the VS Code terminal.

sfdx force:auth:web:login -d -a <devHubAlias>        

  • Now, utilize this command to set your org as the default, replacing "__username" with your org username.

sfdx force:config:set defaultusername='username____'        

  • Open the sfdx-project.json file in VS Code and update the content as provided below. Make sure to replace "packageName" (should be unique) and "namespace" (should be linked with the Dev Hub org). Then, save the file.

{
  "packageDirectories": [
    {
      "path": "force-app",
      "default": true,
      "package": "__PackageName",
      "versionName": "ver 0.1",
      "versionNumber": "0.1.0.NEXT",
    }
  ],
  "namespace": "__Namespace",
  "sfdcLoginUrl": "https://login.salesforce.com",
  "sourceApiVersion": "59.0"
}        

  • Next, utilize the following command to generate the managed package.

sfdx package create --name "__PackageName" --path force-app --package-type Managed        

  • Congratulations, the package has been successfully created. You will receive the package ID. If you encounter any errors, please comment them, and I will respond as soon as possible.
  • You must now create a package version using the provided command. Replace the package name and password. If you don't need a password, you can remove the '--installation-key __Password' part from the command.

sfdx package version create --package "__PackageName" --code-coverage --installation-key __Password --wait 10        

  • If you encounter this error, navigate to sfdx-project.json in VS Code and change 'default: false' to 'default: true', then save the file and retry the same command.

  • Congratulations! Your 2nd Generation managed package is created. Install it in your desired org using the provided link or through commands.


Thank you

Juan Manuel Garrido ?

? Tu empresa trabaja con Salesforce? Te ayudo crecer con el ERP de EGA Futura y con los servicios de Vantegrate ? | Founder, EGA Futura ? | Co-Founder, Vantegrate ?? | Salesforce Partner

11 个月

Such valuable insights in this post, can't wait to level up my Salesforce development skills! Himanshu Sharma

要查看或添加评论,请登录

Himanshu Sharma ??的更多文章

社区洞察

其他会员也浏览了