Sub topic 7
Authorizing an Org and Setting the Default
This chapter assumes you have installed VS Code, the Salesforce CLI (sf), a Java JDK, and the Salesforce Extension Pack. It also assumes you have created a Salesforce DX project, as in the previous chapter. You also need a Developer Edition org or a sandbox that you are allowed to use for development.
Why authorization comes first
A DX project only holds files on your disk. To deploy source, run tests, or collect debug logs, the tooling must talk to an org. Authorization is the step that lets the Salesforce CLI act on your behalf in that org. After you authorize, the CLI stores a token locally and refers to the org by its username or an alias that you choose.
The VS Code extensions do not keep their own credentials. They call the same CLI, so an org you authorize in the terminal shows up in the editor, and the reverse is also true.
Authorize an org from the command line
Run this from the project folder:
sf org login web --alias my-org --set-default
sf org listThe first command opens a browser window. Log in with your org username and password, then approve the access request. The CLI then reports that it authorized the org.
--alias my-orggives the org a short name. You can use the alias anywhere the CLI asks for a target org.--set-defaultmakes this org the default target for the current project.sf org listshows every org the CLI knows about, with its alias, username, and connection status.
If you want to log in to a sandbox, add --instance-url https://test.salesforce.com. The sfdcLoginUrl value in sfdx-project.json is also used as the login URL when you do not pass one, which is why you can leave this flag off for production and Developer Edition orgs.
Authorize an org from VS Code
Open the Command Palette and run one of these commands:
- SFDX: Authorize an Org logs in to a production or Developer Edition org.
- SFDX: Authorize an Org using Session ID and SFDX: Authorize a Dev Hub are alternatives for other cases.
- SFDX: Authorize an Org asks you to pick a login URL (production, sandbox, or custom) and then asks for an alias.
The editor runs the same sf org login web command and opens the same browser flow.
Default orgs and the status bar
The status bar at the bottom of VS Code shows the current default org. Click it, or run SFDX: Set a Default Org, to switch to another authorized org. The choice is saved in your project, in a .sf/config.json file, so it applies to this project only and not to your other projects.
You can read and change the same setting in the terminal:
sf config get target-org
sf config set target-org my-orgEvery deploy, retrieve, and test command in later chapters uses target-org unless you override it with --target-org.
Common problems
| Symptom | Likely cause | Fix |
|---|---|---|
| The status bar shows "No Default Org Set" | No org was authorized, or no default was chosen | Run **SFDX: Set a Default Org** |
| A command fails with an error about no authorization information | The alias or username does not match an authorized org | Run `sf org list` and check the name |
| The org shows as expired or disconnected | The token was revoked, or a scratch org expired | Authorize the org again with `sf org login web` |
| You log in to the wrong org | The browser already has a session for a different org | Log out of Salesforce in the browser, or use a private window |
Check your work
After you authorize, run a command that needs the org, such as listing its limits:
sf org display --target-org my-orgThe output shows the username, org ID, instance URL, and connection status. If it prints these values, the project can reach the org and you are ready to deploy.
Exercise
You ran sf org login web --alias my-org without --set-default. The status bar shows no default org. Name two ways to fix this without logging in again.
Show answer
Run the SFDX: Set a Default Org command in VS Code and choose my-org, or run sf config set target-org my-org in the terminal. Both write the default to the project's configuration, so no new login is needed.
Exercise
Why does an org that you authorized in the terminal also appear in VS Code?
Show answer
The extensions use the Salesforce CLI and read the same stored authorizations, so they do not need a separate login.