Use Metadata API or Package.xml with Salesforce DX

Use Metadata API 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

Posted

in

by


Related Posts

Comments

5 responses to “Use Metadata API with Salesforce DX”

  1. Manjunadh Manubolu Avatar
    Manjunadh Manubolu

    great job Jitendra bro. I work at Salesforce but I used to follow your blog for any quick reference. 😉 Thanks for all your good work.!!

  2. Sandeep Avatar
    Sandeep

    Hi Jitendra,

    Thanks for the blog. Can you please explain that how would we deploy our changes to sandbox/dev org. When I am using the command sfdx force:mdapi:deploy -c -d ../mdAPIZip/unpackaged -u jzaa1 -w 10 , I am not getting any errors, but my changes are not being reflected to the target org.

    Thanks in advance

    1. Alejandro Avatar
      Alejandro

      this command have the -c flag which means that the package would be validated only, so, if you remove from the command the package should be deployed in the org that you specify

      Regards

  3. Shailendra Singh Avatar
    Shailendra Singh

    Thanks Jitendra. Very well explained.

  4. Shubham Sarkate Avatar
    Shubham Sarkate

    Hello sir,
    I am trying to write a script to automate creation and configuration of lightning communities using experienceBundle metadata api.
    any guidance will be highly appreciated.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Discover more from Jitendra Zaa

Subscribe now to keep reading and get access to the full archive.

Continue Reading