Data from Salesforce can be exported and used for backup. What about metadata?
How do we know what changes went in a specific release? Can we track the changes from release to release?
Visual studio code (VS Code) is the best tool to retrieve the metadata of your salesforce org and push it to Git so that you will have the backup of all versions of metadata and can be rolled back to your previous org configuration in case of primary data failure.
STEPS TO RETRIEVE METADATA FROM SALESFORCE AND PUSH TO GIT USING VS CODE
- Create Project with manifest
- Connect to Salesforce Org
- Retrieve Metadata from the org
- Push Metadata to GIT
CREATE PROJECT WITH MANIFEST
Open VS code and press ctrl+shift+p in windows or cmd+shift+p in mac to open the command palette.
Search for SFDX: Create project with Manifest and select it.
Select Standard project template and enter the project name you would like to have and choose the folder in your system to save the project files.
CONNECT TO SALESFORCE ORG
Open command palette in VS Code and search for SFDX: Authorize an Org and select it.
Choose the org type (Production, Sandbox or Custom) that you would like to connect and type an org alias and hit enter.
You will be taken to the Salesforce login page, enter the credentials and login.
Switch back to VS Code and you will see the notification SFDX: Authorize an org successfully ran.
RETRIEVE METADATA FROM THE ORG
Under the manifest directory on the left pane in VS Code you will see a file package.xml which will define what components to retrieve from or deploy to the org. We can manually enter in this file or we can use a plugin as mentioned in the next step.
There is an extension in VS Code called Salesforce Package.xml Generator for VS Code which will allow you to update the package.xml file by selecting the available metadata components from your org.
Install the extension and open command palette and search for SFDX Package.xml Generator: Choose Metadata Components and select it.
You will be taken to a screen which will look like this
Select the metadata components that you would like to retrieve from your org and click Update package.xml at the top of the screen.
You will see the updated package.xml file now.
Right click on the package.xml file and click on Retrieve Source in Manifest from Org.
You will see the notification Retrieve source from org Successfully ran.
Retrieved files can be seen under the force-app directory on the left pane.
PUSH METADATA TO GIT
Open the terminal in the VS Code and configure your username and email using the below commands.
git config --global user.name "Your username" git config --global user.email "Your emailaddress"
To initialize the project folder as a local repository use the below command.
Or to clone a remote repository use this command
git clone <remote repository link>
Add the folders that you want to include in the local repository using the below command.
git add <folder name>
Commit changes to the local repository with a comment using the below command.
git commit -m "first commit"
Now, add a remote repository to which you want to push the files, using the below command. Your address varies with respect to your host and repo name.
git remote add origin <respository url>
Set the branch to main or master depending on the hosting service you use. GitHub changed its default branch to main on October 1 2020. To know more about why GitHub changed its default branch from master to main, check this link.
git branch -M main
Push your files to your remote repository using the below command.
git push -u origin main
Open your remote repository and you will see the pushed files.
WRAPPING IT UP
In this blog we have covered how to retrieve metadata and push it to a remote repository using Git.
Leave a Comment