SFDX, VSCode, and deploying from a package

SFDX, VSCode, and deploying from a package

 

While I do not immediately dislike new tools, I do struggle with adopting them when I find nothing wrong with the old ones. Then I delay learning them until I’m forced to, which is the case of Visual Studio code for Salesforce (they are no longer supporting the Eclipse IDE and removed the DX extension for Eclipse before DX went GA) and Git. I find the best way to learn new tools is to write about how to learn them, so here we go.

 

Let’s dive into SFDX, VSCode, and deploying from a package.

 

(In the spirit of working in a low code platform, we will also see how much of this I can do with just links to existing documentation…)

 

Be warned: Deploying will over-write any existing components with the same name.

 

If you haven’t already, Install Salesforce Extensions for VS Code.

 

Then Enable Dev Hub in Your Org and Enable Second-Generation Packaging. Note that while 2GP is beta as of this writing, this is required to enable first generation Unlocked Packages which is GA.

I couldn’t find a stand-alone URL for creating an SFDX project, so I’m going to borrow a section from a Trailhead lesson:

  1. Open VS Code.
  2. From the menu, select View | Command Palette. In the command palette search box, enter [PROJECT_NAME].
  3. Select SFDX: Create Project.
  4. Use the same name as your GitHub repo, then click Enter.
  5. Click Create Project.
  6. Create a .gitignore file to ignore hidden directories:
  1. Hover over the title bar for the DX project. then click the New File icon.
  2. Enter .gitignore. [check if it already exists and just edit if so]
  3. In the text editor, indicate to ignore these two hidden files:
.sfdx
.vscode

 

To foster good habits, I will set up a github repo to store this project in (though following a full lifecycle will be another article) by following the excellent documentation on github and add the project to the repository.

 

Now go do some work in Salesforce. For example purposes, let’s Build a Simple Flow.

 

After you complete the project, follow the instructions to Create and Upload an Unmanaged Package, skipping the Upload part. I named the project TH_Flow_Project, which you don’t have to. I only mention that as I will use that text in the example commands.

 

Salesforce provides a nice reference about how to Create a Salesforce DX Project from an Existing Source. I have some additional thoughts about how to go about this part, so I will end the approach of linking to references and switch to my own approach. If you followed the last link and stopped here, you won’t learn any more about the Salesforce DX capabilities, but you may miss out on some of my shortcuts.

 

Next, authorize the org you created the flow in with the following:

 

 

Example:

sfdx force:auth:web:login --setalias TH-ORG02 --instanceurl https://infa-ca-wav-dev-ed.my.salesforce.com/

 

A bit late to mention, but if you are using a Developer org, I highly recommend to Set Up Your Domain. Trailhead orgs already have one. If you haven’t, you can probably leave off the instanceurl parameter and it should pick it up from the default configuration for your project (YMMV). Otherwise use the URL that you login to your org with.

 

Next, download the package using the following:

 

 

Example:

sfdx force:mdapi:retrieve -r ../ -p TH_Flow_Project -u This email address is being protected from spambots. You need JavaScript enabled to view it. 

 

Let’s break that down just a bit. The first part is the base command to retrieve (sfdx force:mdapi:retrieve). The -r parameter determines where the downloaded zip file will be located. The example uses a relative path indicating the folder above the DX project. As a best practice, I recommend always staying in the project directory inside the VSC terminal, with all commands based on being relative to that location. This way you can maintain a list of commonly-used commands that will be re-usable across all projects. The downloaded file name is always unpackaged.zip.

 

The files need to be unzipped before they can be used (someone should make a feature request for the convert command to work on zip files instead of having to unpack them first). In Linux the relative command is:

unzip ../unpackaged.zip -d ../ 

 

Now we add the files from the package to our project using the relative path command:

 

 

Example:

sfdx force:mdapi:convert -r ../TH_Flow_Project -d force-app 

 

Now all files from your package are part of your project.

 

To add this to your target org, first authorize that org as done previously for the source org, i.e.:

 

 

Example:

sfdx force:auth:web:login --setalias TH-ORG02 --instanceurl https://infa-ca-wav-dev-ed.my.salesforce.com/ 

 

And (almost) finally, deploy the updates from your project to the target org with:

 

 

Example:

sfdx force:source:deploy -u This email address is being protected from spambots. You need JavaScript enabled to view it. -x 
../TH_Flow_Project/package.xml 

 

Another feature recommendation is to have an alias option instead of only the username.

 

Finally (this time for real!) look in your list of flows to see the flow installed in your target org.

 

One final note: We used the package.xml from the downloaded package for the sake of simplicity. Once the package import is validated, you will want to combine the package.xml from the download with the package.xml in your project located in the manifest folder of your project.

 

The project created from the writing of this article can be found at https://github.com/ssnsolutionist/trailhead1.

 

 

Scott Nelson is a Senior Technical Director at Primitive Logic. Scott is a seasoned IT leader and agent of change with over 20 years of experience working with the full delivery life cycle of solutions for system integration, process automation, and human workflow applications. He is also deeply experienced working with enterprises in need of guidance through complex changes, and has a long track record of managing digital transformation, expansion and stabilization initiatives.

 

Follow Scott on LinkedIn