Step 1: Define a Platform Event
Step 2: Build the Client
Step 3: Configure Client Parameters
Step 4: Publish and Subscribe to Platform Events
Step 5: Subscribe to Change Events
Code Examples
Subscribe to change events and perform bitmap field expansion. By setting a flag in arguments.yaml, the Subscribe example expands the changedFields bitmap fields in change events into its component fields.
Change event headers are described in ChangeEventHeader Fields in the Change Data Capture Developer Guide. To learn more about bitmap field expansion, see Event Deserialization Considerations.
Prerequisites
In your pub-sub-api folder, go to java/src/main/resources and open the arguments.yaml file.
TOPIC: /data/OpportunityChangeEventPROCESS_CHANGE_EVENT_HEADER_FIELDS: true.In a Terminal window, navigate to the java folder.
To run the example, from the top-level java folder, enter:
./run.sh genericpubsub.Subscribe
The example subscribes to the /data/OpportunityChangeEvent channel and captures any new or changed Opportunity records.
In Salesforce, create an opportunity record. The change event is received and printed. For example:
1{
2 "ChangeEventHeader": {
3 "entityName": "Opportunity",
4 "recordIds": [
5 "006ZM000001dwBYYAY"
6 ],
7 "changeType": "CREATE",
8 "changeOrigin": "com/salesforce/api/soap/61.0;client=SfdcInternalAPI/",
9 "transactionKey": "00007d13-afe8-458d-370b-72f604213dd3",
10 "sequenceNumber": 1,
11 "commitTimestamp": 1722536398000,
12 "commitNumber": 77395491487,
13 "commitUser": "005ZM000000M6o1YAC",
14 "nulledFields": [],
15 "diffFields": [],
16 "changedFields": []
17 },
18 "AccountId": "001ZM000001QUlLYAW",
19 "IsPrivate": null,
20 "Name": "My Opportunity",
21 "Description": null,
22 "StageName": "Prospecting",
23 "Amount": 10000,
24 "Probability": 10,
25 "ExpectedRevenue": null,
26 "TotalOpportunityQuantity": null,
27 "CloseDate": 1725235200000,
28 "Type": null,
29 "NextStep": null,
30 "LeadSource": null,
31 "IsClosed": false,
32 "IsWon": false,
33 "ForecastCategory": "Pipeline",
34 "ForecastCategoryName": "Pipeline",
35 "CampaignId": null,
36 "HasOpportunityLineItem": false,
37 "Pricebook2Id": null,
38 "OwnerId": "005ZM000000M6o1YAC",
39 "CreatedDate": 1722536397000,
40 "CreatedById": "005ZM000000M6o1YAC",
41 "LastModifiedDate": 1722536397000,
42 "LastModifiedById": "005ZM000000M6o1YAC",
43 "LastStageChangeDate": null,
44 "ContactId": null,
45 "ContractId": null,
46 "LastAmountChangedHistoryId": null,
47 "LastCloseDateChangedHistoryId": null
48}Update the opportunity record. For example, select a Type of New Customer.
The change event for the update is received and printed. The output includes the Changed Fields section, which contains the fields that were expanded from the changedFields bitmap field. It lists the two fields that were changed: the Type field that you changed, and the LastModifiedDate field that the system changed. For example:
1{
2 "ChangeEventHeader": {
3 "entityName": "Opportunity",
4 "recordIds": [
5 "006ZM000001dwBYYAY"
6 ],
7 "changeType": "UPDATE",
8 "changeOrigin": "com/salesforce/api/soap/61.0;client=SfdcInternalAPI/",
9 "transactionKey": "00007cd5-988e-e3db-3adc-158958207197",
10 "sequenceNumber": 1,
11 "commitTimestamp": 1722536439000,
12 "commitNumber": 77395586403,
13 "commitUser": "005ZM000000M6o1YAC",
14 "nulledFields": [],
15 "diffFields": [],
16 "changedFields": [
17 "0x01000800"
18 ]
19 },
20 "AccountId": null,
21 "IsPrivate": null,
22 "Name": null,
23 "Description": null,
24 "StageName": null,
25 "Amount": null,
26 "Probability": null,
27 "ExpectedRevenue": null,
28 "TotalOpportunityQuantity": null,
29 "CloseDate": null,
30 "Type": "New Customer",
31 "NextStep": null,
32 "LeadSource": null,
33 "IsClosed": null,
34 "IsWon": null,
35 "ForecastCategory": null,
36 "ForecastCategoryName": null,
37 "CampaignId": null,
38 "HasOpportunityLineItem": null,
39 "Pricebook2Id": null,
40 "OwnerId": null,
41 "CreatedDate": null,
42 "CreatedById": null,
43 "LastModifiedDate": 1722536439000,
44 "LastModifiedById": null,
45 "LastStageChangeDate": null,
46 "ContactId": null,
47 "ContractId": null,
48 "LastAmountChangedHistoryId": null,
49 "LastCloseDateChangedHistoryId": null
50 }
512024-08-01 11:20:50,888 [grpc-default-executor-3] java.lang.Class - ============================
522024-08-01 11:20:50,889 [grpc-default-executor-3] java.lang.Class - ChangedFields
532024-08-01 11:20:50,889 [grpc-default-executor-3] java.lang.Class - ============================
542024-08-01 11:20:50,889 [grpc-default-executor-3] java.lang.Class - Type
552024-08-01 11:20:50,889 [grpc-default-executor-3] java.lang.Class - LastModifiedDate
562024-08-01 11:20:50,889 [grpc-default-executor-3] java.lang.Class - ============================The client code for the Pub/Sub API RPC methods is in the genericpubsub folder. To check out the implementation for each RPC method, open the corresponding Java file in the genericpubsub folder. For the Subscribe example, open Subscribe.java.
See Subscribe RPC Method.
See Also