How to Use Platform Events With MuleSoft's Salesforce Connector
Learn how to use the Anypoint Connector for Salesforce for integration, and the difference between Platform Events and Streaming API Events.
Join the DZone community and get the full member experience.Join For Free
Salesforce introduced Platform Events with its Summer '17 release (API v40). While the Anypoint Connector for Salesforce has supported Streaming API Events, the following description from Salesforce might be helpful to understand the difference between Platform Events and Streaming API Events.
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. Apps can publish and subscribe to Platform Events on the Force.com Platform using Apex or using CometD in external systems.
Streaming API Events
The Streaming API provides two types of events that you can publish and subscribe to: PushTopic and generic. PushTopic events track field changes in Salesforce records and are tied to Salesforce records. Generic events contain arbitrary payloads. Both event types don't provide the level of granular customization that Platform Events offer. You can send a custom payload with a generic event, but you can't define the data as fields. You can't define those types of events in Salesforce, and you can't use them in Apex triggers.
Anypoint Connector for Salesforce
In order to provide granular customizations for MuleSoft users, we are excited to announce the release of the Anypoint Connector for Salesforce v8.4.0, which supports receiving and publishing Platform Event messages. In this blog post, I'd like to walk you through a simple demo app, which shows you how to receive Platform Events and publish Platform Event messages with the Salesforce Connector.
Before you start with the demo app, please make sure you download the Salesforce Connector v8.4.0 from Anypoint Exchange.
After you install the Salesforce Connector, copy and paste the following XML in your Mule project, and you will see the two Mule flows in the project.
Once you load the example, let's configure the connector. To configure the Salesforce Connector, go to Global Elements, and find "Salesforce." After selecting "Salesforce," click "Edit."
In the "Salesforce: Basic Configuration," you can specify the configuration for Salesforce. You can directly add your Salesforce information in the configuration, but we recommend using the properties file to add your configuration information.
In mule-app.properties, please configure the following properties based on your Salesforce instance.sfdc.username= sfdc.password= sfdc.token=
Now, it is time to define a Platform Event which the Salesforce Connector will listen to. For this demo app, I followed the Salesforce instructions to create and define the Platform Event called The connector in this demo app is configured to listen through the "Replay streaming channel" operation.
As Salesforce stores the Platform Event messages in 24 hours, you can replay the messages with "Replay Id." Please take a look at the previous blog post for more information.
Now you are ready to receive the platform messages from Salesforce. After you run the demo app, please go to Salesforce Workbench to publish Platform Event Messages to the Low_Ink_e.
As soon as you create a Platform Event message, you will see the following message logged in Studio. (Note: Your result might be different.)
Let's take a look at the second flow to see how the Salesforce Connector enables you to publish Platform Event messages in Mule.
When you select the "Publish platform event message" operation, the connector shows a list of Platform Events in your Salesforce instance and the metadata for the selected Platform Event. Based on the metadata, I can use DataWeave to create a platform event message in a simple way.
As soon as you hit localhost:8081/publishMessages with Postman or a browser, you will see the following logged message in Studio. (Note: Your result might be different.)
For new users, try the above example to get started, and for others, please share with us how you plan to use the Platform Events with the Salesforce Connector!
Also, be sure to explore Anypoint Exchange to see other resources you can leverage today.
Published at DZone with permission of , DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.