Demystifying Second-Generation Managed Packages (2GP) in Salesforce
Introduction: What is a Managed Package?
In the world of Salesforce development, managed packages are essential tools for distributing and selling your applications. With the introduction of Second-Generation Managed Packages (2GP), Salesforce has provided developers with a more flexible and efficient way to build, package, and distribute their apps.
Understanding Second-Generation Managed Packages (2GP)
Second-Generation Managed Packages (2GP) offer an improved packaging experience compared to First-Generation Managed Packages. They provide better version control, modular packaging, and more granular dependency management. 2GP separates the source code from packaging metadata, enabling developers to version and manage their source code independently.
Benefits of 2GP
- Version Control: With 2GP, you can leverage version control systems like Git to manage your source code. This makes it easier to collaborate with other developers, track changes, and maintain a clean history.
- Modular Packaging: 2GP allows you to create modular packages by organizing your components into namespaces. This enables you to distribute features as separate packages, providing greater flexibility to your customers.
- Dependency Management: You can manage package dependencies more effectively with 2GP. Specify dependencies between packages and ensure that the required components are included when your package is installed.
- Unlocked Packages: 2GP uses an unlocked package format, which provides better flexibility for customers to customize their components while still receiving updates from their package.
Here is the quick difference between 1GP and 2GP:
Pre-requisites for creating 2GP Packages:
- Create Dev Hub
- Enable Unlocked and Second-Generation Managed Packaging
- Install Salesforce CLI
- Create and Register Your Namespace. (Make sure that you do this step in the ORG other than the DEV hub).
After creating the namespace in Non-Devhub Org, Register and link this Namespace in DevHub
Creating a Second-Generation Managed Package (2GP) - Step-by-Step Demo:
For this demo, let's create a simple Second-Generation Managed Package that includes a custom object, a Lightning web component, and a few custom fields.
- Set Up Your Environment: Make sure you have Salesforce CLI installed and authenticated. - Set up your project directory and initialize a new Salesforce DX project.
- Create Package Directory: - Create a directory for your package, e.g., "SGMPDemo". You can do it using the command palette ( Ctrl + Shift + P) or you can use the below command on the terminal
sfdx force:project:create -n <projectname> --template standard
- Authorize your Dev Hub: You can do this with the command palette or with the below command.
sfdx force:auth:web:login --setdefaultdevhubusername --setalias <youralias>
- Create Scratch Org (Dev Edition can also work): Check sure every component of the package is present in the project directory where you wish to create it.
sfdx force:org:create -f config/project-scratch-def.json --targetusername <yourorgname> --durationdays 15 --setdefaultusername
- Develop the app in Scratch Org:
To open Scratch Org, run the below command.
sfdx force:org:open
You can use the push and pull command to push and pull the changes between the project and the org.
sfdx force:source:pull
sfdx force:source:push
- In the sfdx-project.json file, fill in the details of the namespace in the namespace attribute. For example: “namespace”: “SGMP-Demo”.
- Create Package and Package Version: - Create a package and a version of your package using the below command. - Specify the version name, package name, and other relevant details.
sfdx force:package:create --name <yourPackageName> --packagetype Managed --path force-app
After the package is created, review the sfdx-project.json file
Now create a version of your package.
sfdx force:package:version:create --package <yourPackageName> --installationkey test123 --codecoverage --wait 10
Install the package version with the URL provided with the success message. Refer to the above screenshot and test it in scratch org.
- Use scratch org other than the one created in the above step .
- Use the Package installation URL and paste it into any browser.
- Open the scratch org in which you want to Install the package.
- Enter the Installation Key(test123).
- Click on Install for Admins only (This depends on what you want to choose).
- Click Install.
- You can update the code and create multiple versions of your package.
- Promote to Release: - Promote your package version to a released state using "force:package:version:promote".
sfdx force:package:version:promote --package <yourPackageAlias>
- Install and Test: - Install the package in a sandbox org or developer org. - Validate that the components are successfully installed and functional.
Best Practices for Second-Generation Packages
When working with second-generation packages, we advise that you stick to these best practices.
- Modular Packages: Organize components into smaller, focused packages for easier installation and updates.
- Version Control: Follow semantic versioning and communicate changes clearly in release notes.
- Dependency Management: Define dependencies accurately and use version ranges for flexibility.
- Testing and QA: Rigorously test packages in scratch orgs to ensure reliability and compatibility.
- Documentation: Provide thorough installation and usage instructions to assist users effectively.
Limitations:
Below are the limitations of 2G Managed Package:
- You cannot use push upgrades for 2GP.
- There's a small set of metadata types that are supported in 1GP but not in 2GP.
Conclusion
Second-Generation Managed Packages (2GP) bring a modern packaging approach to Salesforce development, offering improved version control, modular packaging, and better dependency management. By following the step-by-step demo outlined in this blog post, you can easily create and distribute your own 2GP. Embrace the power of 2GP to enhance your app distribution and management experience within the Salesforce ecosystem.
Blueflame Labs, salesforce implementation partners can help you with the 2GP Managed Package. Get in touch to discuss it with our Salesforce experts.
Recent Blogs
Salesforce Data Migration Tools and Their Usage
Read MoreSignal Chain Categories to Improve Customer Business using Rootstock
Read MoreMaster .NET Aspire: Level Up Your .NET Skills
Read MoreSafeguarding Salesforce - Uncovering and Addressing Security Vulnerabilities
Read MoreMastering Amortization Flow in NetSuite
Read MoreNetSuite Advanced Revenue Management (Essential)
Read MoreMastering Shipment Tracking with Zenkraft (Bringg)
Read MoreIntegrating Form Titan with Salesforce
Read More