The Maven Tools Event Hub module enables users to subscribe to specific events in Salesforce. For those who may not be very familiar with events, they essentially represent events that take place and hold significance for the user, such as the completion of an assigned task. Salesforce features various types of events, each with its own advantages and disadvantages: Generic Events, Platform Events, Push Topics, and Change Data Captures. We won't delve deeply into the specifics of each event type, but you can refer to the following pages in Salesforce Help to gain a more comprehensive understanding:
Currently, Event Hub supports Generic Events, Platform Events and Push Topics. Change Data Captures will be added in a later release.
Before we show you how subscribing to events in the Event Hub is done, let's have a quick rundown of the module:
Now that we've showed you what Event Hub is like, we can continue and create a Platform Event. We've created a Platform Event called 'HelloWorld' which we will use in this example.
Now we need to publish our event, and we'll do that by executing this code in the Execute Anonymus window:
// Create an instance of the event and store it in the newsEvent variable
Hello_World__e testEvent = new Hello_World__e(
Description__c='Testing');
// Call method to publish events
Database.SaveResult sr = EventBus.publish(testEvent);
// Inspect publishing result
if (sr.isSuccess()) {
System.debug('Successfully published event.');
} else {
for(Database.Error err : sr.getErrors()) {
System.debug('Error returned: ' +
err.getStatusCode() +
' - ' +
err.getMessage());
}
}
By executing this code, we have successfully published our event. Let's now head back to Maven Tools and subscribe to the 'HelloWorld' Platform Event. For Event Type choose Platform Event, choose the 'Hello_World__e' event and type in '-1' (New Events) as the Replay Option. Click 'Subscribe'.
We have now successfully subscribed to the 'HelloWorld' Platform Event. As you can see, new pull requests are made all the time to check if there are any new events published. Also, you can see that there are currently no events showing in the response, and that's because we haven't published any since subscribing (we subscribed only to new events). Execute the same code again in the Execute Anonymus code, and you should see an event pop up in the response.
The new event is currently displayed in the response. However, what if we desire the event that was published before we subscribed? This is where the Replay Option '-2' (All Events) comes into play. Salesforce retains high-volume platform events in the event bus for 72 hours. Consequently, we can retrieve events that were published prior to our subscription. To do so, click 'Unsubscribe,' modify the Replay Option to -2, and then subscribe again.
Now the response should return both events that we published from the Execute Anonymus window.
Along with the two subscription types we just showed you, you can also choose a ReplayId from a specific event as the Replay Option. The response will then contain all the events that happened after that event.
Generic Events are another type of event used to send custom notifications that are not tied to Salesforce data changes. To use Generic Events, we first need to create a Streaming Channel. We'll create a new '/u/HelloWorld' Streaming Channel from the Salesforce UI:
Streaming Channel names need to start with the '/u/' prefix
Now that we've created our streaming channel, we can go and subscribe to it in the Event Hub. Select Generic Events as the Event Type, select 'HelloWorld' as the Streaming Channel, and type in '-1' as the Replay Option to receive new events.
Now that we've subscribed to the 'HelloWorld' Streaming Channel, we can go and check out how it works. Generic Events can be published through the REST API, and we can use the Maven Tools Rest Console to publish an event to our channel and see how Generic Events work. We'll open the Rest Console module, and execute the following request:
{
"pushEvents": [
{
"payload": "Broadcast message to all subscribers",
"userIds": []
}
]
}
Leave the request header as it is by default and execute the request. Now if we go back to the Event Hub, we can see that our subscription captured the event we've just published.
PushTopic events provide a secure and scalable way to receive notifications for changes to Salesforce data that match a SOQL query you define. Event Hub lets you create, edit, delete and subscribe to Push Topics. For example, let's create a PushTopic that subscribes to updates on Name, Phone and Industry fields on all the Account records. We'll name our PushTopic 'AccountUpdates' and use the following simple query:
SELECT Id, Name, Phone, Industry
FROM Account
Every time someone updates one of the fields specified in the SELECT clause on any record that meets the query criteria, a notification will be streamed to the PushTopic channel. The query always needs to contain the Id field, even though its value is not editable.
After selecting the PushTopic API version, click the 'Save' button to save the PushTopic and then click 'Subscribe'.
Now we can go ahead and change any of the fields specified in the SELECT clause on any Account record. When we do that, a new event will pop up in the Event Hub: