Add a Filter with Tooling API

Create a channel and channel member in Tooling API using API version 56.0 or later.

User Permissions Needed
To create or update PlatformEventChannel and PlatformEventChannelMember objects:Customize Application
To use REST API:API Enabled

You can use your preferred REST API tool to perform these steps. We recommend using Postman with the Salesforce Platform APIs collection, which contains handy templates for Salesforce API calls. See Quick Start: Connect Postman to Salesforce in Trailhead.

The steps are based on a custom channel. We recommend using a custom channel instead of the standard channel so that the filtered stream is isolated from the standard event stream and subscribers expect the stream to be filtered. You can alternatively use the ChangeEvents standard channel. If you do so, you can skip steps 1 and 2, and in step 4, adjust the FullName field and use this eventChannel value: "eventChannel": "ChangeEvents".

  1. To create a channel, send a POST request to this URI.

    1/services/data/v68.0/tooling/sobjects/PlatformEventChannel

    If you’re using Postman, expand Event Platform | Custom Channels | Platform Event, and then click Create channel.

  2. Use this example request body. The channelType is data because this is a channel for change events.

    1{
    2  "FullName": "FilteredChannel__chn",
    3  "Metadata": {
    4    "channelType": "data",
    5    "label": "My Custom Filtered Channel"
    6  }
    7}

    You receive a response similar to this example response.

    1{
    2  "id" : "0YLRM000000004m4AA",
    3  "success" : true,
    4  "errors" : [ ],
    5  "warnings" : [ ],
    6  "infos" : [ ]
    7}
  3. Add a channel member that specifies the Industry field as an enriched field, a change event type of AccountChangeEvent, and a filter expression.

    1/services/data/v68.0/tooling/sobjects/PlatformEventChannelMember

    If you’re using Postman, expand Event Platform | Custom Channels | Platform Event, and then click Create channel member.

  4. Use this example request body.

    1{
    2  "FullName": "FilteredChannel_chn_AccountChangeEvent",
    3  "Metadata": {
    4    "eventChannel": "FilteredChannel__chn",
    5    "filterExpression": "Industry='Agriculture' AND NumberOfEmployees>1000",
    6    "selectedEntity": "AccountChangeEvent"
    7  }
    8}

    You receive a response similar to this example response.

    1{
    2  "id" : "0v8RM0000004VAKYA2",
    3  "success" : true,
    4  "errors" : [ ],
    5  "warnings" : [ ],
    6  "infos" : [ ]
    7}

To update a filter expression, perform a PATCH request to /services/data/v68.0/tooling/sobjects/PlatformEventChannelMember/<ChannelMemberID>, and pass in the entire request body with the new filter expression. You can update only the filter expression and enriched fields of a channel member. All other fields aren’t updateable.

If your Salesforce org has a namespace, prepend the namespace prefix to each field used in filterExpression and the selectedEntity value in the PlatformEventChannelMember request body. For example, if the namespace is ns, the request body in this example becomes:

1{
2  "FullName": "FilteredChannel_chn_AccountChangeEvent",
3  "Metadata": {
4    "eventChannel": "FilteredChannel__chn",
5    "filterExpression": "ns__Industry='Agriculture' AND ns__NumberOfEmployees>1000",
6    "selectedEntity": "AccountChangeEvent"
7  }
8}

You can add another filter to the same channel for another change event by adding another channel member. For example, if you want to filter lead change events, add a second channel member for the FilteredChannel__chn channel. In this member, specify selectedEntity as LeadChangeEvent, and specify the filter expression. For example:

1{
2  "FullName": "FilteredChannel_chn_LeadChangeEvent",
3  "Metadata": {
4    "eventChannel": "FilteredChannel__chn",
5    "filterExpression": "AnnualRevenue>1000000",
6    "selectedEntity": "LeadChangeEvent"
7  }
8}

See Also