How To Connect VS Code to a Salesforce Org (For Beginners)

Alex Barnes
4 min readMay 14, 2019

--

I recently started a new role at a small startup doing Salesforce Development for a SF consultancy. For the first time in my career as a developer my IDE wasn’t mandated by what the rest of my team was using and I was free to use whatever tools I saw fit. After reading through several forums and posts, VSCode struck me as the most appealing IDE since it comes with a helpful suite of Salesforce add-ons and integrations including SFDX.

As I looked around for instructions on how to set up an Org in VSCode, I found several guides that didn’t seem to address what I needed exactly. They’d have you make packages in your SF org containing all the relevant data you wanted to pull down, then connect using SFDX, then deleting the packages. My first few attempts I ended up missing data in my local IDE. These steps seemed convoluted for what I was trying to accomplish.

After some trial and error, I’ve boiled it down to 7 easy steps to quickly hook up to your SF org to VSCode so you can get to developing fast! To accomplish this you’ll need to have installed VSCode along with the full suite of Salesforce Extensions (currently called ‘Salesforce Extension Pack’).

Step 1

Select ‘Create New Project with Manifest’ via (cmd + shift + p). I name the projects according to the sandbox name, but it can be called whatever you prefer. This command can be called from any directory, so long as you select the correct destination to save the project.

Step 2

For every use case so far, I select the default “Standard Project Template”

Step 3

Select ‘Authorize an Org’ (cmd + shift + p) from the same menu as the previous step.

For URLs like “https://ab12.salesforce.com/”, I use the ‘Custom’ setting. After the URL is entered, you’ll select an ‘alias’ for your Org. Again you can call it whatever you want, but you’ll need to enter it again later so don’t forget what you called it. You’ll then be prompted to authorize the org via OAuth. I find this to be so convenient since you won’t ever have to mess with SF’s Security Tokens.

Additionally if the username on the OAuth screen is being populated with extra stuff (i.e. “/oauth/test”), ensure the custom url you’re using is something like —

https://ab12.salesforce.com/

and NOT like —

https://ab12.salesforce.com/?un=sfadmin%40testorgname.com.sandbox

After a successful login, select ‘Allow’ from the following screen which will give SFDX access to your org.

Step 4

Run the following command -

sfdx force:mdapi:retrieve -r ./temp -u <org alias here> -k ./manifest/package.xml

For example -

sfdx force:mdapi:retrieve -r ./temp -u MyTrailheadPlayground -k ./manifest/package.xml

This could take a while to run. You should see packages queueing up in your terminal with “status” and “jobId” listed. The great thing about this is you can declare what data you’d like pulled own in the manifest/package.xml.

If you authorized an org but it didn’t get an alias assigned, use the following command to set an alias

sfdx force:alias:set YourDesiredAlias=username@example.com

Step 5

Run command

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

You should see activity in the Terminal indicating that the files are being unzipped. When it’s completed you should now see ‘unpackaged.zip’ and a folder name ‘unpackaged’ in your ‘temp’ folder.

Step 6

Now in the Command Line run —

sfdx force:mdapi:convert --rootdir temp

This will relocate all of your metadata files to the ‘force-app/main/default’ directory.

Step 7

Delete the ‘temp’ folder. If you leave the ‘temp’ folder in your directory, it is still under the umbrella of the “Global Search” function so you may find yourself developing on the wrong file.

And you’re done! Now running the ‘sfdx retrieve/ deploy’ commands will work appropriately! Enjoy :)

Helpful Tip

Run all of these commands at once using a Bash Script! Get it here — “Use a Bash Script to Connect VSCode To a Salesforce Org

--

--

Alex Barnes

Salesforce Developer at RedPoint Solutions in Denver, CO