June 11, 2024

Implementing Second-Generation Packaging (2GP) in Salesforce 

Welcome to our comprehensive guide on implementing Second-Generation Packaging (2GP) in Salesforce! In this blog post, we’ll walk you through the step-by-step process of leveraging 2GP, a powerful feature in Salesforce that revolutionizes how you package and distribute your applications. Whether you’re a seasoned developer looking to streamline your packaging workflow or a newcomer eager to dive into the world of Salesforce packaging, this guide is designed to equip you with the knowledge and tools you need to succeed. Let’s embark on this journey together as we unlock the potential of 2GP and elevate your Salesforce development experience to new heights.

Transitioning from 1GP to 2GP: A Step-by-Step Guide 

Currently, there’s no direct upgrade path from an existing First-Generation Package (1GP) to a Second-Generation Package (2GP) in Salesforce. However, Salesforce offers a pilot program for ISVs that allows participants to undertake this transition.If you are interested in this program, you can reach out to your Salesforce representative for more details. 

Steps to Migrate from 1GP to 2GP: 

    1. Create a New 2GP Package: Utilize the Dev Hub and Salesforce CLI to create a new package with a fresh namespace. This serves as the foundation for the upgraded package. 
    2. Install the New Package: Existing subscriber organizations with a 1GP package will need to install the new 2GP package. 
    3. Configuration: Configure the new package according to the requirements of the subscriber organization. 
    4. Data Conversion: Perform data conversion as necessary to ensure seamless migration of data from the old package to the new one. 
    5. Thorough Testing: Rigorously test the functionality of the new 2GP package to ensure it meets specifications and functions smoothly in the subscriber organization’s environment. 

By following these steps, users can successfully transition from a 1GP to a 2GP package, unlocking the benefits and capabilities of the latest packaging model in Salesforce. 

Technical Steps to Implement 2GP for Your Product 

Step 1: Create a Salesforce DX Project 

To begin developing a Salesforce app with the second-generation packaging (2GP) model, the first step is to create a Salesforce DX project using the Salesforce CLI. This involves setting up a repository in your source control system to manage the metadata for your app.

Step 2: Develop Your App 

Next, develop your package by creating a scratch org using Salesforce Extensions for VS Code. Add all necessary metadata components to your project directory. Ensure they are in the correct location.

Step 3: Configure and Create Your Package 

Specify your package’s namespace in the sfdx-project.json file. Review the project file to ensure it includes the settings and any dependencies your package requires. Create a scratch org definition file, or an external .json file, that specifies the features and org settings required for the metadata in your package. From your local Salesforce DX project directory, create the package using the force:package:create Salesforce CLI command. Only run this command once for each package you create. This CLI command is run against your Dev Hub org, and your Dev Hub org becomes the owner of the packages you create.

Step 4: Create a Package Version 

Subsequently, from your project directory, create a package version using the force:package:version:create Salesforce CLI command. When you create a package version, you take a snapshot of your package metadata to create an immutable, installable artifact. As you iterate on your package, you come back to this step and create new package versions.

Step 5: Install and Test the Package Version in a Scratch Org 

Create a new scratch org to test out your package. You need a new scratch org for this step. Don’t reuse the scratch org that you used to develop your package in step two. Install your package into the new clean scratch org using the force:package:install CLI command. Open the scratch org and view your app.

Step 6: Promote and Release the Package Version 

Finally, a package version is, by default, a beta version until you promote it. Beta versions can’t be installed in a production org. This safeguard ensures that the package version you release is production-ready. When a package version is ready for the world, you promote the package version to be released using the force:package:version:promote command. To promote a package version to released, the Apex code must meet a minimum 75% code coverage requirement, and every Apex Trigger in a package needs test coverage.

Illustration of the 2GP Package Development Lifecycle 

An illustration demonstrating the six key steps in the 2GP package development lifecycle. 

2GP Package Development Lifecycle
2GP Package Development Lifecycle

Automate Package Development Steps 

Additionally, write continuous integration (CI) scripts to automate these package development steps. For example, you could write a script that creates a package version, creates a scratch org, installs the package version in the scratch org, runs Apex tests, and emails the test results to the release manager.

Conclusion

In conclusion, implementing Second-Generation Packaging (2GP) in Salesforce offers significant improvements in development efficiency, version control, and future-proofing. By following the steps outlined in this guide and leveraging the automation tools available, you can streamline your packaging processes and stay ahead in the ever-evolving Salesforce ecosystem.

Explore Salesforce’s official documentation on 2GP for detailed technical insights here 

Share this post:
Facebook
Twitter
LinkedIn
WhatsApp

Discover more articles