DEV Community

Cover image for Subscribing to Salesforce Platform Events in Workbench: A Step-by-Step Guide
Oleksandra Todorenko
Oleksandra Todorenko

Posted on

Subscribing to Salesforce Platform Events in Workbench: A Step-by-Step Guide

Salesforce platform events are a powerful tool for real-time data processing within the Salesforce ecosystem. Using platform events, you can easily transfer data of different types (with customizable fields) using a publish-subscribe model, where a sender (publisher) sends an event and one or more receivers (subscribers) receive the event.
Unlike sObjects, platform events can’t be queried using SOQL or SOSL, that is why we can use Workbench to subscribe to the specific Platform Event and track its validity.

In this article, we’ll discuss how to create platform events, their specifics and will subscribe to one of them using Workbench to see what data is received.

Table of Content

Creating platform event

To demonstrate how platform events work, let’s create an event that will be published when the Contact record changes (in the trigger) to let subscribers know that the contact has changed his job (Title or Department fields are changed).
To create a platform event, go to Setup -> Platform Events -> New Platform Event.

Platform events in setup
Here is an example of a created platform event. Please pay attention to the fields - we can add custom fields of different data types (Checkbox, Date, Date/Time, Number, Text, Text Area Long).
In our case, we need the following custom fields: Contact Company (AccountId), ContactId, Contact Name, Department and Job Title. These fields will be filled in the Contact Trigger.

Created platform event
Note: don’t forget to provide the required permissions to the users who will publish and subscribe to the events. It can be done by adding an object assignment to the custom profile or to the permission set that will be assigned to the specific users.

Publishing platform event

Platform events can be published from different places, such as:

  1. Flow
  2. Process Builder
  3. Apex
  4. External apps using API

Today we are going to consider the third option - publishing a platform event from Apex.
As we need to publish an event when the contact record has been updated with the new Title or Department, let’s create an Apex trigger on the Contact object. Below is the code of this trigger with the comments.

trigger ContactTrigger on Contact (before update) {
    // Create a static set to store the Contact Ids that have been modified
    private static Set<Id> contactIds = new Set<Id>();
    // Create a list to store the platform events
    List<ContactJobChange__e> events = new List<ContactJobChange__e>();

    if (Trigger.isBefore && Trigger.isUpdate) {
        // Iterate through the list of updated contacts
        for (Contact c : {
            // Check if the Contact Id is already in the set
            if (!contactIds.contains(c.Id)) {
                // Check if the Title or Department fields have been modified
                if (c.Title != Trigger.oldMap.get(c.Id).Title ||
                    c.Department != Trigger.oldMap.get(c.Id).Department) {
                    // Add the Contact Id to the set
                    // Create a new platform event
                    ContactJobChange__e event = new ContactJobChange__e();
                    event.ContactId__c = c.Id;
                    event.ContactCompany__c = c.AccountId;
                    event.ContactName__c = c.FirstName + ' ' + c.LastName;
                    event.Department__c = c.Department;
                    event.JobTitle__c = c.Title;
                    // Add the platform event to the list
Enter fullscreen mode Exit fullscreen mode

Please note: according to the best practices, it is preferable to keep trigger logic in the separate handler class. This example is simplified for understanding and contains the business logic inside the trigger.

Subscribing to the platform event using Workbench

When the trigger logic is done, we can proceed to the subscription to the platform event.
To do this, go to Workbench and log in using Salesforce.
Navigate to queries -> Streaming Push Topics. Then select the "Generic Subscriptions" tab and in the "Subscription" field enter the name of your platform event in the following format:
/event/EventName__e. If you are using an event with a namespace, the format will be the following: /Namespace__event/EventName__e.
For the ContactJobChange event, I have the following value: /event/ContactJobChange__e.

Streaming Push Topics in Workbench menu
If everything is set correctly, after filling in all the fields and pressing the "Subscribe" button, you will receive the message that confirms a successful subscription:

Subscribed to event
Everything is ready to see how our events work!


Below is a detailed demonstration of how our contact has been updated and event message was received in a real-time:
platform event subscription demonstration
You may take a look at the received message with the updated JobTitle__c on the following screenshot:

Message received
All fields are also filled as expected.


In this article, we discussed the concept of platform events in Salesforce and how they can be used to track changes in business processes. We also covered how to subscribe to platform events using the Salesforce Workbench tool. We provided step-by-step instructions on how to create a platform event, subscribe to it, and view its data in Workbench. By following these steps, you can easily subscribe to platform events in Workbench, taking full advantage of the real-time data processing capabilities of Salesforce.

Lets connect!

If you found this article informative and useful, I would love to connect with you on LinkedIn. I am always happy to expand my network and connect with individuals who share a passion for Salesforce. Feel free to reach out to me with any questions or feedback regarding the article, I am more than happy to engage in a conversation and provide further assistance. So don't hesitate to connect with me and let's learn and grow together in the Salesforce community!

Top comments (0)

Here is a post you might want to check out:

Regex for lazy developers

regex for lazy devs

Sorry for the callout 😆