DEV Basics: How to set up your computer for Salesforce Development
Despite Salesforce ongoing efforts to simplify implementations by moving from coding to configuration (That’s what they usually call low-code or no-code), sometimes there is just the need to leverage the coding abilities to modify Salesforce in order to fully meet clients requirements or to implement complex processes and logics. In those situations I often experience Salesforce consultants, who are feared of coding or developers who still default to the developer console when it comes to implementing smaller or larger APEX logics, performing SOQL queries or even creating interface components. And even if there is nothing factually wrong about this — the developer console will do the trick — there are just better ways to code for Salesforce that will simplify the work, let you write better code and speed up the overall development time.
Due to some recent events, involving the german train service, a tray with several full coffee cups and a train attendee that got surprised by an upcoming curve in the train tracks, I was faced with a rather wet then functioning MacBook and had to set up my development environment from the ground up.
That’s why I want to use this post to share my personal — and I can only highlight this word since I urge everyone to set up their own environment how they like it — developer environment with you and give a step by step guide on how to start with coding for Salesforce.
Additionally, as I am writing this, I am planning to present the post contents to the Salesforce Developer Group, Hamburg on February 10th, 2022.
Step 1 — How do I organise my work?
Developer or not, I promise you that you will be able to follow me on this step! As everywhere in professional work life, self-organisation is key. And in development that for example means, that you should come up and maintain a proper folder structure on your device that lets you easily navigate between tasks. As I am working in a consultancy, the folder structure is extra critical to keep the separation between clients and to make sure that I do not mix up work. That’s why I use the following folder structure:
In my home folder /Users/florian, I do have a dedicated folder projects that contains everything client related. In that folder, each client that I work for gets their own dedicated folder. If the consultancy would staff me for example on a client called “Banana Corporation”, I would create a folder in there abbreviating the clients name in camel case, bananaCorp.
Obviously you can be a bit creative here. Personally, I usually try to not show the contents of my /projects folder, but since we do have NDAs in place with several customers, I tend to come up with folder names for those clients that are obvious to me, but do not reveal the clients to other clients when I should accidentally show the folder during screen share. In that case, Banana Corporation would be named something likefruitCorp.
As I am not only developing for Salesforce, but also for MuleSoft and other technologies, thats my next folder layer. In case you are just working with one technology, you probably do not need this.
Lastly, when setting up a new client, I create a new folder called Workspace in the Salesforce folder. This is where Visual Studio Code will “live” in. There is more on this later.
Step 2 — Terminal
Personally, I like to work in my terminal. You might see that differently and this guide — especially the steps around Salesforce CLI and Visual Studio Code — should enable you to not be needing the terminal at all, but since I also customised my terminal and you are probably seeing screenshots of it even in this post, I don’t want to leave them unmentioned.
Usually, the terminal looks something like this:
Not really nice, huh? And on windows, it is probably even uglier. That’s why I go with Hyper. Hyper is a very easy to install, themeable and extendable terminal replacement that is build using modern technologies (Electron to be precise). As you can install various themes, mine currently looks like this:
Step 3— Git
There is no other way to say this: If you are not using git yet, you should start using it immediately. And this is not my personal opinion, but consensus in the whole developer sphere, spanning basically all technologies, industries and projects. But why? Well, git gives you versioning of your code, enabling to go back in time to restore functioning versions of your code or to collaboratively work on code and merge the changes together in the end. I am not going into detail on this as there are various posts about this and also trailhead has this covered quite intensively.
But what you need to know is how you set git up on your computer. In order to do that, the easiest way on Mac is to open the terminal and execute the following command.
git --version
If not installed yet, an installation dialog will open and guide you through the installation. On Windows, just follow the official installation guide.
As git is using encryption to communicate with git servers such as github, bitbucket or GitLab, you need to generate a key pair on your device. The key pair consists of a public and a private key, that are mathematically work together. In easy words, you can share the public key with whomever you want and they can do two things with it:
- verify that a message that you send is indeed yours and was not modified during the transmission
- send you a message that only you can read
But how do we generate such a keypair? Well — using the terminal :)
Let’s open the terminal again and enter the following command, replacing the placeholder with your (professional) email address:ssh-keygen -t ed25519 -C "your_email@example.com"
Here is how the output looks like:
The private and public key will be placed in a .ssh folder within your user folder and contain an id_ed25519 and id_ed25519.pub file, the former being the private and the latter being the public key.
Depending on the git server that you use, you need to import that identity to that server (meaning that you tell the server your public key). In order to do so, you first need to copy the contents of the public key file. In the terminal, you can show the contents of a file by using the cat-command.
cat .ssh/id_ed25519.pub
In Bitbucket for example you enter the public key by opening the SSH-Key settings, clicking on “Add key”, giving it a name and pasting the public key file contents.
Step 4— Salesforce CLI
Now lets get Salesforce-specific. To interact with Salesforce from our computer, we need some kind of interface that takes our commands and interacts with Salesforce, performing the actions that we want to do. As a default in development, this task is usually done by tools that can be executed in the terminal which are therefore called “Command Line Interfaces” or short CLIs. Salesforce publishes such a CLI, so there is not much that we need to configure or set up here. You can simply download it from here and install it like any other program.
But what’s the benefit of this tool? Well, since we do have a working terminal, we could try it out, right?
So after the installation has been done, we can go to the terminal (maybe you need to re-open the terminal) and execute the sfdx command. As we have just called the CLI, but didn’t specify what it should do, the output should just be the included help text which looks like this:
If you want to frequently use the Salesforce CLI directly from terminal, you might want to enable autocompletion which can be done by following the instructions that are shown if you execute sfdx autocomplete.
But now, lets do something useful like querying records from an org. In order to do so, we need to tell the CLI which Salesforce org we want to query records from and log in to that org. We do that by typing sfdx auth:web:login -a mySandbox --instanceurl <instanceurl>.
The parts prefixed with one or two hyphens are parameters. For this command-a can be used to give an Alias for the org that you are connecting to and--instanceurl is used to give the Instance URL to the command.
Generally speaking, the CLI itself is also the best help resource, if you have questions, how a specific command works. Usually, you can execute the sfdxand all sub-commands with the --help parameter to show useful information about the command and get explanations on all parameters.
Executing the command should open our default browser and direct to the Salesforce login page. Once you have logged in like you would do when accessing Salesforce in your Browser, you are asked to allow the Salesforce CLI. After you confirm that request, the and the terminal window indicates that the authentication was successful and prints out the username of the authenticated user.
Using that authenticated user, we can query some records. In order to do so, we can executesfdx force:data:soql:query -q “<query>” -u <username>. In this query the -q parameter is used to give the SOQL query to the command and the -u parameter is used to determine the user (that we previously authenticated). For example, the output of the query above could look like this:
Step 5— Visual Studio Code
Last, but definitely not least, we are now setting up Visual Studio Code. You might ask yourself what Visual Studio Code is or you even confuse it with Visual Studio that is widely used in .NET and C++ development.
Now, what is Visual Studio Code? Basically, it’s a text editor, but it is extensible and there is a huge community behind it that is constantly creating and maintaining plugins that help you work with it and make your life easier.
Because of the huge developer support and adoption, companies and software vendors have started to also create plugins to support their product in Visual Studio Code — and so did Salesforce: In their movement from the classic, rather slow and proprietary development environments (you might remember the developer console) in the direction of modern and source driven development, they have also opted for Visual Studio Code as the primary development environment.
So here is a rundown of what Visual Studio Code can do for us. But before that, let’s quickly install it by downloading the latest version for your system from here.
Extensions
The great thing about Visual Studio Code is that you can basically use it for any programming language. You just need to install the right extension. So the first thing that we are going to do is installing the official Salesforce Expansion Pack or the Extended Salesforce Expansion Pack. Those give you the ability to communicate with Salesforce via the CLI, but also some nice other features such as syntax highlighting for APEX, Aura Components, LWC and Visualforce as well as access to the debugger, unit tests and the apex logs. In order to install the extensions, you simply open the extension menu on the left and search for “Salesforce”.
After the extensions are installed and a quick reload of Visual Studio Code, we can start to create our first project. To do that, we will use the most important key shortcuts that you need to know for Visual Studio Code: SHIFT + CMD/STRG + P. This shortcut opens the Command Palette, which lets you search through the available commands for the installed extensions. As we want to create a project, we search for “sfdx” and select “SFDX: Create Project”.
This guides us through the creation of a new project. For our first project, we want to enter the following information into the requested fields:
- Project Template:
Empty - Project Name:
yourProjectName
In the following folder selection dialog, you choose the Projects folder according to your folder structure (example above). It will create a new folder with the projects name within the selected folder. In my case, I chose the name “ServiceCloudImplementation”, so the full path is /Users/florian/projects/bananaCorp/Salesforce/ServiceCloudImplementation.
Once confirmed, the project will be created with the core structure of a SFDX Project. At this point we will not go into detail on this, but directly connect to an org and fetch some metadata from it. To do that, you open the project and notice, that it tries to already connect to a “Default Org”, that we have not set so far. So open the Command Palette again or click on the “No Default Org Set” Text in the lower left of Visual Studio. This time, you select “SFDX: Authorize an Org”.
Visual Studio Code now prompts you for the very same parameters as you would have entered into the Salesforce CLI (the login URL and an alias), just gives you some options so that you don’t need to type it. If you have followed this guide so far, you will notice that the previously authenticated org from the Salesforce CLI chapter is already appearing in the list with the alias that you gave before. Afterwards, Visual Studio Code directly executes the CLI command, which opens the Browser where you can log in as previously. Afterwards, you can select the org as default. Once this is done, you will notice a new icon on the left icon bar: a Salesforce cloud icon.
If you click on it, it gives you access to your orgs metadata and lets you fetch the metadata with a little download icon.
And that’s it! Your computer is ready to develop for Salesforce! If you want to learn more or earn some points on trailhead for what you just have done, here are some links that you can follow:
- Trailhead Module “Install Development Tools”
- Trailhead Module “Command-Line Interface”
In the next blogpost, you will learn how to utilize these tools to develop for Salesforce.
