Using SalesforceDX (SFDX) with non Scratch Orgs

Author posted by Jitendra on Posted on under category Categories Salesforce and tagged as Tags , with 10 Comments on Using SalesforceDX (SFDX) with non Scratch Orgs

How to use SalesforceDX (SFDX) with Sandbox, Developer and Production Orgs

Use SFDX with non scratch Orgs

We have gone through few blog posts about SFDX and its capabilities in past. In this post, I will explain steps on how to use SFDX with developer, Sandbox or Production Org. In short we will discuss how to use SFDX with non Scratch Orgs. We would be using official Salesforce IDE for SFDX, which is VSCode.

Assumption – VSCode extension is already installed in your VSCode.

  1. Create SFDX project in VSCode

Open VScode and press cmd+Shift+p, It will give some option. Select SFDX: Create Project.

SFDX : Create Project
SFDX : Create Project

Next screen will ask for project name and then location where to save.

SFDX Project Name
SFDX Project Name

Now, its time to know some basic SFDX commands. You can refer this post for SFDX crash course.

Using SFDX with Sandboxes

If you want to use SFDX with Sandbox, then open file sfdx-project.json in VScode and change sfdcLoginUrl to https://test.salesforce.com or my domain URL if SSO is configured.

2. Authenticate Developer Account with SFDX

Run SFDX command in VSCode
Run SFDX command in VSCode

Before authenticating new org, its always good idea to check if its already authenticated using below command

sfdx force:org:list

if not, then run this command to authenticate Salesforce developer org

sfdx force:auth:web:login --setdefaultdevhubusername --setalias my-devhub-org

3. Create package.xml
below is sample package.xml, It contains Lightning Component and Flow

4. Retrieve Metadata

I have created metadata folder in my project where package.xml exists. By running below command, I get zipped file of metadata retrieved.

sfdx force:mdapi:retrieve -r metadata -u jit11 -k metadata/package.xml
  • -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

problem with above command is that, it gives zip file. You would need to unzip it every time. To save my time, What I do is, I create a command / batch file to retrieve metadata & at same time unzip it. Below is content of file to unzip metadata

running above file is straight forward in windows operating system, but in Mac, we need to let OS know that metadata_r.command file is executable file by running below command. Make sure to run this command from Terminal and not from VSCode.

chmod +x mdapi_r.command

And command / batch file can be executed in Mac using

bash filename

5. Deploy Metadata

Below command can be used to deploy component in non scratch Salesforce instances

sfdx force:mdapi:deploy -c -f ../mdAPIZip/unpackaged.zip -u jit11 -w 10
or
sfdx force:mdapi:deploy -c -d ../mdAPIZip/unpackaged -u jit11 -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

To know more about how to deploy to Developer, Sandbox or Production org (non scratch Org) using SFDX, refer this post.

Related posts

10 thoughts on “Using SalesforceDX (SFDX) with non Scratch Orgs”

  1. Great article!

    DX has a lot of power in force:source:pull/push commands since they get rid of maintaining a template file but those don’t work with non-scratch orgs.

  2. After the final step, once we retrieve the package should we use Ant to deploy? Or Has salesforce come up with deploying metadata as well to the destination org?

  3. We have created a SSL Certificate and added it to Production DevHub Org and we are able to login to DevHub Org using JWT:grant.

    We have used same SSL certificate and server.key and configured the same steps in sandbox as well. However, when we try to do JWT:grant, we are getting following error message:
    ERROR: This org appears to have a problem with its OAuth configuration. Reason: invalid_grant – audience is invalid
    username: epamadmin@XXXXX.com.devcicd,
    clientId: XXXXXXXXXXXXXXXX,
    loginUrl: https://eis1–devcicd.my.salesforce.com,
    privateKey: D:\Projects\XXXX\test\server.key.

    Could you please let us know whether we can do JWT:grant in sandbox as well. If yes, is there any other setting we require to do in order to fix this issue in sandbox.

  4. In our Dev Sandbox, we have enabled comminuties. In this sandbox we are trying to create a SFDX package having community configuration. For this package, we are trying to generate a package version.

    However, the SFDX package generation asks us to enable communities. Below is the error:

    Error: You need to enable communities to be able to install Lightning Pages of type CommPbjectPage.

    We did not enable communities in DevHub yet.

    Is Package and package version generated only from DevHub? Not from a lower sandbox?

    My question is why do I need to enable community in prod to test / generate package in DEV?

    Could you please help me on this.

Leave a Reply

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