Create and consume a Salesforce Platform Event using Mule 4 with no code
The world of data is moving faster than ever; to keep up with it demands agility and real-time responsiveness. Salesforce Platform Events are here to help by enabling real-time data notifications across your Salesforce org and beyond. And guess what? Mule 4 makes integrating and leveraging these events a breeze!
In this article, I will explain in just a few steps how we can start sending data from Salesforce into Mule. Mule may then use that data for consuming other services or may simply process that data in an asynchronous way.
Create the Platform Event object in Salesforce
We start by creating our platform event within Salesforce. Click the gear in the top right of the page to go into Setup. Type “Platform Event” in the search bar to bring up the Platform Events Setup.
From there, simply click on “Create new Platform Event” and populate the required fields; in this case, just give a meaningful name to the platform event. Click on “Save” to proceed.
In the Custom Fields and Relationships section, let’s create a couple of fields we can use to map Account Name and Account Description:
We have specified here the two fields from Account which we want to send over in our platform event. The API names for these two custom fields are Account_Description__c and Account_Name__c.
Let’s move on to the next configuration step.
Create the Platform Event using Flow
The platform event has been created, and we have a couple of fields we can use from the event. Now we need a way to push the data into the platform event every time an Account is created or modified; Salesforce Flow is going to help us to do just that.
From the search bar, type “Flow” to bring up the Flows Setup. Click on “New Flow”:
Select Record-Triggered Flow, and we should then be able to see the canvas to start setting up our process.
We start by selecting the Account object. For the Trigger, select “A record is created or updated”, and make no other changes for now.
Next, we click the + sign to add a new element to the flow. Type “create” to search for and select the “Create Records” action:
Now we need to give a name to our action. For the Object type to Create a Record of, select Account Platform Event:
On the left-hand side of the Field section, we select the couple of fields we previously created in the Platform event, Account_Name__c and Account_Description__c. On the right-hand side, we map values to the fields using this notation like this: {!$Record.Description}
The completed flow should look like the following:
Click on the Save button to commit the changes.
At this point, anytime a new Account gets created or updated, details regarding the change are collected and transmitted as a platform event.
The Mule application
Next, we hop into Anypoint Studio to create our Mule 4 application with a new flow to listen to a Salesforce channel. From the Mule Palette, simply click the Salesforce component and then type “replay” in the search box to narrow down the available operations. Click and drag the Replay channel listener operation to the application canvas.
We can then specify the name of our Streaming channel for the Salesforce Platform Event API as /event/Account_Platform_Event__e:
And that’s all there is to it to get started! After we have deployed our Mule application and it is up and running, we may then go browse its log after we have created or updated an account record in Salesforce.
There are many possibilities from which you can take it from here.
I hope this helps to demonstrate how fast and easy it is to create such an integration between Salesforce and Mule, and then set it off and running.