back to blog
Retrieve Metadata from Salesforce and Push to Git
September 27 2021 • 25 min read

BUSINESS CHALLENGE

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.

PREREQUISITES

  • Install VS Code

  • Install Salesforce CLI

  • Install GIT

    STEPS TO RETRIEVE METADATA FROM SALESFORCE AND PUSH TO GIT USING VS CODE

    1. Create Project with manifest
    2. Connect to Salesforce Org
    3. Retrieve Metadata from the org
    4. Push Metadata to GIT

    CREATE PROJECT WITH MANIFEST

    1. Open VS code and press ctrl+shift+p in windows or cmd+shift+p in mac to open the command palette.

    2. Search for SFDX: Create project with Manifest and select it.

      screenshot 488

    3. 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.

      screenshot 489

    CONNECT TO SALESFORCE ORG

    1. Open command palette in VS Code and search for SFDX: Authorize an Org and select it.

      screenshot 492

    2. Choose the org type (Production, Sandbox or Custom) that you would like to connect and type an org alias and hit enter.

      screenshot 404

    3. You will be taken to the Salesforce login page, enter the credentials and login.

      screenshot 459

    4. Switch back to VS Code and you will see the notification SFDX: Authorize an org successfully ran.

      screenshot 460

    RETRIEVE METADATA FROM THE ORG

    1. 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.

      screenshot 495

    2. 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.

      screenshot 497

      Install the extension and open command palette and search for SFDX Package.xml Generator: Choose Metadata Components and select it.

      screenshot 499

    3. You will be taken to a screen which will look like this

      screenshot 500

      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.

      screenshot 501

    4. Right click on the package.xml file and click on Retrieve Source in Manifest from Org.

      screenshot 503

      You will see the notification Retrieve source from org Successfully ran.

      screenshot 505

      Retrieved files can be seen under the force-app directory on the left pane.

      screenshot 507

    PUSH METADATA TO GIT

    1. 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"
    2. To initialize the project folder as a local repository use the below command.

      git init

      Or to clone a remote repository use this command

      git clone <remote repository link>
    3. Add the folders that you want to include in the local repository using the below command.

      git add <folder name>
    4. Commit changes to the local repository  with a comment using the  below command.

      git commit -m "first commit"
    5. 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>
    6. 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
    7. Push your files to your remote repository using the below command.

      git push -u origin main
    8. 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

Your email address will not be published

© 2024 Digital Biz Tech