Sub topic 6
Aliases and the default target org
This chapter assumes you can use a terminal, have installed the sf CLI, and have already authorized at least one org with sf org login web. You should also know the topic, command, and flag structure of sf commands.
Why aliases and defaults exist
Most sf commands act on an org. An org is identified by its username, such as [email protected], which is long and easy to mistype. The CLI gives you two ways to shorten this:
- An alias is a short name that points to a username.
- The default target org is the org a command uses when you do not name one.
Together they let you write sf data query --query "..." instead of repeating a username in every command.
Create an alias while logging in
The --alias flag saves the alias during authorization. The --set-default flag also makes the org your default target org.
sf org login web --alias myorg --set-default
sf org listsf org list shows every org the CLI knows about. The alias appears in its own column, and the default org is marked.
You can manage aliases separately from login. sf alias set creates or changes one, and sf alias list shows them all.
sf alias set [email protected]
sf alias listSet the default target org
The default target org is a configuration value named target-org. You can set it directly:
sf config set target-org myorg
sf org displaysf org display prints details for the current default org, including its username, org ID, and instance URL. Use it to confirm which org your commands will hit.
You can set target-org to an alias or a username. An alias is easier to read and to change later.
Local and global configuration
The CLI stores configuration at two levels:
| Level | Where it applies | Stored in |
|---|---|---|
| Local | Only inside one project directory | `.sf/config.json` in the project |
| Global | Every directory on your machine | `~/.sf/config.json` |
sf config set writes to the local level by default when you run it inside a project. Add --global to write to the global level. Inside a project, a local value overrides a global one.
sf config set target-org myorg --global
sf config list
sf config get target-orgsf config list shows each value and whether it came from the local or global file.
Override the default for one command
Every org-aware command accepts --target-org, or -o for short. It wins over any configured default for that one command.
sf data query --query "SELECT Id, Name FROM Account LIMIT 5" --target-org prodThe following diagram shows how the CLI picks an org.
Common errors
- "No default environment found" or a similar message. No
target-orgis set at any level. Runsf config set target-org <alias>or pass--target-org. - Commands hit the wrong org. A local value in a project is overriding your global one. Run
sf config listand check the location column. - "No authorization information found" for an alias. The alias points to a username that is no longer authorized, for example after logging out. Run
sf org listto see what is currently available, then log in again. - Alias works in one project but not another. Aliases are stored globally. The default target org is what varies by project, so check the config rather than the alias.
Related default: the Dev Hub
Scratch org commands use a separate setting, target-dev-hub, to find your Dev Hub. It works the same way as target-org, including local and global levels and the --target-dev-hub flag override.
sf config set target-dev-hub mydevhubExercise
You run sf config set target-org dev --global. Inside a project, .sf/config.json already sets target-org to qa. You then run sf data query --query "SELECT Id FROM Account LIMIT 1" in that project. Which org is queried, and how would you query dev instead without changing any configuration?
Show answer
The qa org is queried, because a local value overrides a global one. To query dev without changing configuration, add the flag --target-org dev (or -o dev) to the command.