Salesforce CLI

Salesforce CLI

What is it ?

The Salesforce CLI is command line interface for Salesforce comes under Salesforce DX tools. An introduction to Salesforce DX is given here.

What it can do ?

Salesforce CLI can perform various tasks including;

  • Creating APEX classes, running tests and view test results.
  • Import CSV files with the Bulk API.
  • Create and test Lightning component bundles.
  • View org’s limits.
  • Create and delete scratch orgs.
  • Manage users.
  • Create and manage packages (1st and 2nd generation)
  • View the metadata.
  • Create Visualforce pages and components.
  • Push and pull source to and from scratch orgs.

How to install ?

Salesforce CLI can be downloaded from here. For Windows, download and run the installer. Make sure Git is installed and PATH is set properly. You can verify installation by typing ‘sfdx –version’ in command prompt.

If you are using Windows 10, Windows Subsystem for Linux (WSL) allows you to run native Linux command-line tools on Windows 10 alongside your traditional Windows desktop. Detailed instructions can be found here.

If you have already installed, run sfdx update to get the latest version of Salesforce CLI. It is recommended to exclude installation directory (%LOCALAPPDATA%\sfdx) from antivirus software since it could slow down CLI.

Installed, Now what ?

Next step is to authorize your Salesforce org. To do that type “sfdx force:auth:web:login --setalias your-alias --instanceurl https://login.salesforce.com” in the command prompt (CMD). This will opens up Salesforce login page on web browser. Then enter login details to authorize. Once you done that cmd will display following message.

Successfully authorized your.user@yourorg.com with org ID 00D700F0050wfmJUAQ
You may now close the browser

By typing sfdx force:org:list you will able to see orgs which are already connected. To remove a connection,  navigate into %USERPROFILE%/.sfdx (C:\Users\USERNAME.sfdx) and delete your.user@yourorg.com.json file. (In linux, it will be in cd ~/.sfdx)

How to Create a Salesforce DX Project from Existing Source ?

If you have existing source and needs to convert it to Salesforce DX format, follow these steps.

  1. Create a Salesforce DX project.
    sfdx force:project:create --projectname mywork
  2. Create a directory to retrieve metadata.
    mkdir mdapipkg
  3. Use following commands to retrieve metadata.
    Format of Current Source Command
    Your source already defined as a managed package in your packaging org. sfdx force:mdapi:retrieve -s -r ./mdapipkg -u <username> -p <package name>
    You have a package.xml file that defines your unpackaged source. sfdx force:mdapi:retrieve -r ./mdapipkg -u <username> -k ./package.xml
    You don’t have your source defined in a package.
    1. Open the sandbox org that contains your metadata.
      sfdx force:org:open -u <username> --path one/one.app
      You can use the sandbox username or an alias.
    2. Open All Setup.
    3. Open Package Manager.
    4. Create a package with the name of your application.
    5. Add the metadata source to the package.
    6. In your Salesforce DX project, create a folder for the MDAPI retrieve, for example, mdapipkg.
    7. Retrieve the metadata.
      sfdx force:mdapi:retrieve -s -r ./mdapipkg -u <username> -p <package name>
  4. While you in root directory, run unzip ./mdapipkg/unpackaged.zip -d ./mdapipkg/. Please not that this will not work on windows command prompt and you will have to manually extract unpackaged.zip.
  5. Change directory to project root and run following command to convert metadata into Salesforce DX project format.
    sfdx force:mdapi:convert -r ../mdapipkg

How to create a scratch org ?

  1. Enable Dev Hub by following these steps. Please note that this feature is only available on production orgs (Enterprise and Unlimited). But you can register for a 30-day trial org from here.
  2. Set authenticated org as the default Dev Hub org for scratch org creation.
    sfdx force:auth:web:login --setdefaultdevhubusername
  3. Create scratch org definition file. Sample format can be found here.
  4. Run following command to a create scratch org.
    sfdx force:org:create -f demo-scratch-def.json -a DemoScratchOrg (note down the username).
  5. By executing sfdx force:org:open -u <username/alias> will open up scratch org on the browser.

Scratch Org Limits ?

By default, Salesforce deletes scratch orgs and their associated ActiveScratchOrg records from your Dev Hub org when a scratch org expires. A scratch org expires in 7 days unless you set a duration when you create it.

Scratch orgs have these storage limits:

  • 200 MB for data
  • 50 MB for files

How to create an apex class ?

Navigate to classes directory in Salesforce DX project and run sfdx force:apex:class:create -n myclassname. Simlarly you can use force:apex:trigger:create, force:visualforce:page:create or force:visualforce:component:create. A complete list of Salesforce CLI commands can be found here.

How to  push source to the Scratch Org ?

After changing the source, you can sync the changes to your scratch org by pushing the changed source to it. Initially it will push all metadata into scratch org and then it will start tracking code changes. – sfdx force:source:push

You can use sfdx force:source:status to track changes in the source.

How to pull changes from Scratch Org to Project?

To pull changed source from the scratch org to the project:

sfdx force:source:pull

How to create a Scratch Org user ?

Use the force:user:create command to create a user. Specify the --setalias parameter to assign a simple name to the user that you can reference in later CLI commands.

sfdx force:user:list will display list of users and sfdx force:user:display -u username@demoscratchorg.net will display user details including password.

How to run Apex tests ?

sfdx force:apex:test:run

How to deploy changes into Dev Hub ?

First step to deploy is converting Salesforce DX project into metadata format. To do that, run sfdx force:source:convert. Then execute sfdx force:mdapi:deploy -d metadataPackage_XXX to deploy. sfdx force:mdapi:deploy:report will display status of an asynchronous metadata deployment.

Leave a Reply

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