WHERE

By default, a SOSL query on an object retrieves all rows that are visible to the user, including archived rows. To limit the search, you can filter the search result by specific field values.

conditionExpression 

The conditionExpression of the WHERE clause uses the following syntax:

1fieldExpression [logicalOperator fieldExpression2 ... ]

You can add multiple field expressions to a condition expression by using logical operators.

The condition expressions in SOSL FIND statements appear in bold in these examples:

  • FIND {test} RETURNING Account (id WHERE createddate = THIS_FISCAL_QUARTER)
  • FIND {test} RETURNING Account (id WHERE cf__c includes('AAA'))

You can use parentheses to define the order in which fieldExpressions are evaluated. You must specify parentheses when nesting operators. However, multiple operators of the same type don’t need to be nested. In the following example, the expression is true if fieldExpression1 is true and either fieldExpression2 or fieldExpression3 are true.

1fieldExpression1 AND (fieldExpression2 OR fieldExpression3)

However, the following expression is true if either fieldExpression3 is true or both fieldExpression1 and fieldExpression2 are true.

1(fieldExpression1 AND fieldExpression2) OR fieldExpression3

fieldExpression 

A fieldExpression uses the following syntax:

1_fieldName comparisonOperator value

where:

SyntaxDescription
fieldNameThe name of a field for the specified object. Use of single or double quotes around the name result in an error. You must have at least read-level permissions to the field. It can be any field except a long text area field, encrypted data field, or base64-encoded field. The name doesn’t need to be a field in the fieldList.
comparisonOperatorOperators that compare values, such as =,<=, IN, and LIKE. Operators are case insensitive for most fields, but case sensitive for case-sensitive fields.
valueA value used to compare with the value in the fieldName. Supply a value whose data type matches the field type of the specified field. The value must be a valid value, not other field names or calculations. If quotes are required, use single quotes. Double quotes result in an error. Quotes are unnecessary for dates and numbers. The date format is the same as the one used for SOQL.

Comparison Operators 

The following table lists the comparisonOperator values that are used in fieldExpression syntax. Comparisons on strings are case-insensitive.

OperatorNameDescription
=EqualsExpression is true if the value in the fieldName equals the value in the expression.
!=Not equalsExpression is true if the value in the fieldName doesn’t equal the specified value.
<Less thanExpression is true if the value in the fieldName is less than the specified value.
<=Less or equalExpression is true if the value in the fieldName is less than or equal to the specified value.
>Greater thanExpression is true if the value in the fieldName is greater than the specified value.
>=Greater or equalExpression is true if the value in the fieldName is greater than or equal to the specified value.
LIKELikeExpression is true if the value in the fieldName matches the characters of the text string in the specified value. The text string in the specified value must be enclosed in single quotes. The LIKE operator is supported for string fields only. The operator provides a mechanism for matching partial text strings and includes support for the wildcards and escaping described after this table.
ININIf the value equals any one of the values in a WHERE clause. The string values for IN must be in parentheses and surrounded by single quotes. You can query values in a field where another field on the same object has a specified set of values, using IN. You can also use IN and NOT IN for semi-joins and anti-joins when querying on ID (primary key) or reference (foreign key) fields.
NOT INNOT INIf the value doesn’t equal any of the values in a WHERE clause. The string values for NOT IN must be in parentheses and surrounded by single quotes.
INCLUDES EXCLUDES Applies only to multi-select picklists. See Query Multi-Select Picklists.

The LIKE operator includes support for:

  • The % and _ wildcards.

    • The % wildcard matches zero or more characters.
    • The _ wildcard matches exactly one character.
  • The escaping of special characters % or _.

Note: Don’t use the backslash (\) character in a search except to escape a special character. See Quoted String Escape Sequences.

This example query matches Appleton, Apple, and Appl, but not Bappl:

1FIND {test} IN ALL FIELDS RETURNING Contact(AccountId, FirstName, LastName WHERE LastName LIKE 'appl%')

