Use Metadata API with Salesforce DX

Using old project format from ANT Migration toolkit or Force.com IDE ( package.xml ) in Salesforce DX without enabling Developer Hub

Use Metadata API or Package.xml with Salesforce DX

If you have existing VCS which stores metadata information about your Salesforce instance in old format, dependent on package.xml and you want to use Salesforce DX then this blog post is for you. As you might already know, Salesforce DX does not need package.xml and can automatically detect changes in your Salesforce instance, therefore new Salesforce DX project format is different. In this blog post we will see, how Salesforce DX can be used without enabling Developer Hub and using Salesforce DX with normal Salesforce instances.

There are two solution to this problem

  1. Keep using old Project format and use Metadata API to retrieve and deploy
  2. Convert old project format to Salesforce DX project Format

First step is to authenticate your normal Salesforce org. For this, you don’t need to purchase license for Salesforce DX. You can refer this blog post for common Salesforce DX commands. First command we would use is login command.

sfdx force:auth:web:login --setalias jzaa1

Above command will open browser window to authenticate Salesforce instance. This authenticated Salesforce instance can be referred as jzaa1 in other Salesforce DX commands.

Use Salesforce DX with Metadata API to retrieve components

Below command is used to retrieve component from Salesforce using metadata API by Salesforce DX

sfdx force:mdapi:retrieve -r ./mdAPIZip -u jzaa1 -k src/package.xml

where

  • -u : Which Salesforce Org to be used by Salesforce DX
  • -r : Where zip file should be saved
  • -k : List of components to be retrieved using package.xml
Use Salesforce DX with Metadata API to deploy components

Below command can be used to deploy component in regular Salesforce instance where Developer Hub is not enabled using Metadata API and Salesforce DX

sfdx force:mdapi:deploy -c -f ../mdAPIZip/unpackaged.zip -u jzaa1 -w 10
or
sfdx force:mdapi:deploy -c -d ../mdAPIZip/unpackaged -u jzaa1 -w 10

where

  • -u : Which Salesforce Org to be used by Salesforce DX
  • -f : Zip file location containing metadata and package.xml
  • -c : Check only flag, Package would be validates only
  • -w : Wait time in minute for operation to be completed
  • -d : Folder location of non-zipped files, with package.xml in root
Convert old project structure to Salesforce DX format

Lets see, how old project created using ANT Migration toolkit or Force.com IDE can be converted to Salesforce DX format

First we need to create a blank Salesforce DX project with the help of below command, where -n is name of Salesforce Dx project

sfdx force:project:create -n MdAPIDemo

Now, navigate to MdAPIDemo folder

cd MdAPIDemo

Its time to run last command

sfdx force:mdapi:convert -r ../mdAPIZip/unpackaged

Where

  • -r : root directory, containing metadata API folder

Related posts