Deploy & Retrieve metadata using Salesforce DX and VS Code

Visual studio code is one of the most featured, powerful code editors in open source platform developed using Javascript frameworks, node.js, and electron. In this blog, I would like to share with you the steps to deploy and retrieve the metadata from the Salesforce developerproduction and sandbox orgs using the SFDX and my favourite Visual Studio Code.

Please refer my previous blog for the installation of SFDX on Visual Studio Code. Salesforce announced that VS code will be our official Integrated Development Environment (IDE). This informs us that, there won’t be any further updates on the Force.com IDE.

Open Visual Studio Code IDE and follow the below steps.

  1. Create an SFDX Project

open a new folder VS code like below.

Open VS Code from a new folder

Press Ctrl + Shift + P or Cmd + Shift + P to open the command palette, which gives couple of options. Select the SFDX: Create Project option, if it is not showing then type the command in the input box.

Open Command Palette
SFDX: Create Project

Once you have done the previous one, another popup will open up for the project name. Enter the project name and click Enter, then it asks for saving location.

Enter Project Name
Enter the Project name and click Enter

Or

Open the integrated command prompt in VS code. Press Ctrl + ` in windows and cmd + ` in mac os, or go to View->Integrated Terminal, which open up the terminal window of our Operating System in the footer pane of VS code.

Terminal Window
Integrated Command Terminal

Once you have opened the terminal, type the below command for creating a new project using CLI (Command Line Interface of SFDX).

sfdx force:project:create –projectname

create project by command
Create a project with project name as SampleProject

2. Login to Salesforce org

Use the below command to log in to Salesforce production.

sfdx force:auth:web:login --setdefaultdevhubusername --setalias sfdcevangel
login command sfdx
  • The alias attribute is used to set as a reference for future access from the editor. The session id of the logged-in org saves against the alias attribute

The command redirects from the editor to the standard login page URL (https://login.salesforce.com) in the default browser or to a new tab in the active browser. Grant the permission to VS Code to access your org metadata and records once you are successfully logged in.

allow access vs code in salesforce

After a successful login, SFDX CLI will show the below message.

successful login vs code salesforce.JPG

To list all the connected orgs, use the list command as follows.

list of connected orgs
List of all connected org with its alias

Note: If you want to change the salesforce org default login to sandbox or other specific domain, then change the login URL attribute name ‘sfdcLoginUrl’ from ‘sfdx-project.json’ file in our project.

To open the connected org using the open command.

sfdx force:org:open -u

3. Create Package.xml

Now, let’s create the package.xml file detailing about the metadata to be retrieved.  The other metadata retrieval/deployment tools like ant or Force.com IDE uses the same XML schema like below.

package.xml
Package.xml

4. Retrieve metadata

sfdx force:mdapi:retrieve -r metadata -u <alias name> -k <package.xml location>

Retrived Metadata
  • -u: Username or alias of the connected Salesforce Org
  • -r : Location where zip file should be saved
  • -k: location of the package.xml

Here, metadata is the folder name where the retrieved metadata zip file is to be stored (retrieved metadata will be downloaded as a .zip file). As you can see in the above image, metadata/unpackaged.zip is downloaded from the connected org as part of the sfdx retrieve command.

To proceed further we need to unzip the file, use the below command for that.

unzip -o metadata/unpackaged.zip -d metadata

  • -o: the source file location
  • -d: destination folder name or path
unzipped tree structure.JPG

Now, you can work on the retrieved aura component metadata and deploy to production org or any other orgs (ensure that the org is connected to the code, follow step 2 for the same).

5. Deploy metadata

Deploy metadata using the force:mdapi:deploy command. Before deploying to an org, we can validate the metadata using the same command, by adding an attribute.

First of all, validate the metadata dependency using below command.

sfdx force:mdapi:deploy -c -d metadata/unpackaged -u sfdcevangel -w 20

  • -u: Username or alias of the connected Salesforce Org
  • -c: the deploy command only validate the package
  • -d: Folder location of non-zipped files including the package.xml file
  • -w: Wait time in minute

Additional attribute for the zipped file

  • -f: location of zip file to be deployed or validated including the metadata and package.xml
validation result

If the validation is fine, then deploy the metadata by removing the ‘-c’ from the above SFDX command.

sfdx force:mdapi:deploy -d metadata/unpackaged -u sfdcevangel -w 20

Note: you can connect a new org with the editor and deploy the code to it by changing the alias name alone.

deployment

we can wait for some time to get it deployed. The delay time will increase according to the metadata size and dependencies. As you can see in the below image that the metadata components in the package.xml are deployed successfully.

deployment status
The validation and deployment was a success
deployment status detail
Detailed view of the deployment – shows the number of components gets succeded

Don’t forget to share the post if you like it & bookmark the blog for future references. If you have any comments or doubts on this post, Please comment on the box.

Sources :

https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_build_mdapi_deploy.htm

https://developer.salesforce.com/docs/atlas.en-us.sfdx_cli_reference.meta/sfdx_cli_reference/cli_reference_force_org.htm

6 Thoughts to “Deploy & Retrieve metadata using Salesforce DX and VS Code”

  1. No matter if some one searches for his essential thing, therefore he/she wishes to be available that in detail, so that thing is
    maintained over here.

    1. I wish I had THUMBS UP feature in here 🙂

  2. Great blog here! Also your site a lot up fast! What host are
    you using? Can I am getting your affiliate link in your
    host? I desire my website loaded up as quickly as yours lol

  3. When someone writes an piece of writing he/she keeps the plan of a user in his/her mind that how a user can know it. Therefore that’s why this post is outstdanding. Thanks!

    1. Thank you for such kind words.

Leave a Comment