Sub topic 1
What Second-Generation Packaging Is and Why It Exists
Prerequisites
This chapter assumes you know the following:
- Basic Salesforce platform concepts: orgs, metadata, and Apex.
- Salesforce DX basics: scratch orgs and source format.
- The command line and Git.
- JSON configuration files.
The problem 2GP solves
A package is a container of metadata that you can install in another org. Salesforce has two generations of packaging.
First-generation packaging (1GP) builds packages from a packaging org. This is a single org that holds your metadata. The org is the source of truth, so you can't easily track changes in version control or build from a clean state. Teams working in parallel often collide in that one org.
Second-generation packaging (2GP) moves the source of truth to your project. You keep metadata in a Salesforce DX project under Git. You build each package version from that source with the Salesforce CLI. A Dev Hub org tracks the package and its versions, but it does not hold your source.
Package types
Two terms describe what a package is and how it behaves:
- A managed package is tied to a namespace. Its code is hidden from subscribers, and it follows upgrade rules that protect those subscribers. Managed packages are the type you distribute on AppExchange.
- An unlocked package has no code hiding. Subscribers can change the contents. Teams use it to organize and deploy their own org's metadata.
Both types exist in 2GP. This tutorial covers managed packages.
| Row | 1GP managed | 2GP managed | 2GP unlocked |
|---|---|---|---|
| Source of truth | Packaging org | Version control | Version control |
| Built from | Packaging org UI | CLI and project files | CLI and project files |
| Namespace | Required | Required | Optional |
| Code hidden from subscribers | Yes | Yes | No |
How the pieces fit together
You will meet each piece in later chapters. Here is the overview:
- A Dev Hub org owns the package records.
- A namespace gives your components a unique prefix.
sfdx-project.jsondescribes the package and its version number.- The CLI creates package versions from your source.
- You test a version, then promote it to released.
- Subscribers install the released version.
A new version starts as a beta. You can install it only in sandboxes and scratch orgs. After you promote it, it becomes released, and you can install it in production.
Why it matters
Because the source lives in version control, you get code review, branching, and repeatable builds. You can create versions in a CI pipeline. You can also split a large app into several packages that depend on each other, and you can release each on its own schedule.
The trade-off is more setup. You need a Dev Hub, a namespace, and a project configuration before you build anything. The next chapters walk through this setup in order.
Exercise
A team keeps its metadata in Git and wants to build packages in a CI pipeline. Which packaging generation fits, and which org tracks the package versions?
Show answer
Second-generation packaging fits, because it builds versions from project source with the CLI. The Dev Hub org tracks the package and its versions.