Wednesday 15 May 2013


Filtering Records

SOQL supports filter conditions to reduce the number of records returned.A filter condition
consists of a field name to filter, an operator, and a literal value.
Valid operators are > (greater than), < (less than), >= (greater than or equal to), <= (less
than or equal to), = (equal to), != (not equal to), IN and NOT IN (matches a list of literal
values, and supports semi-joins and anti-joins), and INCLUDES and EXCLUDES (match
against multi-select picklist values). On String fields the LIKE operator is also available,
which applies a pattern to filter records.The pattern uses the % wildcard to match zero or
more characters, _ to match one character, and the \ character to escape the % and _
wildcards, treating them as regular characters.
Multiple filters are combined in a single SOQL statement using the Boolean operators
AND and OR and grouped with parentheses. Listing 5-26 returns the names of accounts
with a type of direct customer, a modification date sometime during the current year, and
more than $100 million in annual revenue.


Listing 5-26 SOQL Statement with Filter Conditions
SELECT Name
FROM Account
WHERE AnnualRevenue > 100000000
AND Type = 'Customer - Direct'
AND LastModifiedDate = THIS_YEAR


Notice the way literal values are specified.Apostrophes must be used around String literals
but never with other data types. THIS_YEAR is a built-in relative time function.The
values of relative time functions vary based on when the query is executed. Other relative
time functions are YESTERDAY, TODAY, TOMORROW, LAST_WEEK, THIS_WEEK, NEXT_WEEK, and
so forth.
Absolute dates and times can also be specified without apostrophes. Dates must use the
YYYY-MM-DD format. Datetimes can be YYYY-MM-DDThh:mm:ssZ, YYYY-MMDDThh:
mm:ss+hh:mm, or YYYY-MM-DDThh:mm:ss-hh:mm, indicating the positive or negative
offset from Coordinated Universal Time (UTC).
In addition to filter conditions, SOQL supports the LIMIT keyword. It sets an absolute
upper bound on the number of records that can be returned from the query. It can be
used in conjunction with all the other SOQL features. For example, the SOQL statement
in Listing 5-27 returns up to ten Account records modified today.

Listing 5-27
SELECT Name, Type
FROM Account
WHERE LastModifiedDate = TODAY
LIMIT 10



No comments:

Post a Comment