- Download and install Visual Studio Code
- Setup Visual Studio Code
- Clone the repository
- Create a Project and Authorize the Org
- Retrieve Metadata from the org
- Push Metadata to Master/Main
- Create a branch from master in GIT and push the changes
- Review the changes
DOWNLOAD AND INSTALL VISUAL STUDIO CODE
Install Vs Code Download Visual Studio Code - Mac, Linux, Windows
SETUP VISUAL STUDIO CODE
- Install GIT
- Install Salesforce CLI Salesforce CLI
- Install JDK version 17
- Click on gear symbol at the bottom left corner of VS Code and select Settings.
- in the search bar type java home. Under java home you will find a blank bar. Head to the Java folder in your system where JDK 17 has been installed, copy the path(C:\Program Files\Java\jdk-17.0.2) and paste it as java home value.
- Another way to do it is by searching for the settings.json file from the search bar and changing the java home path
- Install the following extensions in VS Code
- Salesforce CLI Integration
- Salesforce Extension Pack
- Salesforce Extension Pack(Expanded)
- Salesforce Package.xml Generator for VS Code
- Restart VS Code
CLONE THE REPOSITORY
- Create a GitHub account, if you don’t have one.
- If you have an account already, login and Create a new Repository(if you do not have any existing repository).
- Click New.
- Enter a name for the repository, type an optional description, choose whether you want it to be public or private, check the Add a README file option if required and click on Create repository.
- If you have created a new repository, copy the link of the repository.
- If you already have an existing repository, open the repository and copy the link from the address bar of the browser.
Open VS Code and open the terminal using the command Ctrl + ` .
Configure your user email using the below command.
git config --global user.email <user_email>
Use the below command to clone the repository.
git clone <repository_link>
Sign in with your browser or use a personal access token.
- Authorize GitCredentialManager when prompted.
- Confirm access with your password when prompted.
- You will see a screen mentioning the Authentication Succeeded.
- Now the repository will be cloned into your local.
CREATE A PROJECT AND AUTHORIZE THE ORG
- Press Ctrl+Shift+p in windows and Cmd+Shift+p in mac to open the command palette and search for SFDX: Create Project with Manifest and select it.
- Choose the Standard template, enter the Project name and choose the destination folder as the cloned repository folder to save the files.
- It should take around 5 minutes for all the extensions to get ready before authorizing the org.
- Now, open the command palette and search for SFDX: Authorize an Org and select it.
- Choose the type of Org i.e. sandbox or production or custom. Here we pull metadata from Production, so we choose production.
- Type an Org alias and hit enter.
- You will be redirected to Salesforce login page, enter the credentials of the target org and click allow when prompted.
- Now head back to the VS Code and you will see the notification SFDX: Authorize an Org successfully ran on the bottom right corner of the screen.
RETRIEVE METADATA FROM THE ORG
- The file where we define what metadata to be pulled from the Org is the package.xml.
- A single package.xml file cannot pull more than 10,000 files from the Org. We may have to use another package.xml file to pull the remaining metadata.
- Open the command palette and search for SFDX Package.xml Generator: Choose Metadata Components and select it.
- You will see a new tab and the metadata from your org will be processed for a few seconds.
- Click Select All next to Metadata Types and most of the metadata from your Org will be selected and the data will be processed in alphabetical order for a few minutes. You will receive a notification when all the metadata types are imported.
Metadata types can also be selected manually.
Click UPDATE PACKAGE.XML present on the blue header of the screen and your package.xml file will be updated.
We have the default package.xml file. If you have more than 10,000 files to be retrieved, under the manifest folder add another file and name it as package2.xml.
Copy the first two lines and last two lines from the package.xml file and paste it in the package2.xml file as shown below.
<?xml version="1.0" encoding="UTF-8"?> <Package xmlns="http://soap.sforce.com/2006/04/metadata"> <version>56.0</version> </Package>
Split the components in the types tags between the two xml files.
Right click on the package.xml file and select SFDX: Retrieve Source in Manifest from Org.
- All the metadata from the package.xml file will be retrieved into the Project.
- Similarly retrieve the metadata from package1.xml file.
PUSH METADATA TO THE MASTER/MAIN (From Production)
Check for the files to add to GIT
Add all the files to the staging area for GIT using the below command.
git add .
Check whether all the files are added again, the file name displays in green if already added.
Commit records the changes made to the files in the local repository, use the below command to commit changes to the local repository.
git commit -m “commit message in quotes”
To check the current state of the repository use the below command.
Push the changes to the remote repository using the below command.
We may need to provide Password (which we can get/generate from GIT) when prompted
CREATE A BRANCH FROM MASTER IN GIT AND PUSH CHANGES
Authorize the VS Code to the Org where the new feature has been developed.
Create a new branch and check out to the branch using the below command.
git checkout -b <feature_branch_name>
Retrieve the metadata from the Org using the package.xml files.
Check the status of the files.
Add all the metadata changes using the below command to the staging area for GIT using the below command.
git add .
Check the status again to make sure all the files are added.
Commit all the changes to the local repository using the below command.
git commit -m “commit message in quotes”
Check the status of the files after committing to ensure all files are added.
Push metadata to the new branch using the below command
git push --set-upstream origin <feature_branch_name>
REVIEW THE CHANGES
- Create a new pull request.
- From the compare dropdown choose the feature branch created.
- You can see the number of commits, number of files changed and also the number of contributors.
- Now click on the Id where you can see View commit details text when hovered over.
- All the GIT user interfaces(Bitbucket/Azure Devops/GitHub) allow us to view the differences between the master and the branch.
- The GitHub interface displays the changed files with the number of additions and deletions
- The code can be viewed in split or unified mode.
- We can enter comments for a specific line of code by hovering over it and clicking on the + icon that pops up.
- Developer fixes the code in VS Code and pushes the changes to the feature branch.
- After the final review, the branch can be merged with the master.
- The code is now ready to be pushed to production.
WRAPPING IT UP
In this blog we have explained how to establish a code review process using Git and VS Code.