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 developer, production 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.
- Create an SFDX Project
open a new folder VS code like below.
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.
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.
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.
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
2. Login to Salesforce org
Use the below command to log in to Salesforce production.
sfdx force:auth:web:login --setdefaultdevhubusername --setalias sfdcevangel
- 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.
After a successful login, SFDX CLI will show the below message.
To list all the connected orgs, use the list command as follows.
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.
4. Retrieve metadata
sfdx force:mdapi:retrieve -r metadata -u <alias name> -k <package.xml location>
- -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
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
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.
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.
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 :
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.
I wish I had THUMBS UP feature in here 🙂
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
Hi,
Thanks, its http://www.hostgray.com
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!
Thank you for such kind words.