Subscribe to the Channel and Receive the Filtered Event Stream

After configuring the filter, subscribe to the channel, and receive the event messages that match the filter expression. The channel to subscribe to is /data/ChannelName__chn. Only Pub/Sub API and CometD clients support stream filtering. Because Apex triggers don’t support channels, you can’t use them to subscribe to filtered event streams.

Before subscribing to the channel, follow the steps in the previous sections to create the FilteredChannel__chn channel, and configure a filter expression for AccountChangeEvent with Tooling API or Metadata API.

Note

In this example, we use a Pub/Sub API Java client.

  1. To set up the Pub/Sub API Java client, follow the steps in Java Quick Start for Publishing and Subscribing to Events in the Pub/Sub API Developer Guide.

  2. In Step 3: Configure Client Parameters, supply the configuration parameters in arguments.yaml. Also, make sure you supply the values.

    1. TOPIC: /data/FilteredChannel__chn

    2. PROCESS_CHANGE_EVENT_HEADER_FIELDS: true

      This value ensures that the bitmap fields, such as changedFields, in ChangeEventHeader are expanded. For more information, see Event Deserialization Considerations in the Pub/Sub API Developer Guide.

  3. In a Terminal window, navigate to the top-level java folder.

  4. To run the Subscribe RPC example, enter:./run.sh genericpubsub.Subscribe.

  5. Let’s create some accounts and update one account. Each account creation and update generates a change event that Salesforce publishes.

    1. Click App Launcher, and enter Accounts in the search box.

    2. Right-click Accounts, and select to open the link in a new tab.

    3. In the new tab, search for Accounts in the App Launcher.

    4. Create an account with these values.

      • Account Name: Acme North
      • Industry: Agriculture
      • NumberOfEmployees: 1500
    5. Create a second account with these values.

      • Account Name: Acme South
      • Industry: Agriculture
      • NumberOfEmployees: 20
    6. Create a third account with these values.

      • Account Name: Acme West
      • NumberOfEmployees: 1100
    7. Update the ‘Acme North’ account with this value.

      • Type: Prospect

As a refresher, here’s the filter expression that was set in the previous section.

1Industry='Agriculture' AND NumberOfEmployees>1000

From the change event messages that Salesforce published in response to the accounts created, only the first event of account Acme North matches the filter criteria set up in the previous example. The first event message is delivered to the client. Also, the last change event corresponding to the Acme North account update is delivered, as the account continues to match the criteria. Notice that even though only the Type field was updated, the change event includes the NumberOfEmployees and Industry fields because they’re auto-enriched.

The second and third events don’t match the filter criteria and aren’t delivered to the client. In the second event, the NumberOfEmployees field is less than 1,000, and the third event has a blank (null) Industry.

This example shows the received change event messages after subscribing to the /data/FilteredChannel__chn channel.

1{
2  "ChangeEventHeader": {
3    "entityName": "Account",
4    "recordIds": [
5      "001ZM000002JNaXYAW"
6    ],
7    "changeType": "CREATE",
8    "changeOrigin": "com/salesforce/api/soap/61.0;client=SfdcInternalAPI/",
9    "transactionKey": "00007816-6798-4836-5275-80afa466c4d4",
10    "sequenceNumber": 1,
11    "commitTimestamp": 1722531023000,
12    "commitNumber": 77390604774,
13    "commitUser": "005ZM000000M6o1YAC",
14    "nulledFields": [],
15    "diffFields": [],
16    "changedFields": []
17  },
18  "Name": "Acme North",
19  "Type": null,
20  "ParentId": null,
21  "BillingAddress": null,
22  "ShippingAddress": null,
23  "Phone": null,
24  "Fax": null,
25  "AccountNumber": null,
26  "Website": null,
27  "Sic": null,
28  "Industry": "Agriculture",
29  "AnnualRevenue": null,
30  "NumberOfEmployees": 1500,
31  "Ownership": null,
32  "TickerSymbol": null,
33  "Description": null,
34  "Rating": null,
35  "Site": null,
36  "OwnerId": "005ZM000000M6o1YAC",
37  "CreatedDate": 1722531023000,
38  "CreatedById": "005ZM000000M6o1YAC",
39  "LastModifiedDate": 1722531023000,
40  "LastModifiedById": "005ZM000000M6o1YAC",
41  "Jigsaw": null,
42  "JigsawCompanyId": null,
43  "AccountSource": null,
44  "SicDesc": null,
45  "External_Account_ID__c": null
46}
47with schema name: AccountChangeEvent
48
49{
50  "ChangeEventHeader": {
51    "entityName": "Account",
52    "recordIds": [
53      "001ZM000002JNaXYAW"
54    ],
55    "changeType": "UPDATE",
56    "changeOrigin": "com/salesforce/api/soap/61.0;client=SfdcInternalAPI/",
57    "transactionKey": "000077fe-1655-b8fc-44ab-27df66fccbf6",
58    "sequenceNumber": 1,
59    "commitTimestamp": 1722531115000,
60    "commitNumber": 77390686560,
61    "commitUser": "005ZM000000M6o1YAC",
62    "nulledFields": [],
63    "diffFields": [],
64    "changedFields": [
65      "0x400004"
66    ]
67  },
68  "Name": null,
69  "Type": "Prospect",
70  "ParentId": null,
71  "BillingAddress": null,
72  "ShippingAddress": null,
73  "Phone": null,
74  "Fax": null,
75  "AccountNumber": null,
76  "Website": null,
77  "Sic": null,
78  "Industry": "Agriculture",
79  "AnnualRevenue": null,
80  "NumberOfEmployees": 1500,
81  "Ownership": null,
82  "TickerSymbol": null,
83  "Description": null,
84  "Rating": null,
85  "Site": null,
86  "OwnerId": null,
87  "CreatedDate": null,
88  "CreatedById": null,
89  "LastModifiedDate": 1722531115000,
90  "LastModifiedById": null,
91  "Jigsaw": null,
92  "JigsawCompanyId": null,
93  "AccountSource": null,
94  "SicDesc": null,
95  "External_Account_ID__c": null
96}
97with schema name: AccountChangeEvent
982024-08-01 09:52:08,030 [grpc-default-executor-1] java.lang.Class - ============================
992024-08-01 09:52:08,030 [grpc-default-executor-1] java.lang.Class -        ChangedFields
1002024-08-01 09:52:08,030 [grpc-default-executor-1] java.lang.Class - ============================
1012024-08-01 09:52:08,030 [grpc-default-executor-1] java.lang.Class - Type
1022024-08-01 09:52:08,030 [grpc-default-executor-1] java.lang.Class - LastModifiedDate
1032024-08-01 09:52:08,030 [grpc-default-executor-1] java.lang.Class - ============================