Use a Bash Script to Connect VSCode To a Salesforce Org

Alex Barnes
3 min readAug 5, 2020

--

As a Salesforce Developer and Consultant, I connect to a new org anywhere from 1 to 3 times a week. The steps I outlined in another article, “How To Connect VS Code to a Salesforce Org (For Beginners)” give an easy introduction to those who may be just getting used to running SFDX commands. However, once you’ve run those same commands 100+ times, things can get a little tedious. That’s where I’ve found major relief in the following Bash script to help automate the SFDX commands. I’m running this script on a Mac and have VSCode as well as the full suite of Salesforce Extensions installed.

We’ll add this script to your Bash Profile so that you can access it anywhere from the command line. To access your Bash Profile using VSCode, run the following command in the Terminal from any directory —

code ~/.bash_profile

Your window should look something like this if you’ve not updated your Bash Profile before—

Next, paste the following snippet into your profile and Save

function connectOrg {
echo "Please Enter New Project Name"
read projectName
echo "Creating Project $projectName..."
sfdx force:project:create -x -n $projectName
cd $projectName
echo "Is this for a Sandbox[s] or Production[p] org?"
read orgType
while [ "$orgType" != "s" ] && [ "$orgType" != "Sandbox" ] && [ "$orgType" != "p" ] && [ "$orgType" != "Production" ] do
echo "I'm sorry. I didn't understand your response."
echo "Is this for a Sandbox[s] or Production[p] org?"
read orgType
done
echo "Please enter an Alias for this org:"
read orgAlias

if [ "$orgType" == "s" ] || [ "$orgType" == "Sandbox" ]
then
echo "Authenticating Sandbox org..."
sfdx force:auth:web:login --setalias $orgAlias --instanceurl https://test.salesforce.com --setdefaultusername
else
echo "Authenticating Production org..."
sfdx force:auth:web:login --setalias $orgAlias --instanceurl https://login.salesforce.com --setdefaultusername
fi
echo "Authentication Successful"
# CLI notifies user that it's "Retrieving Source"
sfdx force:mdapi:retrieve -r ./temp -u $orgAlias -k ./manifest/package.xml
echo "Successfully retrieved metadata!"
echo "Unzipping data"
unzip ./temp/unpackaged.zip -d ./temp/
echo "Unzip successful!"
echo "Converting metadata to source format...."
sfdx force:mdapi:convert --rootdir temp
echo "Conversion complete!" echo "Delete temp folder? [y]/[n]"
read deleteTemp
while [ $deleteTemp != "y" ] && [ $deleteTemp != "n" ]
do
echo "I'm sorry. I didn't understand your response."
echo "Delete temp folder? [y]/[n]"
read deleteTemp
done
if [ $deleteTemp == "y" ]
then
echo "Deleting temp folder..."
rm -rf ./temp
echo "Success!"
fi
echo "Org Connection Complete!"
echo "Opening Project in new Window..."
code .
return
}

NOTE: You will need to restart your Terminal before the script will run.

Once this is in place, you can connect VSCode to Salesforce Orgs simply by running the following command from the Terminal —

connectOrg

Consider that the script will create the new Project Folder in whatever directory you are in at the time of running, so be sure to navigate to the appropriate directory. Optionally you can add a “change directory” command to the beginning of the script so that it always navigates to the appropriate directory. Mine is as follows —

cd ~/Library/Application\ Support/Code/User

NOTE: The forward slash is an ‘escape space’ character since the folder “Application Support” has a space in the name.

That’s it! Hope this helps to make your Salesforce development life easier!

--

--

Alex Barnes

Salesforce Developer at RedPoint Solutions in Denver, CO