Skip to content

Instantly share code, notes, and snippets.

@rgvaldovino
Last active August 3, 2023 14:52
Show Gist options
  • Save rgvaldovino/0ca6b1981538653e930d74a3a2ee28a6 to your computer and use it in GitHub Desktop.
Save rgvaldovino/0ca6b1981538653e930d74a3a2ee28a6 to your computer and use it in GitHub Desktop.
Steps to create a sfdx unlocked package from existing project

Assumptions

  1. Your project does not have any dependency on external metadata (unless that dependency is another package)
  2. All your unit tests are passing
  3. Enabled Packaging 2 feature is enabled in your Dev Hub org (Production Org)
  4. You have permissions to create scratch orgs

Steps

Clone project repo

# for example: SALESFORCE-UTILS
$ git clone https://github.com/UCSF/SALESFORCE-UTILS.git

Create sfdx project

# run this command outside cloned project folder
# make sure the project name is the same as cloned repo name to avoid creating a new folder
$ sfdx force:project:create -n SALESFORCE-UTILS

Convert project to sfdx format

# change working directory to cloned repo
$ cd SALESFORCE-UTILS

# run command to convert project
$ sfdx force:mdapi:convert -r src/

Create scratch org

# 
$ sfdx force:org:create -f config/project-scratch-def.json --durationdays 30 -a UTILS-DEV

Create package

$ sfdx force:package2:create --name UCSF-SALESFORCE-UTILS --description "Utility code for Salesforce projects" --containeroptions Unlocked --nonamespace --targetdevhubusername DevHub
Successfully created a second-generation package (package2). 0HoA00000008OI5KAM 033A0000000Eu6JIAS
=== Ids
NAME                   VALUE
─────────────────────  ──────────────────
Package2 Id            0HoA00000008OI5KAM
Subscriber Package Id  033A0000000Eu6JIAS

Create package version and install in scratch org

Follow the steps documented in following link under "Create the Package Version and Install It in Your Scratch Org" section: Reference

  1. Update sfdx-project.json as explained in prevoius link
{
  "packageDirectories": [
    {
      "path": "force-app",
      "default": true,
      "id": "0HoA00000008OI5KAM",
      "versionName": "Version 1.0",
      "versionNumber": "1.0.0.NEXT"
    }
  ],
  "namespace": "",
  "sfdcLoginUrl": "https://login.salesforce.com",
  "sourceApiVersion": "42.0"
}
  1. Create package version
# create package version
$ sfdx force:package2:version:create --installationkey "8kiGTyMM6XGeovTRm4J%4pAE" --directory force-app --wait 10

# after few minutes see result of packge version creation
Successfully created the package2 version [08cA00000008OI5IAM]. Package2 Version Id: 05iA00000008OI5IAM. Subscriber Package2 Version Id: 04tA0000000FfNEIA0.
Package Installation URL: https://login.salesforce.com/packaging/installPackage.apexp?p0=04tA0000000FfNEIA0
As an alternative, you can use the "sfdx force:package:install" command.
  1. Install the package in the scratch org
# use the Subscriber Package2 Version Id: 04tA0000000FfNEIA0 (created from previous command)
$ sfdx force:package:install -i 04tA0000000FfNEIA0 -u UTILS-DEV --wait 10 --publishwait 10
Waiting for the package install request to complete. Status = IN_PROGRESS
Waiting for the package install request to complete. Status = IN_PROGRESS
Waiting for the package install request to complete. Status = IN_PROGRESS
Successfully installed package [04tA0000000FfNEIA0]
  1. Open the scratch org and view the package
$ sfdx force:org:open -u UTILS-DEV

Wait for your default browser to open the scartch org and then navigate to Setup -> Installed Packages

Release package version

Following command will make this package version available for production orgs

$ sfdx force:package2:version:update -i 05iA00000008OI5IAM --setasreleased
Are you sure you want to release package version 05iA00000008OI5IAM? You can't undo this action. Release package (y/n)?: y
Successfully updated the package version. ID: 05iA00000008OI5IAM.

Now try to install in a developer org or a Trailhead Playground

# install package version
$ sfdx force:package:install -i 04tA0000000FfNEIA0 -u MyTPO --wait 10 --publishwait 10
Waiting for the package install request to complete. Status = IN_PROGRESS
Waiting for the package install request to complete. Status = IN_PROGRESS
Waiting for the package install request to complete. Status = IN_PROGRESS
Waiting for the package install request to complete. Status = IN_PROGRESS
Waiting for the package install request to complete. Status = IN_PROGRESS
Waiting for the package install request to complete. Status = IN_PROGRESS
Successfully installed package [04tA0000000FfNEIA0]

# open the org and avlidate that package version is installed
$ sfdx force:org:open -u MyTPO
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment