Add a Filter with Metadata API
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 |
Before you add a filter, create a channel. Use PlatformEventChannel in Metadata API, and specify API version 56.0 or later.
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. For more information, see Metadata API Developer Tools and Quick Start: Metadata API in the Metadata API Developer Guide.
This sample custom channel definition is for the MyChannel__chn channel. The file name is MyChannel__chn.platformEventChannel. To have this channel accept platform events, event is specified for channelType.
<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventChannel xmlns="http://soap.sforce.com/2006/04/metadata">
<channelType>event</channelType>
<label>Custom Channel for Platform Events</label>
</PlatformEventChannel>
This channel member specifies the custom platform event and filter expression. This sample channel member definition associates the custom platform event to the channel and adds a filter expression. The file name is MyChannel_chn_Order_Event_e.platformEventChannelMember.
<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventChannelMember xmlns="http://soap.sforce.com/2006/04/metadata">
<eventChannel>MyChannel__chn</eventChannel>
<filterExpression><![CDATA[(City__c LIKE 'S%' OR City__c='New York') AND Amount__c>10.50]]>
</filterExpression>
<selectedEntity>Order_Event__e</selectedEntity>
</PlatformEventChannelMember>
<?xml version="1.0" encoding="UTF-8"?>
<PlatformEventChannelMember xmlns="http://soap.sforce.com/2006/04/metadata">
<eventChannel>MyChannel__chn</eventChannel>
<filterExpression><![CDATA[(ns__City__c LIKE 'S%' OR ns__City__c='New York') AND ns__Amount__c>10.50]]>
</filterExpression>
<selectedEntity>ns__Order_Event__e</selectedEntity>
</PlatformEventChannelMember>
This package.xml file references the channel and channel member.
<?xml version="1.0" encoding="UTF-8"?>
<Package xmlns="http://soap.sforce.com/2006/04/metadata">
<types>
<members>MyChannel__chn</members>
<name>PlatformEventChannel</name>
</types>
<types>
<members>MyChannel_chn_Order_Event_e</members>
<name>PlatformEventChannelMember</name>
</types>
<version>64.0</version>
</Package>
To update a filter expression, redeploy the package with an updated value for the filterExpression field in the PlatformEventChannelMember component. You can update only the filterExpression field of a channel member. All other fields aren’t updateable.