Last week on CodeLive special guest John Daniel and I took an existing codebase of Apex code and converted it to a DX unlocked package! Unable to join us? That’s ok the recording is available here. If you’re more of a reader than a viewer don’t fear — this post covers the highlights!
Our general ‘use-case’ was to take an existing bit of code (in this case Apex Promises) and convert the old metadata style repository to a source tracking style. We also configured the repository to build an unlocked package. This takes a handful of easy steps, most of which we demonstrated live! Let’s dive into exactly how this is done.
Kevin’s top tip: You can always append a
-hflag to anysfdxcommand to see what command line params it accepts!
The steps
- Create a new project, either with the VS Code extensions or the CLI with
sfdx force:project:create --projectname MyAwesomeProject - Import your code. On the live stream we created new classes, and copy/pasted our code it. That makes sense, when you’re talking about a grand total of three classes. If your codebase is larger, though, you can use the handy Metadata api conversion tools built into
sfdx. Usesfdx force:mdapi:convert -hfor more information on how to use it! - At this point in our episode we made an entirely optional change to our codebase to separate our test classes from our regular classes. The key to remember is this: any and all folders underneath the folder you specify in the
sfdx-project.jsonfile’spackageDirectories/pathkey will be included in your package. - Next we needed to create a package. This command is only available via the command line. For our use case the command was:
sfdx force:package:create -n ApexPromises -t Unlocked -d "Use the Promise pattern in Apex" -r force-app -eThose command line options like-nallow us to pass in our package name, etc. Use-hto see the full list of options available. - Next we created a package version using
sfdx force:package:version:create -p ApexPromises -c -w 30 -xNote the-c -w 30and-xflags. They calculate the code coverage(-c), wait up to 30 minutes (-w) and bypass the requirement of a package password (-x) - At this point, we have a package we can install using
sfdx force:package:install -p <packageID>. However, this package is only available in staging environments. - To make our package available to production orgs we need to take one last step – promoting. To do this we used
sfdx force:package:version:promote -p <packageID>
Want to learn more about Unlocked Packages? Checkout this Trailhead Quick Start: Unlocked Packages, or watch the CodeLive episode on demand here!
And of course, you’re welcome to install the Apex Promises package using this command: sfdx force:package:install -p 04tB0000000QEHpIAO and start using that wherever you’d like!