Skip to main content

Master Salesforce CLI: Streamline Development, Deployments & Org Management

In plain words: The Salesforce Command Line Interface (Salesforce CLI / SFDX) is a powerful developer tool that lets you manage orgs, deploy code, run automated tests, and handle source control directly from your terminal. Instead of clicking through manual setup menus in the browser, Salesforce CLI allows you to execute deployment scripts, spin up temporary scratch orgs, and integrate seamlessly into CI/CD pipelines.

Modern enterprise Salesforce development has shifted away from traditional browser-based development toward source-driven workflows. The Salesforce CLI serves as the underlying engine powering this transition. Whether you are building Lightning Web Components locally, deploying metadata via Git, or running automated unit tests on scratch orgs, mastering CLI commands is essential for modern Salesforce engineers.

1. Getting Started: Installation & Org Authentication

Before running commands, you must install the global CLI package and connect your workspace to a target Salesforce environment.

  • Installation: Install the modern npm package globally or download the standalone installer for Windows, macOS, or Linux.
  • Web Login: Connect your developer edition, sandbox, or production org using browser-based OAuth authentication:
    sf org login web --alias MyDevOrg --set-default
360 Salesforce CLI Command Summary Card:
  • Modern Command Syntax: Uses the sf binary (superseding legacy sfdx commands).
  • Org Authorization: sf org login web
  • Source Deployment: sf project deploy start
  • Source Retrieval: sf project retrieve start
  • Scratch Org Creation: sf org create scratch

2. Core Development & Deployment Workflows

Salesforce CLI simplifies everyday developer tasks through intuitive project commands:

  • Source Deployments: Push local metadata changes directly to your connected sandbox or scratch org:
    sf project deploy start --target-org MyDevOrg
  • Metadata Retrieval: Pull updated configuration files from your org back into your local IDE workspace:
    sf project retrieve start --target-org MyDevOrg
  • Executing Anonymous Apex: Run diagnostic scripts or test logic instantly:
    sf apex run --file scripts/apex/hello.apex --target-org MyDevOrg
  • Running Unit Tests: Execute local Apex tests and view code coverage reports directly in your console:
    sf apex test run --code-coverage --result-format human --target-org MyDevOrg

3. Advanced Features: Scratch Orgs & CI/CD Pipelines

Advanced Workflow: Spin Up and Configure a Temporary Scratch Org
Scratch orgs provide completely clean, configurable Salesforce environments configured via project definition files (project-scratch-def.json).
# 1. Create a temporary scratch org valid for 7 days
sf org create scratch --definition-file config/project-scratch-def.json --alias MyScratchOrg --set-default --duration-days 7

# 2. Push local source code into the new scratch org
sf project deploy start

# 3. Open the scratch org browser window
sf org open --target-org MyScratchOrg

Furthermore, Salesforce CLI integrates natively with modern Continuous Integration and Continuous Deployment (CI/CD) pipelines such as GitHub Actions, GitLab CI, and Jenkins, enabling automated testing and zero-touch deployments.

4. Common Developer Traps & Best Practices

Syntax Trap: Mixing Up Legacy sfdx and Modern sf Commands
Salesforce has modernized its command structure from sfdx force:source:push to the streamlined sf project deploy start syntax. Using outdated legacy syntax in modern CI/CD scripts can cause deprecation errors. Always reference current Salesforce CLI documentation.
Core Rule: Authorize your default target org explicitly using alias flags, maintain a clean local project structure mapped to Salesforce DX source formats, and automate repetitive deployments using CLI scripts.
  • Use Descriptive Aliases: Always assign clear aliases (--alias) when authenticating orgs so you never accidentally deploy code to production.
  • Leverage Version Control: Commit your local source files and configuration definitions into Git to maintain a reliable source of truth across your team.
  • Keep Plugins Updated: Periodically update your core CLI tool and plugins using sf update to ensure access to the latest platform features.

Summary

The Salesforce CLI is an indispensable tool for modern developers and administrators. By mastering commands for source deployments, scratch org management, automated unit testing, and pipeline integration, you can drastically reduce manual deployment errors, speed up release cycles, and deliver enterprise-grade Salesforce applications efficiently.