Sub topic 12
Confirming the org with a SOQL query and Apex
This chapter assumes you have a Developer Edition org, you can log in through its My Domain URL, and the Salesforce CLI (sf) is installed. You should be comfortable running commands in a terminal. The goal is to confirm three facts: which org you are connected to, what edition it is, and which user you are acting as.
Why verify the org
The CLI can hold authorizations for several orgs at once. A command that omits --target-org uses your default org, which may not be the one you meant. Checking the org name, edition and username before you deploy or load data prevents mistakes in the wrong org.
Two tools give you the answer:
- A SOQL query run through the CLI. SOQL (Salesforce Object Query Language) is a SQL-like language for reading records.
- Anonymous Apex run in the org. Apex is Salesforce's server-side programming language, and anonymous Apex is code you run once without saving it.
Step 1: Check the CLI authorization
Start by listing the orgs the CLI knows about, then open the Developer Edition org in a browser.
sf org list
sf org open --target-org my-dev-orgsf org list shows each alias, username and connection status. Confirm that my-dev-org appears and its status is connected. sf org open uses the stored authorization to log you in without a password. If it opens the wrong org, your alias points somewhere unexpected.
Step 2: Query the Organization object
Every org has exactly one record in the Organization object. It holds details about the org itself.
sf data query --target-org my-dev-org --query "SELECT Name, OrganizationType FROM Organization"The result is a table with one row. Name is the company name from the sign-up form. OrganizationType should read Developer Edition. If it shows another value, you have authorized a different org.
The flow of the check is shown below.
Step 3: Confirm the user with anonymous Apex
SOQL tells you about the org. Apex can also tell you which user is running the code. Open the Developer Console from the gear menu in the org, choose Debug, then Open Execute Anonymous Window, and run this:
System.debug('Org: ' + UserInfo.getOrganizationName());
System.debug('User: ' + UserInfo.getUserName());Tick Open Log before you click Execute. In the log, filter for USER_DEBUG to see the two lines. The username should match the one you chose at sign-up, in the form of an email address.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `No authorization information found for my-dev-org` | The alias was never saved or was mistyped | Run `sf org login web --alias my-dev-org` again |
| Query returns an expired or invalid session error | The stored token is no longer valid | Log in again with the same alias |
| `OrganizationType` is not `Developer Edition` | The command used a different org | Pass `--target-org` explicitly |
| No `USER_DEBUG` lines in the log | The log filter hides them, or **Open Log** was not ticked | Clear the filter and run the code again |
Summary
You used sf org list and sf org open to check the stored authorization. You ran a SOQL query to read the org name and edition, and anonymous Apex to read the org name and username. When all three agree with what you entered at sign-up, the org is ready for the rest of this tutorial.
Exercise
A teammate's query returns OrganizationType as Sandbox when you expected Developer Edition. Name the most likely cause and one way to prevent it.
Show answer
The command ran against a different org than intended, probably the default org. Prevent it by passing --target-org my-dev-org explicitly, and by running sf org list to check which orgs and aliases are authorized.
Exercise
Which two pieces of information does the anonymous Apex snippet print, and which methods supply them?
Show answer
It prints the org name from UserInfo.getOrganizationName() and the username from UserInfo.getUserName().