Sub topic 1
What Second-Generation Packaging Is and Why It Exists
Prerequisites
This chapter assumes you understand Salesforce orgs, metadata, Apex, scratch orgs, Salesforce DX source format, Git, and basic JSON. You should also be comfortable running commands in a terminal.
What 2GP Adds
Second-generation packaging (2GP) builds installable Salesforce packages from source-format files. The Dev Hub stores the package definition and creates immutable package versions. This makes Git the source of truth and makes package creation easier to automate.
2GP differs from first-generation managed packaging (1GP) because versions are built from a Salesforce DX project instead of from metadata assembled inside a packaging org. It also differs from unlocked packages. Unlocked packages are useful for internal modularization and usually do not provide the same subscriber protections as managed packages.
A managed package is intended for distributing an application to other organizations. It uses a namespace, protects packaged components from subscriber edits, and applies rules that preserve upgrade compatibility.
What Makes a Package Managed
A namespace is a unique prefix added to components such as acme__Invoice__c. Register the namespace and link it to the Dev Hub before creating the package. The namespace separates your components from components created by other developers.
Managed packages provide intellectual property protection. Subscribers can use the exposed behavior, but they generally cannot modify or delete packaged components. Salesforce also checks package upgrade rules. For example, removing or changing a component in a way that breaks subscribers can prevent an upgrade.
Preparing the Dev Hub and Namespace
Enable Dev Hub in the Salesforce organization that will own your packages. Authenticate that org and make it the default Dev Hub:
sf org login web --set-default-dev-hub --alias devhubRegister a namespace through Salesforce Setup, then link that namespace to the Dev Hub in the Dev Hub settings. Use the same namespace in the project configuration.
Configuring `sfdx-project.json`
The project file identifies the package directory, package name, version format, and namespace:
{
"packageDirectories": [
{
"path": "force-app",
"default": true,
"package": "AcmeApp",
"versionName": "Initial release",
"versionNumber": "1.0.0.NEXT",
"ancestorVersion": "NONE"
}
],
"namespace": "acme",
"sfdcLoginUrl": "https://login.salesforce.com",
"sourceApiVersion": "61.0"
}NEXT tells Salesforce to assign the next build number. The package directory must contain source that is valid for the managed package.
Creating the Managed Package
Create the package record in the Dev Hub:
sf package create --name AcmeApp --package-type Managed --path force-app --target-dev-hub devhubThe command adds the package identity to the project. Later chapters cover creating versions, testing them, and promoting a validated version for installation.
Exercise
Why does a managed 2GP project need both a Dev Hub and a namespace?
Show answer
The Dev Hub owns package definitions and versions, while the namespace uniquely identifies and protects the package's components in subscriber orgs.