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” 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 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 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/
      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/ -d ./mdapipkg/. Please not that this will not work on windows command prompt and you will have to manually extract
  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 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.

Introduction to SalesForce Developer eXperience (SFDX)

Salesforce DX (SFDX) provides an integrated, end-to-end lifecycle designed for high-performance agile development.  It consists set of tools including Salesforce CLI, Second-Generation Packaging (2GP), Scratch Orgs,  VCS and CI support.

  1. Salesforce CLI

    The Salesforce CLI is a command line interface that simplifies development and build automation when working with Salesforce org. It can be used to;

    • Aggregate all the tools you need to develop with and perform commands against your Salesforce org
    • Synchronize source to and from scratch orgs
    • Create and manage orgs
    • Import and export data
    • Create and execute tests
    • Create and install packages
  2. Second-Generation Packaging (2GP)

    2GP allows create packages in a source-driven development environment. It can be used to create and deploy packages in own Salesforce org or to develop and distribute packages to customers. Here are some highlights of 2GP;

    • Salesforce command-line (CLI) support.
    • Options for enterprise customers to organize and deploy metadata to production orgs.Salesforce DX Logo
    • Multiple packages per namespace
    • Feature branch development and testing.
    • Fully API-driven functionality.
    • Packages that are source-driven, not org-based.
    • Ease of development and distribution of dependent packages.
  3. Scratch Orgs

    The scratch org is a source-driven and disposable deployment of Salesforce code and metadata. A scratch org is fully configurable, allowing developers to emulate different Salesforce editions with different features and preferences. And the scratch org configuration file can be shared with other team members.

String Manipulation in Salesforce #1

Salesforce provide set of methods for String manipulation. This article explains usage of each function with examples.


  1. Truncate strings with ellipses.
    abbreviate(maxWidth) returns a trimmed version of the String, of the specified length and with ellipses (…) appended if the current String is longer than the specified length; otherwise, returns the original String without ellipses.

    'Test Text Field'.abbreviate(8); //Test ...
  2. Convert to title case (Capitalize first letter)
    capitalize() capitalize first letter of the string.

    'test text field'.abbreviate(8); //Test text field
  3. Pad with Spaces (front & back)
    center(size) pads string with spaces in front and back while keeping String in the center.

    'test'.center(10); //    test
  4. Pad with Text (front & back)
    center(size, paddingString) pads string with specified text in front and back while keeping String in the center.

    'test'.center(10,'#'); //###test###
  5. Remove leading and trailing spaces
    trim() removes leading and trailing white spaces from the string.

    '   test    '.trim(); //test
  6. Escape characters.
    escapeCsv() – columns in CSV string enclosed with double quotes.
    escapeHtml3(), escapeHtml4() – escapes html characters in String.
    escapeJava() – escapes characters in String with Java rules. Characters escaped include quotes and control characters, such as tab, backslash, and carriage return characters.
    escapeSingleQuotes(stringToEscape) – escapes single quotes in String with backslash.
    escapeUnicode() – escapes Unicode characters.
    escapeXml() – escapes Xml tags in String.
  7. Convert first letter to lowercase
    uncapitalize() returns the String with the first letter in lowercase.

    'Test tTest Test'.uncapitalize(); //test tTest Test
  8. Convert to uppercase
    toUpperCase() Converts all of the characters in the String to uppercase using the rules of the default (English US) locale.

    'Test'.<code>toUpperCase</code>(); //TEST
  9. Convert to lowercase
    toUpperCase() Converts all of the characters in the String to uppercase using the rules of the default (English US) locale.

    'TesT'.<code>toLowerCase</code>(); //test
  10. Remove HTML tags
    stripHtmlTags() Removes all html tags from the String.