Salesforce’s second-generation packaging (2GP) is a new way to develop, distribute, and manage apps and metadata on the AppExchange. The 2GP is considerably different from the first generation packaging (1GP). In this blog, the primary focus is on the role of namespace and the advantages of 2GP.
Package –
A package is a container that bundles metadata such as classes, lightning components, visual force pages, custom objects’s/fields, apps, etc. An AppExchange partner typically combines various metadata elements as a package and distribute it on the AppExchange.
Namespace:
A namespace is a unique alphanumeric identifier that distinguishes one package from another in a Salesforce org. Salesforce automatically prepends namespace prefix to all the contents within the package.
In the First generation package, a namespace is tied to a package. Each Salesforce org can contain only one namespace. So as a developer, if you want to distribute two different packages onto the AppExchange, you need two Salesforce org’s; one for each of the namespaces.
With 2GP, a namespace can be shared with multiple packages. Now, the development for multiple packages can happen in the same dev hub and the code can be shipped with any of the packages.
Source Driven System:
The first generation packages rely on the packaging org as the source of truth. However, in the 2GP the dev hub owns the package; thus the source of truth is a version control tool such as Github. So you can develop the code on any org under a dev hub by registering the corresponding namespace. Packaging and publish to the AppExchange can be managed using Salesforce CLI (SFDX) from version control, without relying on the development org.
Besides the namespace, the following are a few advantages of using the 2GP.
- One namespace can be applied to multiple packages.
- We can share the code by using @namespaceAccessible annotation for public Apex classes and methods across Multiple packages sharing the same namespace.
- All packaging operations can be automated using Salesforce CLI.
- Second-Generation Package versioning supports branches. Whereas the first-generation package is linear.
References –
https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_dev2gp_before.htm
https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_dev2gp.htm