# Step 5: Subscribe to Change Events

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](https://developer.salesforce.com/docs/atlas.en-us.change_data_capture.meta/change_data_capture/cdc_event_fields_header.htm) in the *Change Data Capture Developer Guide*. To learn more about bitmap field expansion, see [Event Deserialization Considerations](/docs/platform/pub-sub-api/guide/event-deserialization-considerations.md).

**Prerequisites**

- Because this example subscribes to changes for Opportunity, select Opportunity for change data capture in Setup. For more information, see [*Select Objects for Change Notifications in the User Interface*](https://developer.salesforce.com/docs/atlas.en-us.change_data_capture.meta/change_data_capture/cdc_select_objects.htm) in the *Change Data Capture Developer Guide*.

1. In your `pub-sub-api` folder, go to `java/src/main/resources` and open the `arguments.yaml` file.
   - Provide the change event channel: `TOPIC: /data/OpportunityChangeEvent`
   - Enable bitmap field expansion for ChangeEventHeader: `PROCESS_CHANGE_EVENT_HEADER_FIELDS: true`.

2. In a Terminal window, navigate to the `java` folder.

3. 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.

4. In Salesforce, create an opportunity record. The change event is received and printed. For example:

   ```
   {
      "ChangeEventHeader": {
         "entityName": "Opportunity",
         "recordIds": [
            "006ZM000001dwBYYAY"
         ],
         "changeType": "CREATE",
         "changeOrigin": "com/salesforce/api/soap/61.0;client=SfdcInternalAPI/",
         "transactionKey": "00007d13-afe8-458d-370b-72f604213dd3",
         "sequenceNumber": 1,
         "commitTimestamp": 1722536398000,
         "commitNumber": 77395491487,
         "commitUser": "005ZM000000M6o1YAC",
         "nulledFields": [],
         "diffFields": [],
         "changedFields": []
      },
      "AccountId": "001ZM000001QUlLYAW",
      "IsPrivate": null,
      "Name": "My Opportunity",
      "Description": null,
      "StageName": "Prospecting",
      "Amount": 10000,
      "Probability": 10,
      "ExpectedRevenue": null,
      "TotalOpportunityQuantity": null,
      "CloseDate": 1725235200000,
      "Type": null,
      "NextStep": null,
      "LeadSource": null,
      "IsClosed": false,
      "IsWon": false,
      "ForecastCategory": "Pipeline",
      "ForecastCategoryName": "Pipeline",
      "CampaignId": null,
      "HasOpportunityLineItem": false,
      "Pricebook2Id": null,
      "OwnerId": "005ZM000000M6o1YAC",
      "CreatedDate": 1722536397000,
      "CreatedById": "005ZM000000M6o1YAC",
      "LastModifiedDate": 1722536397000,
      "LastModifiedById": "005ZM000000M6o1YAC",
      "LastStageChangeDate": null,
      "ContactId": null,
      "ContractId": null,
      "LastAmountChangedHistoryId": null,
      "LastCloseDateChangedHistoryId": null
   }
   ```

5. 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:

   ```
   {
      "ChangeEventHeader": {
         "entityName": "Opportunity",
         "recordIds": [
            "006ZM000001dwBYYAY"
         ],
         "changeType": "UPDATE",
         "changeOrigin": "com/salesforce/api/soap/61.0;client=SfdcInternalAPI/",
         "transactionKey": "00007cd5-988e-e3db-3adc-158958207197",
         "sequenceNumber": 1,
         "commitTimestamp": 1722536439000,
         "commitNumber": 77395586403,
         "commitUser": "005ZM000000M6o1YAC",
         "nulledFields": [],
         "diffFields": [],
         "changedFields": [
            "0x01000800"
         ]
      },
      "AccountId": null,
      "IsPrivate": null,
      "Name": null,
      "Description": null,
      "StageName": null,
      "Amount": null,
      "Probability": null,
      "ExpectedRevenue": null,
      "TotalOpportunityQuantity": null,
      "CloseDate": null,
      "Type": "New Customer",
      "NextStep": null,
      "LeadSource": null,
      "IsClosed": null,
      "IsWon": null,
      "ForecastCategory": null,
      "ForecastCategoryName": null,
      "CampaignId": null,
      "HasOpportunityLineItem": null,
      "Pricebook2Id": null,
      "OwnerId": null,
      "CreatedDate": null,
      "CreatedById": null,
      "LastModifiedDate": 1722536439000,
      "LastModifiedById": null,
      "LastStageChangeDate": null,
      "ContactId": null,
      "ContractId": null,
      "LastAmountChangedHistoryId": null,
      "LastCloseDateChangedHistoryId": null
      }
   2024-08-01 11:20:50,888 [grpc-default-executor-3] java.lang.Class - ============================
   2024-08-01 11:20:50,889 [grpc-default-executor-3] java.lang.Class -        ChangedFields
   2024-08-01 11:20:50,889 [grpc-default-executor-3] java.lang.Class - ============================
   2024-08-01 11:20:50,889 [grpc-default-executor-3] java.lang.Class - Type
   2024-08-01 11:20:50,889 [grpc-default-executor-3] java.lang.Class - LastModifiedDate
   2024-08-01 11:20:50,889 [grpc-default-executor-3] java.lang.Class - ============================
   ```

## Learn More

The client code for the Pub/Sub API RPC methods is in the [genericpubsub](https://github.com/forcedotcom/pub-sub-api/tree/main/java/src/main/java/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](https://github.com/forcedotcom/pub-sub-api/blob/main/java/src/main/java/genericpubsub/Subscribe.java).

See [Subscribe RPC Method](https://developer.salesforce.com/docs/platform/pub-sub-api/references/methods/subscribe-rpc.md).

**See Also**

- *Change Data Capture Developer Guide*: [ChangeEventHeader Fields](https://developer.salesforce.com/docs/atlas.en-us.change_data_capture.meta/change_data_capture/cdc_event_fields_header.htm)
- [Event Deserialization Considerations](/docs/platform/pub-sub-api/guide/event-deserialization-considerations.md)
