The optional WHERE clause allows you to filter search results for a given object based on individual field values. If unspecified, the search retrieves all the rows in the object that are visible to the user.
The conditionExpression of the WHERE clause uses the following syntax:
fieldExpression [logicalOperatorfieldExpression2 ... ]
You can add multiple field expressions to a condition expression by using logical operators.
fieldExpression1 AND (fieldExpression2 OR fieldExpression3)
(fieldExpression1 AND fieldExpression2) OR fieldExpression3
A fieldExpression uses the following syntax:
fieldName comparisonOperator value
where:
| Syntax | Description |
|---|---|
| fieldName | The name of a field in the specified object. Use of single or double quotes around the name will 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. It does not need to be a field in the fieldList. |
| comparisonOperator | Case-insensitive operators that compare values. |
| value | A value used to compare with the value in fieldName. You must supply a value whose data type matches the field type of the specified field. You must supply a native value—other field names or calculations are not permitted. If quotes are required (for example, they are not for dates and numbers), use single quotes. Double quotes result in an error. |
The following table lists the comparisonOperator values that are used in fieldExpression syntax. Note that comparisons on strings are case-insensitive.
| Operator | Name | Description |
|---|---|---|
| = | Equals | Expression is true if the value in the specified fieldName equals the specified value in the expression. String comparisons using the equals operator are case-insensitive. |
| != | Not equals | Expression is true if the value in the specified fieldName does not equal the specified value. |
| < | Less than | Expression is true if the value in the specified fieldName is less than the specified value. |
| <= | Less or equal | Expression is true if the value in the specified fieldName is less than, or equals, the specified value. |
| > | Greater than | Expression is true if the value in the specified fieldName is greater than the specified value. |
| >= | Greater or equal | Expression is true if the value in the specified fieldName is greater than or equal to the specified value. |
| LIKE | Like | Expression is true if the value in the specified fieldName matches the characters of the text string in the specified value. The LIKE operator
in SOQL and SOSL is similar to the LIKE operator in SQL; it provides a mechanism for matching partial text
strings and includes support for wildcards.
For example, the following query matches Appleton, Apple,
and Bappl , but not Appl: SELECT AccountId, FirstName, lastname FROM Contact WHERE lastname LIKE 'appl_%' |
| IN | IN | If the value equals any one of the specified values in a WHERE clause. For example: Note that the values for IN must be in parentheses. String values must be surrounded by single quotes. IN and NOT IN can also be used for semi-joins and anti-joins when querying on ID (primary key) or reference (foreign key) fields. |
| NOT IN | NOT IN | If the value does not equal any of the specified values in
a WHERE clause. For example: SELECT Name FROM Account
WHERE BillingState NOT IN ('California', 'New York')Note that the values for NOT IN must be in parentheses, and string values must be surrounded by single quotes. There is also a logical operator NOT, which is unrelated to this comparison operator. |
| INCLUDES EXCLUDES | Applies only to multi-select picklists. |
The following table lists the logical operator values that are used in fieldExpression syntax:
| Operator | Syntax | Description |
|---|---|---|
| AND | fieldExpressionX AND fieldExpressionY | true if both fieldExpressionX and fieldExpressionY are true. |
| OR | fieldExpressionX OR fieldExpressionY | true if either fieldExpressionX or fieldExpressionY is true. Relationship queries with foreign key values in an OR clause behave differently depending on the version of the API. In a WHERE clause using OR, if the foreign key value in a record is null, the record is returned in version 13.0 and later, but not returned in versions before 13.0. SELECT Id FROM Contact WHERE LastName = 'foo' or Account.Name = 'bar' |
| NOT | not fieldExpressionX | true if fieldExpressionX is false. There is also a comparison operator NOT IN, which is different from this logical operator. |
You can use the following escape sequences with SOSL:
| Sequence | Meaning |
|---|---|
| \n or \N | New line |
| \r or \R | Carriage return |
| \t or \T | Tab |
| \b or \B | Bell |
| \f or \F | Form 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 ( % ) |
If you use a backslash character in any other context, an error occurs.
| Example(s) |
|---|
FIND {test}
RETURNING Account (id WHERE createddate = THIS_FISCAL_QUARTER)
|
FIND {test}
RETURNING Account (id WHERE cf__c includes('AAA'))
|
FIND {test}
RETURNING Account (id), User(Field1,Field2 WHERE Field1 = 'test' order by id ASC, Name DESC)
|
FIND {test} IN ALL FIELDS
RETURNING Contact(Salutation, FirstName, LastName, AccountId WHERE Name = 'test'),
User(FirstName, LastName),
Account(id WHERE BillingState IN ('California', 'New York'))
|
FIND {test}
RETURNING Account (id WHERE (Name = 'New Account' and ((not Id = null) or Id != null))
or (Id = '001z00000008Vq7'
and Name = 'Account Insert Test')
or (NumberOfEmployees < 100 or NumberOfEmployees = null)
ORDER BY NumberOfEmployees)
|
To search for a Salesforce Knowledge article by ID:FIND {tourism}
RETURNING KnowledgeArticleVersion (Id, Title WHERE id = 'ka0D0000000025eIAA') |
To search for multiple Salesforce Knowledge articles by ID:FIND {tourism}
RETURNING KnowledgeArticleVersion
(Id, Title WHERE id IN ('ka0D0000000025eIAA', 'ka0D000000002HCIAY')) |