Metadata API Example: Create a Custom Channel and Add Real-Time Event Monitoring Events
User Permissions Needed | |
---|---|
To deploy and retrieve metadata types: | Customize Application |
To update metadata types: | Modify Metadata Through Metadata API Functions |
To use Metadata API: | API Enabled |
To use Real-Time Event Monitoring events: | Salesforce Shield or Event Monitoring add-on subscription and the View Real-Time Event Monitoring Data user permission. |
In this example, you create a channel for Real-Time Event Monitoring events named Event_Monitoring_Channel__chn by using the eventType field in PlatformEventChannel. Also, you add two Real-Time Event Monitoring events as members: ApiAnomalyEvent and FileEvent.
To create a channel and channel member with Metadata API, you can use tools such as Visual Studio Code with the Salesforce Extension pack or Salesforce CLI. See Metadata API Developer Tools and Quick Start: Metadata API in the Metadata API Developer Guide.
This sample custom channel definition is for the Event_Monitoring_Channel__chn channel. The file name is Event_Monitoring_Channel__chn.platformEventChannel. To have this channel accept Real-Time Event Monitoring events, event is specified for channelType and monitoring is specified for eventType.
<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventChannel xmlns="http://soap.sforce.com/2006/04/metadata">
<channelType>event</channelType>
<eventType>monitoring</eventType>
<label>Custom Channel for Real-Time Event Monitoring events</label>
</PlatformEventChannel>
This channel member definition associates ApiAnomalyEvent to the channel. The file name is Event_Monitoring_Channel_chn_ApiAnomalyEvent.platformEventChannelMember.
<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventChannelMember xmlns="http://soap.sforce.com/2006/04/metadata">
<eventChannel>Event_Monitoring_Channel__chn</eventChannel>
<selectedEntity>ApiAnomalyEvent</selectedEntity>
</PlatformEventChannelMember>
This sample channel member definition associates FileEvent to the channel. The file name is Event_Monitoring_Channel_chn_FileEvent.platformEventChannelMember.
<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventChannelMember xmlns="http://soap.sforce.com/2006/04/metadata">
<eventChannel>Event_Monitoring_Channel__chn</eventChannel>
<selectedEntity>FileEvent</selectedEntity>
</PlatformEventChannelMember>
This package.xml file references the channel and its two channel members.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>Event_Monitoring_Channel__chn</members>
<name>PlatformEventChannel</name>
</types>
<types>
<members>Event_Monitoring_Channel_chn_ApiAnomalyEvent</members>
<name>PlatformEventChannelMember</name>
</types>
<types>
<members>Event_Monitoring_Channel_chn_FileEvent</members>
<name>PlatformEventChannelMember</name>
</types>
<version>64.0</version>
</Package>
Before you subscribe to the Event_Monitoring_Channel__chn channel, enable Real-Time Event Monitoring and streaming for the specific events. See Enable Access to Real-Time Event Monitoring and Manage Real-Time Event Monitoring Events in Salesforce Help.
To subscribe to the channel, see Subscribe to the Channel and use /event/Event_Monitoring_Channel__chn for the TOPIC parameter. After you start the subscription, perform some actions in the Salesforce org to cause some events to be generated. For example, to receive FileEvent events on the channel, download a document.