Version Control Implementation on SFDC

Step by step guide to setup version control in salesforce.

Whole version control session will consist of 3 Articles. This is the first article in which we are going to set up our Code Base from the Scratch that means we will be pushing our whole code into the Remote Repository. In later articles we will be discussing how this code base can be used by different teams and is the only source of truth. In the last article of this series we will see how the code can be deployed across different sandboxes.

So, Lets start setting up our code base on GitHub but before..

Why version Controlling?

For almost all software projects, the source code is like the crown jewels — a precious asset whose value must be protected and salesforce is not different.

We all know salesforce works in a multitenant environment and the same set of resources are being shared across the team/Individuals. In projects it’s seen very often that someone’s code is overwritten by someone else, as both of them were working on the same resource(Code). These issues are faced by almost all the developers in their day to day life. So, in order to make sure this doesn’t affect the deliverability of the project and resources are being utilised wisely and not waste time in rewriting the same code. VCS has been implemented in a lot of SFDC projects.

In this article i am going to explain how the Version Control system can be implemented with salesforce using Visual Studio Code as IDE and GITHUB as our Source platform. Our main Aim in this article is to push the Code from the production into Repository. For those who don’t know what a repository is? Consider it as a place where your code(File) can be stored, something like google drive or a central location in which your code is stored and managed. In order to work in your local machine you can pull the copy from your repository and bring it into your Local Machine.

Lets start by creating an Account on GITHUB using https://www.github.com

Once you enter all the details and logged in to your GITHUB account. You will see a home screen. Click on New Button to create your New Repository

Click on New Button to create your New Repository

You can give your repository a name(here ’sfdcDemoRepository’) and make it either Public or Private based on your use case. Private repositories means no one on the GitHub platform will be able to see your repository until you provide access to someone, whereas, Public means anyone can see your repository. Once set click on the ‘Create Repository’ button.

Please copy the highlighted link and store it somewhere, we will use it in the later steps.

We have just created a repository but there is no data inside it.

As our next step we need to install visual studio code from https://code.visualstudio.com/

Install VScode and Open it and you should see a welcome screen like below

Another important step is to install Salesforce CLI in the System. Salesforce CLI is a Command line interface tool used to synchronize your sfdc data into your Local machine. It simplifies development and build automation when working with your Salesforce org. Currently we are only leveraging its ability to Connect between our org and local machine to exchange data. In more simple words just imagine CLI as a middle man between Visual Studio Code and your Org which will help you move your data across each other.

You can Install Salesforce CLI from

https://developer.salesforce.com/docs/atlas.en-us.sfdx_setup.meta/sfdx_setup/sfdx_setup_install_cli.htm

Once installed in your System you can test it via two ways if it’s installed correctly

1>open your CMD and type sfdx. If it’s installed correctly you will see the screen below.

2>Open your Visual Studio code and open the terminal. So those who are new to VS Code, it provides its own Command prompt which works very similar to windows Command prompt.

To open the terminal for the first time click on the highlighted tab on the bottom left most and select terminal and type ‘sfdx’.

If it’s installed correctly you will see below screen

Salesforce Cli provides many commands that are used to make connections between your org and Visual Studio code. But it is recommended if you install a salesforce extension, that will write the command for you and you don’t have to remember CLI commands. Most commonly used extension is “Salesforce Extension Pack”.

We are all set to go!

Press Ctrl+shift+P, Command pallet will open and select sfdx:create Project with manifest

Then Select “Standard”

Give your project a name and Enter

Locate your project in your local machine and click “Create Project” You will see your project is getting created in the Local.

Once the project is created you will see the screen below

Now we need to connect this project with our Org. Again Press ctrl+shift+P and select sfdx:authorize an org(highlighted in Green) and follow the instructions.

You will see, you are being redirected to login.salesforce.com (if you want to connect with sandbox then test.salesforce.com). Enter your Login credentials

Once your org is connected you will see a success message like below screen.

Now that we are connected with our Org, we need to bring all the Code that is residing in our Production Org. To do that, open the package.xml file from the left hand side of the sfdcDemoproject folder. Package.xml is basically a XML file that contains all the by default components that you want to bring to your local machine. In case if you want to bring more components or remove a few of the components then you can modify it as per your need.

Right Click on the Package .XML file Screen and Select retrieve Source in Manifest from org.

This process will take some time depending on the amount of data you are fetching from the Org.

Now, I have retrieved all the Data from my Prod(Org) into my local machine as you can see all the classes, pages and triggers. I have opened a few classes, you can have a look at the screenshot attached.

Now we are going to push all the data from our local machine into our GitHub repository(that we created earlier). To do this we need GIT installation in our Local Machine. GIT and GITHUB are two different applications. GIT comes as a whole platform and GITHUB is an application on top of it. It is very similar to the Force.com and Salesforce where force.com is the platform and Salesforce is built on top of it. You can follow the link to install GIT https://git-scm.com/download/win

Once installed successfully, Open your Cmd and type “git” if it runs that means it has been installed successfully. It is the same as installing salesforceCLI in the system.

VisualStudioCode (IDE) leverages the machine’s Git installation and we can use it directly from there. To check whether git has been leveraged properly by VSCode we can click on source control from the left panel and check if it contains the “Initialize repository” Option. If you can see the same screen as below then you are good to push your changes into the GITHUB.

Lets Understand what exactly happens when we click “Initialize Repository”. When we click Initialize repository it converts our current working directory into a git repository by adding .git subdirectory into the workspace folder. Because most of the git commands are not available outside of an initialized repository. That’s the reason git init(git initialize) is considered as the first command before using it.

Once the repository is initialized you will see all your Codes under the Source Control Section. It will also create a by default “master” Branch as our current Branch. See the screenshot below

Now, we need to make our code ready to be sent over the remote repository(GITHUB repository). All the Files in the source control panel have been marked as ‘U’. It basically means they are not ready to be moved to a remote repository and are known as “Unstaged”. In order to make sure a file is ready to be moved into the Remote Repository is once you Stage them. We can stage a single file as well but in this case as we are moving to the remote repository for the first time. So, We need to stage all of them.

Click on more action from the Source Control Pane and select Stage All Changes as per the screenshot below. We can also “stage” via git command “git add” but the VSCode can do it for us without memorizing the command.

Once all the Files have been staged you will see below screen →

Now the system knows all the files which are ready to be sent over the remote repository. Now we will make a package or commit these files and push it to the repository.

Click on Commit as per the screenshot below with your message in the message box.

Now our Package is ready to be pushed or in terms of Git we can say, We have committed our changes and Now ready to Push.

Once you click the Push option from the More Action button, you will see below screen

It says your repository has no remote configured to Push to, That means we have not connected to our GITHUB. Without the connection you will not be able to PUSH your commits.

Click on Add Remote and select “Add Remote from GITHUB”

It will redirect to the GITHUB website for authentication. Once you enter your Username and Password. You will get an Authorization success screen. Click on Open Visual Studio Code.

It will redirect to the visual studio page where we will see a list of all the repositories of our GITHUB Account. We need to select the repository that we have created for this operation i.e. sfdcdemorepository.

Select the repository and you will see your changes are being pushed to the remote repository. You can go to

your remote repository and verify it from there.

As you can see this is the repository that we created initially and now it has all the Production code and is ready to become our source of truth.

In our next article, I will be explaining how your team members can use this repository to pull the code and start developing their own feature and Push it back to the remote repository to merge.