Salesforce DX: Useful CLI Commands
Tags: salesforce, salesforcedx, trailhead, apex
Published: 2017-10-03 13:13:21

---
title: "Salesforce DX: Useful CLI Commands"
published: true
description: 
tags: Salesforce, Salesforce DX, Trailhead, Apex 
---

Salesforce DX is finally available and it is amazing! Now professional developers can build collaboratively with continuous delivery using <a href="https://developer.salesforce.com/platform/dx" target="_blank">Salesforce DX</a>, the open and integrated experience that makes development on the Salesforce Platform easy.

I strongly recommend you to watch this presentation, made by <a href="https://twitter.com/wadewegner" target="_blank">Wade Wegner</a>, with an introduction to Salesforce DX, talking about principles of Modern Software Delivery, how DX will improve the Developer Experience, some patterns and anti-patterns and how to adopt Salesforce DX:

{% youtube exZ3TICOzd8 %}

Well, once you decide to switch, you'll notice that you can use the <a href="https://developer.salesforce.com/tools/sfdxcli" target="_blank">Salesforce command-line interface (CLI)</a> for most Salesforce DX tasks. These tasks include authorizing a Dev Hub org, creating a scratch org, synchronizing source code between your scratch orgs and VCS, and running tests.
You can start using the CLI right after you install it.

The CLI commands are grouped into top-level topics. For example, the force top-level topic is divided into topics that group commands by functionality, such as the force:org commands to manage your orgs.

Run ---help at each level to get more information.

<code>sfdx --help                   // lists all top-level topics</code>
<code>sfdx force --help             // lists all the topics under force</code>
<code>sfdx force:org --help         // lists all the commands in the topic force:org</code>
<code>sfdx force:org:open --help    // detailed info about the force:org:open command</code>

Imagine you want to login to your Hub Org:

<code>sfdx force:auth:web:login --setalias my-hub-org --instanceurl https://login.salesforce.com</code>

Imagine now that you want to login to your sandbox:

<code>sfdx force:auth:web:login --setalias my-sandbox --instanceurl https://test.salesforce.com</code>

Maybe you want to get a list to all orgs you're connected to: 

<code>sfdx force:org:list</code>

<img src="http://matheusgoncalves.com/wp-content/uploads/2017/08/Screen-Shot-2017-08-30-at-12.30.40-PM.png" alt="" width="738" height="142" class="aligncenter size-full wp-image-596" />

To open one of these orgs:

<code>sfdx force:org:open -u my-sandbox</code>

To mark a scratch org for deletion: 

<code>sfdx force:org:delete -u my-sandbox</code>

<hr />

To <strong>remove an old hub org</strong>, or non-scratch org from Salesforce DX org list, you need to manually cleanup the DX configs in the local installation. It would be nice for this to be something in the actual CLI commands: the ability to clean up old orgs that are no longer needed. For the time being, use the following steps:

On <strong>Mac OS and Linux</strong>, you can find your DX config folder, called .sfdx, in your user home directory.

<code>cd ~/.sfdx</code>

On <strong>Windows</strong>, you can use 

<code>%USERPROFILE%\.sfdx</code>

In that folder there are a host of .json files, named for the username of the admin user you registered for that org. In my case, in this instance it was called mgoncalves@dx.pilot, so sure enough, there it was:

<code>mgoncalves@dx.pilot.json</code>

Inside the file, or the hashes for current access token, refresh token, and all the other OAuth goodness that allows DX to access your org. So I simply deleted that file:

<code>> rm mgoncalves@dx.pilot.json</code>

That appears to have <strong>cleaned it all up</strong>.

<hr />

To retrieve unpackaged Source Defined in a package.xml file (eg.: the file created by <a href="http://mavensmate.com/" target="_blank">MavensMate</a>):

<code>sfdx force:mdapi:retrieve -r ./mdapipkg -u [your_username] -k ./package.xml</code>

To retrieve an <a href="https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_ws_retrieve_unman_pack.htm" target="_blank">unmanaged package you want to convert to Salesforce DX</a>: 

<code>sfdx force:mdapi:retrieve -s -r ./mdapipkg -u [your_username] -p <package name></code>

To convert the retrieved source Defined in a package.xml file: the first step is to unzip <strong>unpackaged.zip</strong> using a zip file management utility or from the command line. For example, on a Mac, double-click unpackaged.zip in Finder or type the following command on the command line:

<code>unzip ./mdapipkg/unpackaged.zip -d ./mdapipkg/</code>

Then convert the source code to the Salesforce DX project structure:

<code>sfdx force:mdapi:convert -r ./mdapipkg</code>

To create a scratch org, first <a href="https://developer.salesforce.com/docs/atlas.en-us.sfdx_dev.meta/sfdx_dev/sfdx_dev_scratch_orgs_def_file.htm" target="_blank">create a JSON file</a> with the or configuration. Then:

<code>sfdx force:org:create -f project-scratch-def.json -a MyScratchOrg</code>

To push changes, use a <strong>Force Push</strong>: 

<img src="http://matheusgoncalves.com/wp-content/uploads/2017/08/force-push-yoda.gif" alt="" width="500" height="200" class="aligncenter size-full wp-image-608" /> 

I mean, use this: 

<code>sfdx force:source:push -u MyScratchOrg</code>

To push changes ignoring errors (<em>with great powers, great responsibilities. Be careful</em>): 

<code>sfdx force:source:push -u MyScratchOrg --ignorewarnings --forceoverwrite</code>

Salesforce DX CLI is a powerful command line interface, a tool that simplifies development and build automation when working with your Salesforce org.

For even more information, run this command to view all available commands in the force topic.

<code>sfdx force:doc:commands:list</code>

{% gist https://gist.github.com/toadgeek/cdfbef4a96531f30b1ee9d10a5791755 %}

See you next time.

_This article was originally posted at <a href="http://matheusgoncalves.com/salesforce-dx-useful-cli-commands/" target="_blank">matheusgoncalves.com</a>_