[email protected]
Search…
Create your own package

  • Understand what an Unlocked Package is
  • How to validate if your build can be packaged
  • How to create an Unlocked package and version it
  • How to split large packages into modules
Time to complete: 45 Minutes

  • Open your trailhead playground 1
  • Go to Setup
  • Search for and click 'DevHub' in the Quick Find menu
  • Select 'Enable Unlocked Packages and Second-Generation Managed Packages'

Use the sfdx force
πŸ“¦
create command to create an unlocked package
sfdx force:package:create --name dreamhouse-app --packagetype Unlocked --nonamespace --path dreamhouse-app --targetdevhubusername <playground-1>
The package details will automatically be added in the sfdx-project.json and will look like this:
{
"path": "dreamhouse-app",
"default": true,
"dependencies": [
{
"package": "dxatscale-base"
},
{
"package": "LWCC"
},
{
"package": "flowmagic"
}
],
"package": "dreamhouse-app",
"versionName": "ver 0.1",
"versionNumber": "0.1.0.NEXT"
}

Use the sfdx force
πŸ“¦
version:create command to create a version of the unlocked package
sfdx force:package:version:create --wait 30 --installationkeybypass --codecoverage --package dreamhouse-app --definitionfile config\project-scratch-def.json --targetdevhubusername <playground-1>
Did this fail? check the metadata coverage report to understand which metadata is supported in unlocked packages. We will rectify this next.
Validate your package
utilise the sfdx sfpowerkit:package:valid command before packaging
sfdx sfpowerkit:package:valid --package dreamhouse-app

  • Create a folder named 'src-env-specific' in your project root
  • Move sharing rules and settings folder to src-env-specific
  • Add the src-env-specific path to the sfdx-project.json file
"packageDirectories": [
{
"path": "dreamhouse-app",
"default": true,
"dependencies": [
{
"package": "dxatscale-base"
},
{
"package": "LWCC"
},
{
"package": "flowmagic"
}
],
"package": "dreamhouse-app",
"versionName": "ver 0.1",
"versionNumber": "0.1.0.NEXT"
},
{
"path": "src-env-specific",
"package":"env-specific",
"versionName":"ver 0.1",
"versionNumber":"0.1.0.0"
}
]
Now retry the sfdx force
πŸ“¦
version:create command
sfdx force:package:version:create --wait 30 --installationkeybypass --codecoverage --package dreamhouse-app --definitionfile config\project-scratch-def.json --targetdevhubusername <playground>

Now we are going to create a fresh scratch org, install our dependencies and then install our new package
sfdx force:package:install -p <04t version id from your sfdx-project.json> -w 30 --apexcompile package --securitytype AdminsOnly --noprompt -u <new-so-alias>
If you are getting an error during installation of package, remove the <networkMemberGroups> from the Network file of your Community.
If you are getting the message below, error during installation delete the scratch org and recreate another as this is a known salesforce issue.
You can now safely delete this scratch org using the sfdx force:org:delete command.

  • Create a new folder in your project root using this structure: dreamhouse-data-model\main\default\objects
  • Now move the 'Broker__c' and 'Property__c' objects into the newly created folder_
Now create your new package using sfdx force
πŸ“¦
create
sfdx force:package:create --name dreamhouse-data-model --packagetype Unlocked --nonamespace --path dreamhouse-data-model --targetdevhubusername <playground>

"packageDirectories": [
{
"path": "dreamhouse-data-model",
"package": "dreamhouse-data-model",
"versionName": "ver 0.1",
"versionNumber": "0.1.0.NEXT",
"default": false
},
{
"path": "dreamhouse-app",
"default": true,
"package": "dreamhouse-app",
"versionName": "ver 0.1",
"versionNumber": "0.1.0.NEXT",
"dependencies": [
{
"package": "dxatscale-base"
},
{
"package": "LWCC"
},
{
"package": "flowmagic"
},
{
"package": "dreamhouse-data-model",
"versionNumber": "0.1.0.LATEST"
}
]
},
{
"path": "src-env-specific",
"package":"env-specific",
"versionName":"ver 0.1",
"versionNumber":"0.1.0.0"
}
]
Now, create the package version for dreamhouse-app, as we did earlier in this module.
Now create a fresh scratch org, test installing your packages and commit your changes to the repo.

Well done! We have learned how to create unlocked packages, move any unsupported metadata into metadata package (later how we see we can deploy it) and split a package into smaller packages.
Copy link
On this page
Learning Objectives
Steps
Enable Unlocked Packages in DevHub
Create an Unlocked package
Create a package version
Move unsupported metadata to an unpackaged folder
Install the package
Split the dreamhouse-app package into 2 modules
Create versions for the new two unlocked packages
Recap