Newer Version Available
WITH filteringExpression
Unlike the WHERE clause which only supports fields from the object specified in the FROM clause, WITH allows you to filter by other related criteria. For example, you can use the WITH clause to filter articles based on their classification in one or more data category groups. The WITH clause can only be used in the following cases:
- To filter records based on their categorization. See WITH DATA CATEGORY filteringExpression.
- To query and retrieve record changes tracked in a user profile feed. See Custom Object__Feed in the Object Reference for Salesforce.
- To filter records based on user access permissions on fields or objects being queried within Apex code. See Filter SOQL Queries Using WITH SECURITY_ENFORCED in the Apex Developer Guide.
If WITH is specified, the query returns only records that match the filter and are visible to the user. If unspecified, the query returns only the matching records that are visible to the user.
The filtering expression in this statement is highlighted in bold. The syntax is explained in the following sections.
- SELECT Title FROM KnowledgeArticleVersion WHERE PublishStatus='online' WITH DATA CATEGORY Geography__c ABOVE usa__c
- SELECT Id FROM UserProfileFeed WITH UserId='005D0000001AamR' ORDER BY CreatedDate DESC, Id DESC LIMIT 20
Using RecordVisibilityContext
You can also use RecordVisibilityContext to filter WITH clauses. This feature is available in API version 48.0 and later.
For example:
1SELECT Id, RecordVisibility.VisibilityAttribute FROM Account WHERE Id = 'xxx'
2 WITH RecordVisibilityContext (maxDescriptorPerRecord=100, supportsDomains=true, supportsDelegates=true)Or:
1SELECT recordId, VisibilityAttribute FROM RecordVisibility WHERE recordId = 'xxx'
2 WITH RecordVisibilityContext (maxDescriptorPerRecord=100, supportsDomains=true, supportsDelegates=true)The following fields can be used as parameters:
| Field | Description | Default |
|---|---|---|
| maxDescriptorPerRecord | The maximum number of descriptors to return per record. If the actual number of descriptors for a specific record exceeds that value, then a "tooManyDescriptors" visibility attribute is returned for that record. |
|
| supportsDomains | Whether the Visibility Service is allowed to generate "domain" visibility attributes. | true |
| supportsDelegates | Whether the Visibility Service is allowed to generate "delegate" visibility attributes. | true |
At least one field is necessary. This is an invalid query:
1SELECT Id, RecordVisibility.VisibilityAttribute FROM Account WHERE Id = 'xxx'
2 WITH RecordVisibilityContextThis query is valid:
1SELECT Id, RecordVisibility.VisibilityAttribute FROM Account WHERE Id = 'xxx'
2 WITH RecordVisibilityContext (maxDescriptorPerRecord=100)For more information, see RecordVisibility in the Object Reference.