SF Unlocked Package

Tzhe’ela Trooper
3 min readApr 5, 2022

IMPORTANT : Metadata in unlocked packages can be modified in a production org.

From Setup in your Trailhead Playground, enter Dev Hub in the Quick Find box and select Dev Hub. Click Enable Dev Hub and then click Enable Unlocked Packages and Second-Generation Managed Packages.

There are two parts to a package: the first is the package and the second is the package version.

Step 1: Create the unlocked package

sfdx force:package:create --name PackageName --description "Unlocked Package" --path force-app --packagetype Unlocked --targetdevhubusername DevHubAlias

Note: If you are using windows and getting the error

'C:\Program' is not recognized as an internal or external command,
operable program or batch file.

It is due to the spaces in the description parameter, remove the spaces, and you should be able to run the command.

Step 2: Create a version

Update the information under the packageDirectories node in the JSON structure.

"versionName": "Winter 22",
"versionNumber": "0.1.0.NEXT"

The versionNumber auto-increments based on the use of the NEXT keyword.

Run the command :

sfdx force:package:version:create --package PackageName --path force-app --installationkey test1234 --wait 10 --targetdevhubusername DevHubAlias

This operation can take several minutes.

If you get the error

The 'xxx' component instance doesn't have an identifier specified.

You can do one of two things:

  1. Retrieve the component from the org sfdx force:source:retrieve
  2. Lower the API version on sfdx-project.json to 52 and lower

Step 3 : Install the package on other scratch Orgs

Spin up a new scratch org and test the installation

sfdx force:package:install --wait 10 --publishwait 10 --package 04****AAA --installationkey test1234 --noprompt

OR, you can also use the alias

sfdx force:package:install --wait 10 --publishwait 10 --package PackageName@0.2.0-1 --installationkey test1234 --noprompt

You’ll find the alias on the sfdx-project.json file.

Step 4: Increment the version after a change and install the new version

Make your changes and save them (pull them into the repo) + try to sfdx force:source:push them into a new scratch org to verify that everything needed is included in the repo.

If you are ready to create a new version, open the file sfdx-project.json and update the value "versionNumber":"0.1.0.NEXT" to a higher version base on the scope of the change you made.

Then run the below

sfdx force:package:version:create -p PackageName -d force-app -k test1234 --wait 10

For the official documentations for creating a version -

NOTE: The package identifier is not held against the scratch orgs or the DeveloperHub org used. You can test that by uninstalling the package, and deleting it from the DevHub org, then deleting all scratch orgs created in the process, and you will still be able to deploy the package using the CLI

When you are working with a client that needs both a roll back strategy and has a SoC compliance in place, unlocked packages can be a solution for a deployment strategy

When you create a new version, the terminal will provide you with the installation URL as a reminder, for ex:

Package Installation URL: https://login.salesforce.com/packaging/installPackage.apexp?p0=04********AAA

Replace the login.salesforce.com with your target org address (use the classic back door, to avoid re-authentication.

https://companyOrgName.my.salesforce.com/packaging/installPackage.apexp?p0=04********AAA

This will prompt the user to enter the installation key (which is test1234 in this article).

Other considerations can be found in the trailhead

--

--