Example IN query:

1FIND {test} IN ALL FIELDS RETURNING Account(Name WHERE BillingState IN ('California', 'New York'))

Example NOT IN query:

1FIND {test} IN ALL FIELDS RETURNING Account(Name WHERE BillingState NOT IN ('California', 'New York'))

Note: The logical operator NOT is unrelated to this comparison operator.

Logical Operators 

The following table lists the logical operator values that are used in fieldExpression syntax:

OperatorSyntaxDescription
ANDfieldExpressionX AND fieldExpressionYtrue if both fieldExpressionX and fieldExpressionY are true.
ORfieldExpressionX OR fieldExpressionYtrue if either fieldExpressionX or fieldExpressionY is true. In a WHERE clause that uses OR, records are returned even if the foreign key value in a record is null.
NOTnot fieldExpressionXtrue if fieldExpressionX false. There’s also a comparison operator NOT IN, which is different from this logical operator.

Example OR query:

1FIND {test} IN ALL FIELDS RETURNING Contact(Id WHERE LastName = 'Young' OR Account.Name = 'Quarry')

Example NOT query:

1FIND {test} IN ALL FIELDS RETURNING Account(Name WHERE AnnualRevenue > 0 AND (NOT Type = 'Prospect'))

Quoted String Escape Sequences 

You can use the following escape sequences with SOSL:

SequenceMeaning
\n or \NNew line
\r or \RCarriage return
\t or \TTab
\b or \BBell
\f or \FForm feed
\"One double-quote character
\'One single-quote character
\\Backslash
LIKE expression only: \_Matches a single underscore character ( _ )
LIKE expression only:\%Matches a single percent sign character ( % )
\uXXXXA Unicode character with XXXX as the code (for example, \u00e9 represents the é character)

If you use a backslash character in any other context, an error occurs.

Example WHERE Clauses 

Example WHERE clause with fiscal quarter filter:

1FIND {test}
2    RETURNING Account (id WHERE createddate = THIS_FISCAL_QUARTER)

Example WHERE clause with multi-select picklist includes:

1FIND {test}
2    RETURNING Account (id WHERE cf__c includes('AAA'))

Example WHERE clause with multiple objects and ORDER BY:

1FIND {test}
2    RETURNING Account (id), User(Field1,Field2 WHERE Field1 = 'test' order by id ASC, Name DESC)

Example WHERE clause searching contacts, users, and accounts:

1FIND {test} IN ALL FIELDS
2    RETURNING Contact(Salutation, FirstName, LastName, AccountId WHERE Name = 'test'),
3        User(FirstName, LastName),
4        Account(id WHERE BillingState IN ('California', 'New York'))

Example WHERE clause with complex OR/AND conditions:

1FIND {test}
2    RETURNING Account (id WHERE (Name = 'New Account')
3        or (Id = '001z00000008Vq7'
4        and Name = 'Account Insert Test')
5        or (NumberOfEmployees < 100 or NumberOfEmployees = null)
6        ORDER BY NumberOfEmployees)

To search for a Salesforce Knowledge article by ID:

1FIND {tourism}
2    RETURNING KnowledgeArticleVersion (Id, Title WHERE id = 'ka0D0000000025eIAA')

To search for multiple Salesforce Knowledge articles by ID:

1FIND {tourism}
2    RETURNING KnowledgeArticleVersion
3        (Id, Title WHERE id IN ('ka0D0000000025eIAA', 'ka0D000000002HCIAY'))

To search for “San Francisco” in all fields of all My_Custom_Object__c objects that have a geolocation or address location within 500 miles of the latitude and longitude coordinates 37 and 122, respectively:

1FIND {San Francisco}
2    RETURNING My_Custom_Object__c (Id
3        WHERE DISTANCE(My_Location_Field__c,GEOLOCATION(37,122),'mi') < 100)

See Also