0% found this document useful (0 votes)
9 views

SOQL - Salesforce Object Query Language

SOQL (Salesforce Object Query Language) is used to retrieve record data from a Salesforce database, similar to creating a report. It includes required clauses like SELECT and FROM, as well as optional clauses such as WHERE, LIMIT, and ORDER BY to refine queries and manage data output. The document also provides examples of how to use these clauses effectively to filter and organize query results.

Uploaded by

kapiljain522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

SOQL - Salesforce Object Query Language

SOQL (Salesforce Object Query Language) is used to retrieve record data from a Salesforce database, similar to creating a report. It includes required clauses like SELECT and FROM, as well as optional clauses such as WHERE, LIMIT, and ORDER BY to refine queries and manage data output. The document also provides examples of how to use these clauses effectively to filter and organize query results.

Uploaded by

kapiljain522
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

What Is SOQL?

SOQL is a language that gets record data from a Salesforce database.


In Object-Oriented Programming for Admins you learned that you can use
Data Manipulation Language (DML) statements to insert, update, and
delete records. But what if you want to get data that already exists in the
database? SOQL makes it simple to retrieve data.

Creating SOQL queries is similar to creating a report. When you create a


report, you answer questions such as:

1. What fields do I want to display?

2. Where are those fields located?

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).

Optional SOQL Clauses

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:

SELECT Name, Email FROM Contact WHERE FirstName = 'Stella'


Copy

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.

SELECT Name, Email

FROM Contact

WHERE FirstName = 'Stella' AND LastName = 'Pavlova'

Copy

OR Use OR to return records that meet one of two conditions. This query r
last name James or the last name Barr.

SELECT Name, Email

FROM Contact

WHERE LastName = 'James' OR LastName = 'Barr'

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.

SELECT Name, Email FROM Contact

WHERE LastName IN ('James', 'Barr', 'Nedaerk', '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.

1. Return to the Query Editor in the Developer Console.

2. At the end of the query, type LIMIT 5, so that it reads:

SELECT Email, Name FROM Contact LIMIT 5

Copy

3. Click Execute.

The Query Results pane shows five results.

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.

Syntax Description Example

ASC Returns results in ascending order SELECT Name, Email FROM

ORDER BY Name ASC

LIMIT 5

Copy

DESC Returns results in descending order SELECT Name, Email FROM

ORDER BY Email DESC

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

In the Developer Console, click File | New | Apex Class.


public class ContactUtility {

public static void viewContacts(){

List<Contact> listOfContacts = [SELECT FirstName, LastName FROM


Contact];

for (Contact con : listOfContacts){

String fullName = 'First Name: ' + con.FirstName + ', Last Name: '
+ con.LastName;

system.debug(fullName);

You might also like