How to Create, Promote, and Install a Salesforce 2GP (Second-Generation Managed Package) Using Salesforce CLI

How to Create, Promote, and Install a Salesforce 2GP (Second-Generation Managed Package) Using Salesforce CLI

In this guide, you’ll learn how to use Salesforce CLI to create, promote, and install a Second-Generation Managed Package (2GP) from your Salesforce Dev Org to another. We’ll also cover the steps required to enable Dev Hub, Second-Generation Managed Packages, and Unlocked Packages in your Dev Org.




Prerequisites:

Before you can create and manage 2GP packages, make sure you have the following setup:


  • Two Salesforce Developer Orgs:

Dev Hub Org: This will be the central org to manage your packages.

Target Org: The org where you will install the package.


  • Enable Dev Hub in your Dev Hub Org:

Navigate to Setup → Dev Hub → Enable Dev Hub.


  • Enable Second-Generation Managed Packages and Unlocked Packages in your Dev Hub Org:

Navigate to Setup → Dev Hub → Enable Unlocked Packages and Second-Generation Managed Packages

`

  • Salesforce CLI installed:

Ensure you have Salesforce CLI installed on your local machine. You can download it from the official Salesforce website:Download Salesforce CLI.




Steps to Create, Promote, and Install a 2GP (Second-Generation Managed Package)

Once you’ve enabled the required features, you can proceed to the steps for creating, promoting, and installing the package.



Step 1: Authorize Your Dev Hub Org


First, authorize your Dev Hub Org in Salesforce CLI. This will enable you to manage your packages and create scratch orgs.

sfdx force:auth:web:login -d -a Package2Gp

  • -d: Designates this org as your default Dev Hub.
  • -a Package2Gp: Alias for your Dev Hub Org.

After running this command, a web page will open where you’ll log in to your Dev Hub Org.




Step 2: Create the Package

Now that the Dev Hub Org is authorized, you can create a Managed Package (Second-Generation Package) that will contain your Salesforce components.

sfdx force:package:create --name "Package2GP" --path force-app --packagetype Managed --targetdevhubusername Package2Gp

  • --name "Package2GP": Name of your package.
  • --path force-app: The directory where your code and components are located.
  • --packagetype Managed: Specifies that this will be a Managed Package.
  • --targetdevhubusername Package2Gp: Alias of your Dev Hub Org.

This command creates the package and registers it with your Dev Hub Org.




Step 3: Create a Version of the Package

Once the package is created, the next step is to create a version. You can add tests and code coverage as part of the version creation.

sfdx force:package:version:create --package "Package2GP" --installation-key-bypass --wait 10 -v TestScratch --codecoverage

  • --package "Package2GP": Refers to the package name you created in Step 2.
  • --installation-key-bypass: Skips the installation key during version creation.
  • --wait 10: Waits 10 minutes for the version creation to complete.
  • -v TestScratch: The scratch org where you are testing the package version.
  • --codecoverage: Runs tests to ensure that the package has sufficient code coverage.

This command creates a new version of your package, and you’ll receive a version ID.




Step 4: Promote the Package Version

Before releasing the package, it must be promoted from beta to released. This step makes it publicly available for installation.

sfdx force:package:version:promote -p versionname/versionId -v Package2Gp

  • -p versionname/versionId: Replace this with the version name and ID you received in Step 3.
  • -v Package2Gp: Alias of your Dev Hub Org.

Promoting the version will move it from beta to the released state, which means it can now be installed in other orgs.




Step 5: Install the Package

Once the version is promoted, you can install the package into your Target Org. You will get a link for the package, or you can use the following command to install it via Salesforce CLI:

sfdx force:package:install --package versionname --targetusername RoundRobin --installation-key test1234 --wait 10 --publishwait 10

  • --package versionname: The version name of the package you wish to install.
  • --targetusername RoundRobin: Alias for your Target Org where the package will be installed.
  • --installation-key test1234: The installation key for the package.
  • --wait 10: Waits 10 minutes for the installation process to complete.
  • --publishwait 10: Waits 10 minutes for the package to be published.

After executing this command, the package will be installed into your Target Org.




Additional Tips:

  • Automating the Process:

You can automate the entire process with CI/CD tools like GitHub Actions or CircleCI, using Salesforce CLI to create, promote, and install packages automatically.


  • Testing:

Before promoting a version, ensure you test it in a scratch org to verify the components work as expected.


  • Handling Dependencies:

If your package depends on other packages, install them first, ensuring the proper order of installation.


  • Namespace Management:

The namespace you choose for your package cannot be changed after package creation. Be careful when selecting the namespace, as it affects the entire package.


  • Version Control:

Use semantic versioning to keep track of package versions and ensure compatibility when upgrading packages in target orgs.




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

Raptbot Technologies Private Limited的更多文章

社区洞察

其他会员也浏览了