Organize your Trailhead Superbadges with GitHub and Salesforce DX
Trailhead Superbadges are great to apply what you’ve learned in real business cases. However, there is no easy way to retake these Superbadges for now. If you wanted to, you would have to create a new Trailhead account and complete all Superbadges pre-requisites… which is not efficient.
I’ve worked on Salesforce for 3 years now and created around 100 Developer Editions / Trailhead Playgrounds / Sandboxes / Scratch Orgs. I passed 11 Trailhead Superbadges but I lost most of my work because I didn’t have a proper way to organize it. I lost some of my Developer Editions / Trailhead Playgrounds, I used others for too many assignments, etc… Even if I had 11 Trailhead Playgrounds for each Superbadge, I wouldn’t be able to understand each Superbadge step because some steps override previous ones.
As I prepared the “Development Lifecycle and Deployment Designer” Salesforce architecture certification, I learned about Version Control, Git and SFDX, and thought I’d try to apply what I’ve learned with a Superbadge I didn’t complete yet: the “Lightning Experience Reports & Dashboards Specialist”.
Table of Contents
The Big Picture
In this blog post, we’ll see how a Trailhead Playground, its Scratch Org, an SFDX repository (local), a GitHub repository and packages work together.
The Production Environment is the Trailhead Playground. We are not going to develop in this Org. The Development Environment is the Scratch Org (for declarative development) and the local repository (for programmatic development via Visual Studio Code).
We’re going to use SFDX, along with the Salesforce CLI and Visual Studio Code, to keep the 2 development environments (the local repository and the Scratch Org) in sync. We’ll often use 2 commands to do so:
sfdx force:source:push
and
sfdx force:source:pull
Next, use “Pull” when we want to pull changes made in the scratch org to the local repository, and we’ll use “Push” to do the opposite: meaning pushing changes from the local repository to the scratch org. We’ll test what we’ve built in the scratch org.
Once we’re happy with what we’ve built in the development environments, we’ll create a package with SFDX, so we can install it in the Trailhead Playground. We’ll pass challenges from the Trailhead Playground, and we’ll use GitHub to store versions of our work. For this Superbadge, as step 6 overrides previous steps, we must create 1 package with 2 versions, and 2 Git commits.
Pre-requisites
- Unlock the Lightning Experience Reports & Dashboards Specialist Superbadge
- Create a GitHub account
- Install Visual Studio Code and the Salesforce Extensions for VS Code
- Install Git to run Git commands from the Terminal
- Install the Salesforce CLI to run SFDX commands from the Terminal
- Check these useful resources
Prepare a Trailhead Playground
We’ll use this Trailhead Playground to pull changes made in its scratch orgs. We’ll use this Trailhead Playground to pass the Superbadge steps.
- Go to the Lightning Experience Reports & Dashboards Specialist Superbadge
- Create a new Trailhead Playground for this Superbadge
- Name it “superbadge-lex-rd” or anything catchy, from your settings page
- Enable Scratch Orgs in your Playground
- Open your Playground Org, go to setup > Dev Hub, and:
- Enable Dev Hub
- Enable Second Generation Packages
- As we’ll need the password for this Trailhead Playground later, we’ll store once here
- Go to setup > users and click your name and reset the password
- Open your Playground Org, go to setup > Dev Hub, and:
Create an SFDX project
We’ll use this local folder to pull changes we need from the scratch org. Once pulled, we’ll push the changes we need to our Version Control System, in our case GitHub, and to the Trailhead Playground.
- Open Visual Studio Code
- Toggle the Terminal by pressing Ctrl+ù
- Open a folder where you want to create your project locally, for example:
cd C:\Users\Nathan\Desktop\Documents\SFDX
- Create an SFDX Project:
sfdx force:project:create -n superbadge-lex-rd
- Then navigate to the folder:
cd superbadge-lex-rd
- Open the folder in Visual Studio Code
You should see this
Open the folder in Visual Studio Code
Link your SFDX project with your Playground
- From the terminal:
sfdx force:auth:web:login --setalias MyTPforSuperbadgeLexRD --setdefaultdevhubusername
- We added –setdefaultdevhubusername to link the Trailhead Playground with the SFDX project
- It’s going to open a web page, put your username and password you’ve stored earlier, and close the tab
You should see this. A new folder named “SFDX” has been created, with your Trailhead Playground alias.
Create a Scratch Org
- Type:
sfdx force:org:create -s -f config/project-scratch-def.json -a MyScratchOrgSuperbadgeLexRD
Notice that SFDX added your Scratch Org in the sfdx-config.json file.
If you’ve done everything correctly, you should see this when running this command in the terminal:
sfdx force:org:list
(D) and (U) indicate that SFDX is currently utilizing the Trailhead Playground and the Scratch Org we just created.
Now we can start developing in the scratch org (as this badge is mostly declarative development).
Create a GitHub repository
We now must create a GitHub repository to make sure that we track our development changes from the beginning. First, go to https://github.com and create a new repository. Here’s mine https://github.com/NathanPrats92/LEX_R-D.
Next, from Visual Studio Code, check if you have git installed properly:
git –version
Then, tell git who you are:
git config --global user.email "Your Email Here"
git config --global user.name "Nathan Prats"
Next, make sure that you’re in your SFDX local repo, and create a git folder by running git init. It’s going to create a hidden folder for Git. We’ll then link our local git repo with the remote repo:
git remote add origin https://GitHub.com/NathanPrats92/LEX_R-D.git
For the first push I named “Empty SFDX Project”:
git push -f origin master
Split Steps into Package Versions
Now, we must think about how many versions we need to create. I read all steps and saw that step 6, « Update existing reports » needs a new package version because if we update reports we’ve created in previous steps, we won’t be able to pass the first steps.
In the end, we’ll have 1 package with 2 versions: the first version will allow us to pass the steps 1 to 5, and the second version to pass all remaining steps.
Package Version 1: Challenge 1 to 5
First, we must install the unmanaged package here and load the data https://login.salesforce.com/?ec=302&startURL=%2Fpackaging%2FinstallPackage.apexp%3Fp0%3D04t1H0000018iyq
- Packages start with “04t”, so we’ll just extract this to push it via the Terminal to our Scratch Org:
sfdx force:package:install --package "04t1H0000018iyq"
- Open your scratch org to check what happened:
sfdx force:org:open -u MyScratchOrgSuperbadgeLexRD
As we can see, the package installed successfully.
We’ll retrieve the package metadata,
- Create a folder named RetrievePackage
- Create a zip to the RetrievePackage folder:
sfdx force:mdapi:retrieve --packagenames "SolarBot Acquisition Assets" --targetusername MyScratchOrgSuperbadgeLexRD --retrievetargetdir RetrievePackage
- Unzip the file
- Convert this source:
sfdx force:mdapi:convert --rootdir "RetrievePackage\SolarBot Acquisition Assets"
Now that we retrieve the package metadata, we can uninstall it from the scratch org, and then push from the SFDX repository back to the scratch org:
sfdx force:source:push -u MyScratchOrgSuperbadgeLexRD
Next, we need to import the SolarBot data. I couldn’t see the SolarBots app and associated tabs, so I updated the system administrator profile to have access to all these elements. I just clicked on Import once that was corrected. I’ll just do the same on the Trailhead Playground as it’s easier.
Once you’ve done the first 5 steps, run the following command to list the changes you’ve made:
sfdx force:source:status
If you’ve done everything correctly, you should see some reports, report folder, groups, one report type and one standard value set.
Then we can pull changes from the scratch org to the SFDX repository:
sfdx force:source:pull
Then we can push this commit to the Trailhead Playground, to validate challenges 1 to 5.
- Create a package:
sfdx force:package:create -n LexRD_Step1 -t Unlocked -r force-app
- Create a version so we can install it to the playground.
sfdx force:package:version:create -d force-app -w 20 -k Nathan
- Install the package to the Trailhead Playground.
sfdx force:package:install --package "04t1t000001sjvqAAA" -u MyTPforSuperbadgeLexRD -k Nathan
If it passes, push your changes to GitHub: commit, then push.
In your Trailhead Playground Org, you should see the installed package, with all components.
Package Version 2: Challenges 6 to 8
Similarly, we’re going to pass all challenges in the scratch org, then pull it to our SFDX local repository:
sfdx force:source:pull
Create a package:
sfdx force:package:version:create -d force-app -w 20 -k Nathan
Install it to our Trailhead Playground with the correct package ID:
sfdx force:package:install --package "04t1t000001sjvqAAA" -u MyTPforSuperbadgeLexRD -k Nathan
If it passes the challenges, push your changes to GitHub: commit, then push.
With that being done, even if I lost the Trailhead Playground for this Superbadge or modified it, I’ll be able to check what I’ve done any time with my GitHub account. In about 5 min, I’ll be able to open my GitHub repository’s first commit with Visual Studio Code, create a DevHub and a scratch org and push the repository to my scratch org. Then I could create a second scratch org and push the second commit repository. I also could install the packages I created because the IDs are located on the sfdx-project.json file on GitHub.
I would love to use this formula, but I have install everything but I am not a code person…Find a bit difficult. Can you help