Sub topic 1
What the Salesforce CLI does
The Salesforce CLI is a program you run in a terminal to work with Salesforce orgs. Its command is sf. You use it to log in to an org, move metadata between your computer and the org, query records, run Apex, and create temporary orgs for development. Because every action is a typed command, you can save commands in scripts and run them again, including on a build server.
This chapter explains what the CLI is, what it does on your behalf, and how it fits with the other Salesforce tools. Later chapters cover installing it and running the commands in detail.
Prerequisites
This chapter assumes you already have the following:
- Basic comfort with a terminal. You can open one, change directories, and run a command.
- A Salesforce org you can log in to, such as a free Developer Edition org.
- Familiarity with Salesforce concepts: what an org is, what metadata is (objects, fields, Apex classes, page layouts), and what Apex and SOQL are.
- Node.js installed, if you plan to install the CLI with npm in the next chapter.
You do not need the CLI installed to read this chapter. The commands shown here are previews, and each is explained when you use it later.
What the CLI is
The CLI is a client for Salesforce's APIs. It does not contain your org's data or metadata. When you run a command, the CLI reads your local files and settings, calls one or more Salesforce APIs over HTTPS, and prints the result.
It is built on oclif, an open-source framework for command-line tools. Most of its features come from plugins, which are packages of commands that load into the base program. This is why the same sf program can have commands for deployment, data, Apex, and packaging, and why you can add more later.
Three properties explain most of how it behaves:
- It acts on an org you name. Almost every command needs a target org. You either pass one with a flag or rely on a saved default.
- It works on local files. Deployment and retrieval read from and write to a project folder on your machine. The project folder can be tracked in version control, such as Git.
- It can print machine-readable output. Adding
--jsonto a command returns structured JSON. Scripts can parse it, and so can other tools.
What you can do with it
The commands fall into a few groups. Each group is a topic, and the table shows one example command from each.
| Task | Example command |
|---|---|
| Log in to an org | `sf org login web --alias myorg` |
| Create a project folder | `sf project generate --name myproject` |
| Send local metadata to an org | `sf project deploy start --source-dir force-app` |
| Pull org metadata to local files | `sf project retrieve start --source-dir force-app` |
| Create a temporary org | `sf org create scratch --definition-file config/project-scratch-def.json` |
| Query records | `sf data query --query "SELECT Id, Name FROM Account LIMIT 5"` |
| Run Apex code | `sf apex run --file hello.apex` |
| Run Apex tests | `sf apex run test --test-level RunLocalTests` |
You do not need to memorize these. The point for now is that the structure repeats: a topic (org, project, data, apex), sometimes a subtopic, a command, and flags. The next chapters cover that structure.
How a command reaches your org
A single CLI command can call several Salesforce APIs. The CLI picks the right one for the job, so you do not choose between them.
- Deploy and retrieve commands use the Metadata API, or the newer REST-based deploy path where available, to move metadata.
- Data commands use the REST API, or the Bulk API for large operations.
- Apex commands use the Tooling API to run anonymous Apex and the REST API to start tests and fetch results.
- Login commands use OAuth, the standard protocol for letting an application act on your behalf without handling your password.
The diagram shows the path a command takes.
After you log in, the CLI saves authorization details on your computer. This is how later commands run without asking you to sign in again. Those stored credentials give access to your org, so treat your user account on the machine as part of your security boundary. On a shared computer, log out of orgs you no longer need.
Where the CLI fits among Salesforce tools
Salesforce offers several ways to do the same work. Knowing the differences helps you decide when the CLI is the right choice.
| Tool | Best for | Limitation |
|---|---|---|
| Setup UI in the browser | One-off configuration, exploring features | Not repeatable, not easy to review or version |
| Developer Console | Quick Apex edits and debugging logs | One org at a time, no local project |
| VS Code with Salesforce Extensions | Daily coding with editor support | Runs CLI commands underneath, so it depends on the CLI |
| Salesforce CLI | Repeatable tasks, automation, CI | You type commands and read text output |
The VS Code extensions rely on the CLI. When you click "Deploy This Source to Org" in the editor, the extension runs a CLI command. A CLI problem, such as an old version or a missing default org, can therefore surface as an editor error. Knowing the CLI helps you diagnose those errors.
The move from sfdx to sf
Salesforce's earlier command was sfdx. Its commands used colons, such as sfdx force:source:deploy. The current command is sf, which uses spaces and a cleaner topic layout, such as sf project deploy start.
You will meet both when you read older blog posts, scripts, and team documentation. The practical points are:
- The
sfcommand is the one to learn. This tutorial uses it throughout. - Some old
sfdxcommands still work in a compatibility mode, but they are on a retirement path. Do not write new scripts against them. - A command found online may not map one-to-one to the new name. Search the current documentation or run the built-in help to find the replacement.
- Flag names also changed in places. A command copied from an old example can fail with an unknown flag error even when the topic name is right.
Why use a CLI at all
For a single change, the browser is usually faster. The CLI pays off when work needs to be repeated, shared, or checked.
- Repeatability. A command gives the same result each time. A checklist of browser clicks does not.
- Version control. Metadata stored as files in a project can be diffed, reviewed in a pull request, and rolled back.
- Environments on demand. You can create and delete scratch orgs from a script, so each feature branch gets a clean org.
- Automation. Continuous integration (CI) systems run the same commands on every push: deploy, run tests, report results.
- Speed on larger tasks. Deploying hundreds of components or loading thousands of records is simpler with one command than through the UI.
A typical workflow looks like this:
- Log in to an org and save it under an alias.
- Create or open a project.
- Edit metadata files locally.
- Deploy to a development or scratch org and run tests.
- Retrieve changes made in the org, if any, and commit everything to Git.
Common misunderstandings
Several mistaken assumptions cause most beginner problems.
- "The CLI is the org." It is not. Your local files and the org's metadata are two separate copies, and commands exist to sync them. If someone edits a component in Setup, your local copy is out of date until you retrieve it.
- "Deploying always overwrites safely." A deploy sends your local version to the org, and the org version is replaced for those components. If a teammate changed the same component in the org, their change can be lost.
- "Any command runs against the right org." If you rely on a default org and forget which one it is, you can run a command against the wrong org, including production. Check the target before you deploy. You will see how in the chapter on defaults.
- "Version problems are rare." The CLI updates often. Outdated versions can lack commands or flags that current documentation shows.
You can try a safe first command once the CLI is installed:
sf --helpThis prints the list of topics. It touches no org, so it is a harmless way to see what the CLI offers.
What comes next
The next chapters follow the path of a first working session. You install the CLI and verify it, learn how commands are structured, log in to an org, set a default, create a project, deploy and retrieve metadata, and then try scratch orgs, queries, Apex, JSON output, and updates. Each step builds on the previous one, because later commands depend on having an authorized org and a default target set.
Exercise
Your teammate runs sf project deploy start --source-dir force-app and gets a successful result, but the change does not appear in the org you are looking at in your browser. Name two likely causes that follow from how the CLI works.
Show answer
First, the command acted on a different target org than the one you are viewing, for example because the default target org points elsewhere. Second, the deploy used the teammate's local files, so if those files did not contain the change, nothing new was sent. The CLI only syncs local files to whichever org it targets. It does not know which org you have open in a browser.
Exercise
A blog post tells you to run sfdx force:source:deploy. What should you check before using it, and which style of command should you prefer?
Show answer
Check whether the command is the older sfdx form, which is on a retirement path, and look up the current equivalent. Prefer the sf form, which uses spaces between topic and command, such as sf project deploy start. Also check the flags, because some names changed.