SOQL - Salesforce Object Query Language
SOQL - Salesforce Object Query Language
You ask the same questions when you create a SOQL query. In a query,
the answers to these questions are the fields and the object.
This query contains SOQL keywords (SELECT and FROM), record fields
(Name and Email), and an object (the standard Contact object).
The SELECT clause and the FROM clause are required, but SOQL also has
optional clauses to refine your query.
The example queries that you create in your Trailhead Playground return a
fairly small number of records. The last query returned 20 records. But
what if there were thousands of records? The amount of computing power
needed to process that data would be substantial. Wouldn't it be nice if
SOQL had a way to return a subset of all records? Actually, it does.
WHERE
The WHERE clause sets the conditions that a record must match to be
selected and returned. Use the WHERE clause the same way you use
filters to limit the data shown in a list view or report. For example, if we're
looking for a contact whose first name is Stella, we can add WHERE
FirstName = 'Stella' to the end of our query, like this:
You can also define the WHERE clause to filter using more than one
condition. There are multiple ways to do this using three operators: AND,
OR, and IN. Let's consider some examples.
AND Use AND to return records that meet two conditions. This query returns
the first name Stella and the last name Pavlova.
FROM Contact
Copy
OR Use OR to return records that meet one of two conditions. This query r
last name James or the last name Barr.
FROM Contact
Copy
IN Use IN to return records that meet at least one of three or more condit
commonly used to return the values of a picklist, or values from a LIST
query that would otherwise have many OR conditions. This query retur
the last name James, Barr, Nedaerk, or Forbes.
Copy
LIMIT
The LIMIT keyword sets the maximum number of records to return. LIMIT
is helpful when you're testing and don't want to wait for a query to
process a large set of data. As you learn more about SOQL, you'll discover
more relevant ways to avoid returning too many records, but until then,
LIMIT is an easy solution. Let's try adding a limit to our query.
Copy
3. Click Execute.
ORDER BY
Now that you have a handle on the quantity of results, how can you
organize those results? Use the ORDER BY clause to sort results by the
value of a specific field. Optionally, use a qualifier in an ORDER BY clause
to specify ascending order (default) or descending order, just as you
would in a spreadsheet. Finally, if you have many empty field values, use
the NULLS qualifier to group all of the NULL values either first (default) or
last.
LIMIT 5
Copy
LIMIT 5
Copy
NULLS Returns null records at the beginning SELECT Name, Email FROM
FIRST | LAST (NULLS FIRST) or end (NULLS LAST)
ORDER BY Email
NULLS LAST
Copy
String fullName = 'First Name: ' + con.FirstName + ', Last Name: '
+ con.LastName;
system.debug(fullName);