Subscribing to Salesforce Platform Events in Workbench: A Step-by-Step Guide
Tags: salesforce, workbench, platformevents, pubsub
Published: 2023-01-28 18:44:31

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](#creating-platform-event)
- [Publishing platform event](#publishing-platform-event)
- [Subscribing to the platform event using Workbench](#subscribing-to-the-platform-event-using-workbench)
- [Demo](#demo)
- [Summary](#summary)
- [Let's connect!](#lets-connect)

## 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/6b2b1v0cq1e439sugiiz.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/kui57m2mftzn1nrl174u.png)
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. 

```java
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 : Trigger.new) {
            // 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
                    contactIds.add(c.Id);
                    // 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
                    events.add(event);
                }
            }
        }
        EventBus.publish(events);
    }
}
```

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](https://workbench.developerforce.com/) 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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/gwzir9lbym32d0fvkuq2.png)
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/69wk6lg2og54tkabr87x.png)
Everything is ready to see how our events work!

## Demo

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/h7q2vtjlkzjisi271xvg.gif)
You may take a look at the received message with the updated JobTitle__c on the following screenshot:

![Message received](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/pe3zvf2k4q1uj6cs26qb.png)
All fields are also filled as expected.

## Summary

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](https://www.linkedin.com/in/tdrnk/). 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!