Subscribe to Salesforce Platform Events using MuleSoft
If we want to connect business processes in Salesforce with external apps through the exchange of real-time event data, we can use platform events. Platform events are secure and scalable messages that contain data and are part of Salesforce’s enterprise messaging platform. The platform provides an event-driven message architecture that revolves around Publish-Subscribe model, and this enables salesforce to integrate with other systems in near-real time.
Below are some important concepts that are heavily used in any event-driven architecture:
- Event: it’s a change in state that is significant in a business process
- Event message/notification: it’s a message that contains data about the event. The events are meant to just notify a state change. They are just telling that something has happened
- Event producer: the publisher of an event message over a channel
- Event consumer: a subscriber to a channel that receives messages from the channel
- Event bus: it provides a communication and event storage service that enables event streaming using the pub-sub model. It is also used for the retrieval of stored events at any time during the retention window
With event-driven model, the event producers decouple from the event consumers, in order to simplify the communication model in connected systems. No request needs to be sent to a server to obtain information about a certain state. Instead, a system subscribes to an event channel and is notified whenever new states occur. When an event occurs, systems obtain this information and can react to it in near-real time.
Except for the semantics of the message content, systems that send the events and those that receive the events don’t have dependencies on each other. Publishers and subscribers communicate with each other through platform events. One or more subscribers can listen to the same event and carry out actions. In this case, MuleSoft can be a subscriber that subscribes to the platform event to ingest the message event and sync the change with other systems like databases or writing a file to SFTP locations for further data processing.
We can publish and consume platform events using REST or SOAP API, or declarative tools in salesforce such as flows and process builder or using Apex.
Platform events are special kinds of entities similar to custom objects with the following limitations:
- We can’t view the event records in the salesforce user interface
- We can’t update or delete platform event records
- The Platform events do not have page layouts
- The Platform events are limited to data types such as checkbox, date, date/time, number, text, and text area (long).
Having said that about event-driven architecture in general and the Salesforce platform event in particular, let’s dive deep into how to create and define a platform event in Salesforce, and then how MuleSoft subscribes to the platform event to access the event message.
Create and Define Platform Event in Salesforce
To get started, let’s first create a custom checkbox field “Over $150000****” in the opportunity object then create and define a platform event in Salesforce. We need this field to create the platform event when it’s set to true.
To create the platform event in Salesforce, go to Setup and search for Platform Events then click on New Platform Event
Give a proper name for the platform event, in this case let’s name it as “Opportunity over $150000” and create a custom field called Name field of data type text. As discussed above, in the Salesforce platform events, we can only use limited data types such as checkbox, date, date/time, number, text, and text area (long). Here, the Name field will be mapped with the opportunity name during the process builder creation.
The platform definition will look something like this:
Create Process Builder
Once we create the platform event definition, the next step is to create a process builder that will create the platform event when the “Over $150000” checkbox is set to true on the opportunity object. To do this, go to Setup and search for Process Builder, then click on the new button to start creating the process builder.
Let’s add Opportunity as an object where the process will be created and triggered when a record is created or edited.
Then we will check if the opportunity is meeting the criteria which is needed to create the platform event.
Next, we will define immediate actions to create a record for “Opportunity over $150000” platform event that we've created earlier, and we will map the name field from the opportunity record.
Go ahead and activate the process. At this point, the Salesforce setup is complete.
Now let’s go to the MuleSoft side and create a new project to subscribe to the platform events in Salesforce.
How MuleSoft Subscribes to the Salesforce Platform Event
The input source acts as an inbound endpoint. Every time the subscription receives an event, the input source executes the rest of the flow in the Mule application. The following prerequisites need to be satisfied to follow:
- Install a Salesforce connector
- Salesforce developer account
- Security token
- Consumer key and secret (if we are using the Salesforce connector to access an OAuth API)
Go ahead and drag the “Subscribe channel listener” processor from the Mule palette into the Mule flow.
Configure the connection details for Salesforce. For this example, we will use Basic Authentication, you can also use other connections as well to do the configuration. We will use the same credentials as the Salesforce org we created the platform event in and test it to check if the connection has been established successfully.
In the Subscribe Channel Listener, under Streaming Channel, click the refresh button to pull down the Platform Event we created in our Salesforce org. In the drop-down we should see the platform events created in the org. In this case we see “Opportunity over $150000 (Opportunity_over_150000__e) (/event/Opportunity_over_150000__e)” - select that streaming channel. Then drag and drop Transform Message from the Mule Palette into our flow to read and transform the data and add logger component to print data in the console.
We will read the data like this:
Once the application is deployed, we can go to Salesforce and create a test opportunity. In this case, we created “Dreamers Camp” with the amount greater than $150K. Make sure we populate the opportunity name field. Tick the “Over $150000” check box and save it. Mule will receive and process the event message from Salesforce.
The logger component should log the payload coming from the Platform Event to the console as shown below:
With event-driven architecture, the event publishers and the event subscribers are decoupled. This makes the communication, maintenance, and complexity of the independent systems easier.
When the business requirement allows, it is possible for systems to subscribe to an event channel and get notified whenever there is a state change to be captured instead of sending a request to the server to obtain information about a particular state. MuleSoft can subscribe to the Salesforce platform events to capture the state change, and based on the business needs, we can sync the data in near-real time or write the file to the SFTP location for further data processing. This makes the data integration or synchronization process smoother and faster instead of writing complex queries and sending requests to the server to get the data.
For this to happen, we can leverage the two Salesforce Connectors in Mule 4 such as Subscribe Channel Listener and Subscribe Topic Listener.