Sub topic 10
Opening, inspecting, and logging in to the scratch org
This chapter assumes you have created a scratch org with the alias my-scratch, as in the previous chapter. You should be comfortable running sf commands in a terminal. Scratch orgs have no password at creation. The CLI holds an authorization for the org, and you use it to log in.
Open the org in a browser
sf org open asks the org for a one-time login URL and opens it in your default browser. You are logged in as the scratch org's admin user, and no password is needed.
sf org open --target-org my-scratchYou can open a specific page with --path. This is useful when you want to land directly in Setup.
sf org open --target-org my-scratch --path lightning/setup/SetupOneHome/homeIf you are on a remote machine or don't want a browser to launch, add --url-only. The command prints the URL instead.
sf org open --target-org my-scratch --url-onlyThe printed URL contains a session token. Treat it like a password and don't paste it into chat or tickets.
Inspect the org
sf org display shows the details the CLI stores for the org.
sf org display --target-org my-scratchThe output includes these fields:
| Field | Meaning |
|---|---|
| Username | The generated admin username, usually ending in a random domain |
| Org Id | The 18-character ID of the scratch org |
| Instance Url | The server address of the org |
| Dev Hub Id | The org that created and tracks this scratch org |
| Expiration Date | When the org will be deleted automatically |
| Status | `Active` while the org is usable |
Check the expiration date before a long task. An expired org cannot be recovered.
List every org the CLI knows
sf org listThis prints your Dev Hub and scratch orgs. The default org is marked, and expired scratch orgs are flagged. Use sf org list --clean to remove the entries for orgs that no longer exist. This only cleans up local records. It does not delete anything in Salesforce.
Log in with a password
Sometimes you need a username and password, for example to connect an external tool. The scratch org definition in this tutorial enables the EnableSetPasswordInApi feature, which allows the CLI to set one.
sf org generate password --target-org my-scratch
sf org display --target-org my-scratch --verboseThe first command sets a random password for the admin user. The second command, with --verbose, shows it. The password is stored in the CLI's local files.
Common errors
- "No authorization information found": the alias is wrong, or the org was created on another machine. Run
sf org listand check the alias. - The browser opens a login page: the one-time URL expired or was already used. Run
sf org openagain. - The password command fails: the org was created without
EnableSetPasswordInApiin its definition file.
Exercise
You need the login URL for my-scratch on a server with no browser, and you want to know when the org expires. Which two commands do you run?
Show answer
Run sf org open --target-org my-scratch --url-only to print the login URL. Run sf org display --target-org my-scratch to see the Expiration Date.