Skip to main content

Update an Existing Channel Member

If a channel member already exists, you can’t perform a POST request to overwrite it with enriched fields. Instead, update the channel member by doing a PATCH request. In this step, you'll replace the enriched fields on the SalesEvents channel with one enriched field: the Phone field.

Query the Channel ID

  1. In your fork of the Salesforce Platform APIs collection, expand Event Platform > Custom Channels, and then click List event channels.
  2. In the Params tab, for the q key value, append the following WHERE clause: WHERE DeveloperName='SalesEvents'. The full URI is: {{_endpoint}}/services/data/v{{version}}/tooling/query/?q=SELECT Id,DeveloperName,MasterLabel,ChannelType FROM PlatformEventChannel WHERE DeveloperName='SalesEvents'
  3. Click Send.
  4. The returned response includes the channel ID in the Id field and looks similar to the following example. Copy the Id value to use it in the next step.
{
  "size": 1,
  "totalSize": 1,
  "done": true,
  "queryLocator": null,
  "entityTypeName": "PlatformEventChannel",
  "records": [
    {
      "attributes": {
        "type": "PlatformEventChannel",
        "url": "/services/data/v58.0/tooling/sobjects/PlatformEventChannel/0YL8b000000sXvSGAU"
      },
      "Id": "0YL8b000000sXvSGAU",
      "DeveloperName": "SalesEvents",
      "MasterLabel": "Custom Channel for Sales App",
      "ChannelType": "data"
    }
  ]
}

Query the Channel Member ID

  1. In your forked collection, under Event Platform > Custom Channels, click List channel members.
  2. In the Params tab, for the q key value, append the following WHERE clause and replace <channel ID from query> with the Id you obtained from the previous step: WHERE EventChannel='<channel ID from query>' AND SelectedEntity='AccountChangeEvent'. The full URI looks as follows: {{_endpoint}}/services/data/v{{version}}/tooling/query/?q=SELECT Id,DeveloperName,EventChannel,FilterExpression,SelectedEntity FROM PlatformEventChannelMember WHERE EventChannel='<channel ID from query>' AND SelectedEntity='AccountChangeEvent'
  3. Click Send.
  4. The returned response includes the channel member Id in the Id field (for example, 0YL8b000000sXvSGAU) and looks similar to this example. Copy this Id value to use it in the next step.
{
  "size": 1,
  "totalSize": 1,
  "done": true,
  "queryLocator": null,
  "entityTypeName": "PlatformEventChannelMember",
  "records": [
    {
      "attributes": {
        "type": "PlatformEventChannelMember",
        "url": "/services/data/v58.0/tooling/sobjects/PlatformEventChannelMember/0v88b0000004CgVAAU"
      },
      "Id": "0v88b0000004CgVAAU",
      "DeveloperName": "SalesEvents_chn_AccountChangeEvent",
      "EventChannel": "0YL8b000000sXvSGAU",
      "FilterExpression": null,
      "SelectedEntity": "AccountChangeEvent"
    }
  ]
}

Make a PATCH Request on PlatformEventChannelMember

  1. In your forked collection, under Event Platform > Custom Channels > Change Data Capture, and click Add enriched fields to channel member.
  2. In the Params tab, add the channel member Id value you copied for the PLATFORM_EVENT_CHANNEL_MEMBER_ID parameter.
  3. Click Body and ensure that raw and JSON options are selected.
  4. Replace the body with this JSON body.
    {
      "FullName": "SalesEvents_chn_AccountChangeEvent",
      "Metadata": {
      "enrichedFields": [
        {
          "name": "Phone"
        }
      ],
      "eventChannel": "SalesEvents__chn",
      "selectedEntity": "AccountChangeEvent"
      }
    }
  5. Click Send.
  6. Verify that the response status is 204 No Content.
Note

The update process outlined the steps to do any update to enriched fields, which applies to adding, deleting, or replacing fields. If you're only deleting an enriched field, a simple way to do this is by performing a DELETE request on EnrichedField in Tooling API. First, get the Id of the enriched field to delete by performing a query like the one you did in the "Add a Channel Member and Configure Change Event Enrichment" step. Then, perform a DELETE request on /tooling/sobjects/EnrichedField/<Id>. You can use EnrichedField to delete a field but not to update it.

Subscribe to the Channel and Receive the New Enriched Field

Validate that the enrichment update was done correctly by subscribing to the custom channel, updating a record, and verifying the enriched fields array in the received change event. While still subscribed to the SalesEvents__chn channel, let’s update the account again.

  1. In your Trailhead Playground, switch to the tab for the account you created earlier.
  2. Edit the Type field and change it to Other.
  3. Save the record.
  4. Switch to the Streaming Monitor tab. Click the newest dot in the timeline to view the received event.
  5. The event payload includes the Phone field as an enriched field. It doesn't contain External_Account_ID__c or Industry because they're no longer enriched fields. The changedFields array in the received event message shows what fields changed.
{
  "LastModifiedDate": "2023-08-29T19:37:19Z",
  "Phone": "4155551212",
  "Type": "Other",
  "ChangeEventHeader": {
    "commitNumber": 11658991958431,
    "commitUser": "0058b00000HpWcRAAV",
    "sequenceNumber": 1,
    "entityName": "Account",
    "changeType": "UPDATE",
    "changedFields": [
      "Type",
      "LastModifiedDate"
    ],
    "changeOrigin": "com/salesforce/api/soap/58.0;client=SfdcInternalAPI/",
    "transactionKey": "0001577e-f890-0389-9d0d-c918449ad498",
    "commitTimestamp": 1693337839000,
    "recordIds": [
      "0018b00002Vlq7xAAB"
    ]
  }
}

In this project, you learned many things: how to create a channel, add members to enable notifications for objects, configure event enrichment, and subscribe to a channel to receive events. You used various tools to perform these steps: Postman and the Streaming Monitor app. Now you’re well-equipped to tackle Change Data Capture channels and event enrichment! 

Keep learning for
free!
Sign up for an account to continue.
What’s in it for you?
  • Get personalized recommendations for your career goals
  • Practice your skills with hands-on challenges and quizzes
  • Track and share your progress with employers
  • Connect to mentorship and career opportunities