본문 바로가기

개발자정보

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.

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

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

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

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

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

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.

The validation and deployment was a success

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.

References

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

반응형