Subscribing Salesforce Platform Events Using MuleSoft

Gursimran Singh Ranhotra
Another Integration Blog
5 min readJun 8, 2023

Platform Events enable you to deliver secure, scalable, and customizable event notifications within Salesforce or through external sources. Platform Event fields are defined in Salesforce and they determine the data that you send and receive.

This article describes the method to subscribe and read the Salesforce Platform Events using the MuleSoft Salesforce connector.

Salesforce platform event is a software architecture that is message-driven (or event-driven) & consists of event producers, event consumers, and channels. This architecture separates event producers and event consumers and simplifies the communication model for connected systems, making it suitable for large distributed systems.

You can use Anypoint Connector for Salesforce (Salesforce Connector) as to stream data from Salesforce into your application. You can use any of the below connectors depending on your use-case:

  • Subscribe topic listener
  • Subscribe channel listener
  • Replay topic listener
  • Replay channel listener

Terminologies used-

Event

A significant change in the state of a business process. An order, for example, is a significant event because fulfilment centres anticipate notification before processing an order.

Event message

Event message is a message which contains all the information about an event . An example of an event message is an order notification that contains information about an order.

Event producer

The entity which issued the particular event message.

Event channel

A stream of all instances. Event messages are sent by instance producers, and event consumers read them. A platform event channel can be dedicated to a single platform event or customised to group event messages from multiple platform events.

Event consumer

A consumer subscribes to a channel and receives inputs from it.

Steps:

  1. Login to Salesforce and create a Salesforce Platform Event. Please follow the example in the Trailhead docs to create a Platform Event.
    In this use case, we have created an event named-
 “OQ_RFC__e”.

Steps-

Let’s create a Platform Event in the Salesforce org.

  • Go to Setup and search Platform Event Settings
  • Create New Platform Event
  • Provide the correct label and deployment type
  • Create custom fields and relationships (optional)
  • Save
  • We can publish our platform events using flows/Apex/triggers

We will then test the event and make any necessary changes before proceeding with our Mulesoft configuration.

(Here in this use-case, the event gets triggered if we click on the SF button- “Account”)

2. Create a Mule application and in source section, keep the connector-subscribe channel listener.

After you create a streaming channel, you will start receiving events by subscribing to the channel. The Subscribe channel listener source acts like an inbound endpoint. In this example, every time a subscription to (OQ_RFC__e) receives an event, it executes the rest of the flow and logs a message at the INFO level.

Note-

  1. The Streaming channel field of the Subscribe channel listener operation doesn’t display the change events that are available in the Salesforce environment. However, this connector can subscribe to a streaming channel to obtain this information.
  2. You can also use a Replay Channel Listener which works in similar way with some extra configurations and features as described in the image below-

The differences between this and streaming channel listeners are described at the end of the article.

3. The event should be defined in all cases like —

(/event/<event name>)

3. You need to establish a connection with the Salesforce system.

If connecting with Basic authentication, you need to just give the username, password, and its token, authorization URL-

  • If using connection as Oauth JWT, give the following credentials-

Test the connection and you can validate, if it’s successful-

4. Keep component Subscribe Channel, then specify event name as Streaming channel.
(It should be in source of your Mulesoft Flow).

In Streaming channel field , give your event name.
It is (/event/OQ_RFC__e) according to the current use case.

5. And on hitting the record button, you can see that it successfully reads the event and populates its data.

We can see in the payload that all of the required fields and data are present after hitting-

{
“data”: {
“schema”: “RAVRAV6NuYRxDIm*****”,
“payload”: {
“CreatedById”: “0055r000003e*****”,
“CreatedDate”: “2023–06–07T10:51:46.840Z”,
“OQ_ERPId__c”: “12345”
},
“event”: {
“EventUuid”: “0f5d75d2–8138–468a-8613–293*****c”,
“replayId”: 4149775,
“EventApiName”: “OQ_RFC__e”
}
},
“channel”: “/event/OQ_RFC__e”
}

You can populate the required fields from this payload.
(Like schemaName, replayId, channelName, eventName, erpId, eventUuid, createdById, createdDate etc.)

Pfa the data getting populated into SF UI through event after processing through Mulesoft-

Note-

  1. You can also use Replay Channel Listener to Subscribe to streaming channels. This source provides channel-related notification settings for new events and events stored by Salesforce within a 24-hour retention period. A flow is triggered when an event occurs on the specified channel source.
  2. Subscriber Channel Listener is more faster than Replay Channel Listener as latter internally uses ObjectStore V2 to store automatic message replay and message redelivery.
  3. The Resume from the Last Replay Id checkbox in Replay Channel Listener enables you to specify an automatic replay of stored events, based on the Replay ID of the last event processed by the connector which is not present in Subscriber Channel Listener.

Thanks for reading!
Please feel free to comment and follow me for more helpful articles and insights in the future.

--

--