Managing Multiple Environments with Salesforce DX

March 12, 2019 Thom Behrens

As a Salesforce consultant and developer, I almost always need to access multiple Salesforce environments: Each project might have a development sandbox, a quality assurance sandbox, and a user acceptance testing Sandbox; bigger projects might need System Integration Testing, and I also might be on more than one project at the same time! (A sandbox is a Salesforce non-production environment, which is tied to a Production Org and used for development and testing.)

This means I might have two sets of sandbox environments to manage. Plus, project-related environments don’t include all the non-project orgs I have: demo orgs I use to collaborate with colleagues and present to potential clients, sandboxes I spin up to try out new managed packages, and the multiple environments I use to keep track of personal projects.  

Password managers don’t always cut it 
Even if you use a password manager, the wrinkles might not all be ironed out; accidentally trying to login at the wrong URL or entering out-of-date password information are still problems that need to be solved. 

One of the many reasons Salesforce DX (read more here), specifically the Salesforce CLI (Command Line Interface), is useful is that it provides a suite of tools designed to help simplify the workstream for anyone who needs to juggle multiple Salesforce environments at once. 

Jumping headfirst into a new set of tools like Salesforce DX can feel intimidating, especially for anyone not technical or who’s strapped for time. Trying to understand new concepts and philosophies (like DevOps) while also trying to learn new technologies (like using the command line) might seem more time-consuming than they’re worth. Maybe you think the way you’re doing things already ‘works’ and the time to learn something new doesn’t seem like a great alternative. That can make it easier to ignore the inconvenience of our current workarounds. 

So to start to ease the learning curve, I want to introduce how to use Salesforce DX as an environment manager. This one small change is a manageable approach to integrating Salesforce DX into your workstream that solves both the problem of org management while simultaneously introducing you to the command line in an easy-to-understand way -- and it takes less than 10 minutes! 

Throughout the rest of this post, you’ll be introduced to three command line commands, see how to spend less time shuffling with org logins (and more time doing actual work), and learn the difference between an "environment manager" and a "password manager."
I explain the last one before getting to the steps.

Password Managers (like LastPass, 1Password, and KeyPass) all serve a great purpose: They take the convention used across the web of username and password pairs, and store them for you. When you want to log onto any site across the web, no matter what it is, they’ll supply you with the username and password pair. This means you must remember only one password: the password to your Password Manager.

Using Salesforce DX has three important distinctions from Password Managers: 

  • It’s not for all websites, just your Salesforce environments.
  • It doesn’t store your passwords at all; instead, it stores what is called an ‘Access Token’ for every org. While we use usernames and passwords for logging on through the User Interface, we use Access Tokens for logging in through the Application Interface - the API. Once you set up Salesforce DX per the instructions below, you’ll bypass the User Interface login page and won’t need to store a password.
  • There’s no authentication required to access the credentials after you’ve already logged into your laptop. (Make sure you keep your credentials protected. For ideas on how to get smart about security, check out the tip at the end of this article.).


How to use Salesforce DX as an environment manager

1) Download and install Salesforce CLI. 

Your computer has lots of built-in command line tools, but since the three we are using are specific to Salesforce DX, we’ll have to start by downloading the Salesforce Command Line Interface (CLI). Follow these instructions on how to download the Salesforce CLI on Mac, Windows, or Linux, and come back to this blog post when installation is successfully completed. 

2) Open up the Terminal on Mac/Linux or the Powershell app in Windows.

On a Mac, press cmd+space to open the Spotlight app, type ‘Terminal’, and press Enter when you see the Terminal app suggested. Another way to do this: open Finder, go to the Applications folder and click on ‘Terminal’. 

On Windows, press the Windows key (or open the Start Menu) and type ‘Powershell’ and select ‘Windows Powershell’ when it displays.

If you’re running Linux, open up the command line as you regularly would.

3) Add a new org to the environment manager.

You need to install the Salesforce CLI only once, but you’ll repeat this step for every org you add. This step has two parts: First you enter the command to add the org, and then you enter your username/password.

In this scenario, I’ll add to my environment manager the credentials used to access the development environment for ‘No More Homelessness,’ a fictional nonprofit used in the Fundraising with NPSP trail. If you haven’t checked it out yet, head there next

I begin by naming this set of credentials ‘nmhDev’, because it tells me everything I need to know about the credentials:

  • nmh refers to the name of the client.
  • Dev tells the purpose of the environment. 

Remember, the command line is all about keeping things short and sweet.

Here’s the command to add the org:

sfdx force:auth:web:login -a nmhDev

  • sfdx tells your operating system you’re running Salesforce DX commands.
  • force:auth:web:login tells Salesforce DX you’re authorizing an org by logging in through the web.
  • -a nmhDev tells the login command what you’re naming this set of credentials, ‘nmhDev’.

Pay attention to your surroundings: For this demonstration, I’m using credentials which log into https://login.salesforce.com. Salesforce DX assumes this is our login location, but for sandboxes or orgs with restricted MyDomain login, make sure to add the -r flag along with the location you’re logging into. (E.g., if I were logging into https://test.salesforce.com, my command would be sfdx force:auth:web:login -a nmhDev -r https://test.salesforce.com.)

Expert Tip: If it’s easier for you to remember, use --setalias nmhDev instead of -a nmhDev, and --instanceurl https://test.salesforce.com instead of -r https://test.salesforce.com. These pieces of information being passed to the command line are called ‘flags.’ Most commands have a short version of a flag (symbolized by a single dash and a letter), and a long version of the flag (symbolized by two dashes and one or more words). One is shorter, one is easier to remember, but the choice is yours!

After typing in this command, you’re taken to the Salesforce login page you’re familiar with. This is where the knowledge transfer happens: Enter your username and password from your password manager, browser extension, or wherever you keep your login data. Then, you’ll be asked to authenticate the Salesforce DX Global Connected App. Click Allow, and you’ll be logged in!

Once you’ve successfully logged in, you can close the browser window. Salesforce DX has everything it needs! Next, head back to Terminal/Powershell to put your newly created stored credentials to use.

4) View your stored environments.

Now that you’ve stored your login information in Salesforce DX, make sure you can see it. This command doesn’t have any option flags in this scenario, just the simple command: 

sfdx force:org:list

You should see a table with four columns (right now, only one row might appear, but you can imagine how it will fill up once you transfer all your environments):

  • Alias will be the nicknames we set above (‘nmhDev’, in this scenario). 
  • The next column is the actual Username used for login. 
  • After that, comes the Org Id. (This corresponds to the org ID found under ‘Company Information’ in Setup. You can use it whenever you contact Salesforce Support.)
  • Lastly, there’s Connected Status. If you’ve followed the previously outlined steps correctly, hopefully, you’ll see the words “Connected.” If you ever have problems logging in from the command line in the future, this is the first place to look; you may need to re-authenticate.

5) Log into a stored org.

Now that you’re all set up, you can really see the power of the environment manager in action. Pretend you’ve stepped away for lunch (maybe you have!), or maybe you’re just logging in to start a new day of work. What do you usually set up for work? Maybe you open your email, find a playlist to listen to, check your notes from yesterday, and see how long you have until your first meeting. 

What if you’re in that first meeting and Salesforce isn’t pulled up yet, but a client asks a question about a piece of configuration? You’ll need to get into the new org fast! With your new Environment Manager, it’s easy: Just open search (cmd + space on a Mac; Windows key on Windows), type in the name of your command line app (Terminal or Powershell; on my Mac I only need to type a ‘T’), and with this one simple command, you’re logged in!

sfdx force:org:open -u nmhDev

Expert Tip: Notice that I used the -u flag here to mention the name we created, instead of the -a flag. You can also use --targetusername long version flag if it’s easier for you.

Your default browser automatically opens, logs you in, and you’re off to the races! How long did that take? Five seconds? In the time it takes you to say “Let me pull that up,” you’re already looking at it and answering questions! 

Salesforce DX has a host of tools, all of which work together to simplify the development and deployment process. I highly recommend getting started with Salesforce DX Trailhead Trail if you’re interested in further streamlining your development process and deployment process. 

I hope this small corner of Salesforce DX will be enough of a convenience to encourage you to dive in more, and if not, I hope it’s still a huge time-saver! 

Bonus tips

Tip #1: Color-code your Salesforce instances for easy identification. If you need to manage multiple orgs, a super helpful tool is Salesforce Colored Favicons, a Chrome extension, which allows you to color-code each instance you log into (so it obviously works best on orgs with My Domain enabled). It also means no more looking at endless tabs of little blue clouds! I chose Burgundy for the No More Homelessness Dev org, so if I have multiple orgs (and documentation pages) open, I’ll always know where to click. 

Tip #2: Be sure to keep your credentials safe. As consultants, other people trust us to keep their data - and their clients’ data - safe. Talk to your company’s security officer about the best ways to keep your login credentials protected, and familiarize yourself with the Salesforce Elements of User Authentication. Set screensaver logins and timeout limits on your machine to prevent unauthorized users from accessing your computer when you step away. Additionally, continue to use your password manager to store unique and secure passwords for every website you visit. Compromising one login shouldn’t mean compromising all logins.

Happy Salesforce-ing! Don't forget to check out the Appirio Salesforce Hub for the latest news, trends, and stories. We always love to hear what our readers think, message us on social media to share your thoughts on Salesforce DX, and more! 

About the Author

Thom Behrens

Thom is a Technical Lead at Appirio. He is a relationship-driven software engineer who loves bringing data literacy to nonprofits.

Follow on Linkedin More Content by Thom Behrens
Previous Article
Academic Transformation: Preparing Students for a Digital World
Academic Transformation: Preparing Students for a Digital World

Next Article
How to Best Support Salesforce: In-House or Partner?
How to Best Support Salesforce: In-House or Partner?