Sub topic 1
What the Salesforce CLI does
Prerequisites
This chapter assumes you have the following:
- Basic comfort using a terminal.
- A Salesforce org you can log in to, such as a free Developer Edition org.
- Familiarity with Salesforce concepts such as orgs, metadata, and Apex.
- Node.js installed, only if you plan to install the CLI with npm in the next chapter.
You don't need to run any commands yet. This chapter explains what the tool is for, so the later commands make sense.
What the CLI is
The Salesforce CLI is a command-line program. You run it by typing sf in a terminal. It sends requests to Salesforce on your behalf and prints the results as text.
Anything you type in the terminal can be saved in a script, run on a schedule, or run by a build server. This is the main reason developers use the CLI instead of clicking through Setup.
What you use it for
Most CLI work falls into a few groups. Each group is a topic, which is the first word after sf.
| Task | What the CLI does | Topic |
|---|---|---|
| Connect to orgs | Authorizes an org and stores a login for later commands | `org` |
| Manage a project | Creates a Salesforce DX project on your computer | `project` |
| Move metadata | Deploys local source to an org, or retrieves it back | `project` |
| Work with test orgs | Creates temporary scratch orgs from a definition file | `org` |
| Work with records | Runs SOQL queries against an org | `data` |
| Run code | Runs anonymous Apex and Apex tests | `apex` |
Later chapters cover each row in turn. For now, notice that every task is a command you can type, repeat, and automate.
How it fits into a workflow
The CLI sits between the files on your computer and the org. Your project folder holds metadata as source files. The CLI sends those files to an org (deploy) or pulls the org's version back (retrieve). It can also query records and run Apex in the same org.
Because the project is made of plain files, you can keep it in version control such as Git. The org is then one place where that source runs, not the only copy of your work.
sf and the older sfdx commands
The current command is sf. Older tutorials and scripts use sfdx, whose commands joined words with colons, as in sfdx force:org:list. The sf command uses spaces instead, as in sf org list. If you find an sfdx example online, expect to translate it. The next chapters teach only the sf form.
What the CLI does not do
The CLI does not replace Salesforce itself. It only calls it. Two points often confuse newcomers:
- Commands that deploy, query, or run Apex act on a real org, not on a simulation. Check which org you are targeting before you deploy.
- Your local project is not the org. Changes you make in Setup do not appear in your local files until you retrieve them.
Why this matters
A repeatable command beats a remembered series of clicks. Teams use the CLI to give every developer the same setup steps. They also use it to run the same tests before every release, and to create a fresh scratch org when they need one. Each of those is a short list of commands that can be checked into the project and run again.
Exercise
A teammate says, "I changed a field in Setup, so my local project already has the change." Is that correct? What would you use the CLI for to fix it?
Show answer
No. The local project and the org are separate copies. A change made in Setup exists only in the org. You would retrieve the metadata from the org into the project with the CLI, using sf project retrieve start. Later chapters cover this command.