Sub topic 1
SOQL Syntax Basics: SELECT and FROM
SOQL (Salesforce Object Query Language) reads records from Salesforce objects. This chapter covers the two clauses every query needs: SELECT and FROM. Later chapters add filtering, sorting, and the other clauses.
Prerequisites
This chapter assumes you know:
- Basic Apex syntax: variables, collections, loops, and classes.
- Salesforce data model basics: objects, fields, and lookup and master-detail relationships.
- How to run Apex in the Developer Console or an IDE.
The Shape of a Query
A SOQL query names the fields you want and the object they come from.
SELECT field1, field2 FROM ObjectNameSELECTlists the fields to return, separated by commas.FROMnames exactly one object to query.
SOQL is not SQL. It has no SELECT *, and it has no JOIN keyword. You must list each field by its API name. Relationships are traversed with dot notation and subqueries, which a later chapter covers.
Running a Query in Apex
In Apex, you put a query in square brackets. The result is a List of records for that object.
List<Account> accts = [
SELECT Id, Name, Industry
FROM Account
LIMIT 10
];
for (Account a : accts) {
System.debug(a.Name + ' - ' + a.Industry);
}Keywords such as SELECT and FROM are not case sensitive. Field and object names are also not case sensitive, but use the exact API name for readability. Custom objects and fields end in __c, for example Invoice__c and Amount__c.
Fields You Did Not Select
Apex only fills in the fields you list. Reading any other field throws a System.SObjectException.
Account a = [SELECT Id, Name FROM Account LIMIT 1];
System.debug(a.Name); // works
System.debug(a.Industry); // SObjectException: field was not retrieved by SOQLThe Id field is the exception. Salesforce returns it even if you leave it out of the SELECT list. Most developers still list it, which makes the intent clear.
Counting Records
SELECT COUNT() takes no field list and returns an Integer.
Integer total = [SELECT COUNT() FROM Account];
System.debug('Accounts: ' + total);Common Errors
| Error | Cause | Fix |
|---|---|---|
| `No such column 'Industy' on entity 'Account'` | Misspelled field name | Check the API name in Object Manager |
| `sObject type 'Acount' is not supported` | Misspelled object name | Correct the name after `FROM` |
| `SObjectException: ... field was not retrieved` | Field read but not selected | Add it to `SELECT` |
| Compile error on `SELECT *` | SOQL has no wildcard | List each field |
Apex checks inline queries when it compiles the class. A misspelled name in square brackets fails at save time, not at run time.
Summary
Every query names an object after FROM and a field list after SELECT. Fields you leave out cannot be read, except Id, which always comes back.
Exercise
This code throws a SObjectException. Why, and how do you fix it? Contact c = [SELECT Id, LastName FROM Contact LIMIT 1]; System.debug(c.Email);
Show answer
The query did not select Email, so Apex never loaded it. Add Email to the SELECT list: [SELECT Id, LastName, Email FROM Contact LIMIT 1].
Exercise
Write an inline query that returns the Id and Name of every Opportunity into a list.
Show answer
List<Opportunity> opps = [SELECT Id, Name FROM Opportunity];