Reading Time: 8 minutes

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

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

latest report
Learn why we are the Leaders in API management and iPaaS

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.

salesforce connector

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.

<?xml version="1.0" encoding="UTF-8"?>

<mule xmlns:dw="http://www.mulesoft.org/schema/mule/ee/dw" xmlns:http="http://www.mulesoft.org/schema/mule/http" xmlns:tracking="http://www.mulesoft.org/schema/mule/ee/tracking" xmlns:metadata="http://www.mulesoft.org/schema/mule/metadata" xmlns:sfdc="http://www.mulesoft.org/schema/mule/sfdc"  xmlns:doc="http://www.mulesoft.org/schema/mule/documentation"
xmlns:spring="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-current.xsd
http://www.mulesoft.org/schema/mule/core http://www.mulesoft.org/schema/mule/core/current/mule.xsd
http://www.mulesoft.org/schema/mule/sfdc http://www.mulesoft.org/schema/mule/sfdc/current/mule-sfdc.xsd
http://www.mulesoft.org/schema/mule/http http://www.mulesoft.org/schema/mule/http/current/mule-http.xsd
http://www.mulesoft.org/schema/mule/ee/tracking http://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd
http://www.mulesoft.org/schema/mule/ee/dw http://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd">
    <sfdc:config name="Salesforce__Basic_Authentication" username="${sfdc.username}" password="${sfdc.password}" securityToken="${sfdc.token}" url="" doc:name="Salesforce: Basic Authentication"/>
    <http:listener-config name="HTTP_Listener_Configuration" host="0.0.0.0" port="8081" doc:name="HTTP Listener Configuration"/>
    <flow name="Receive_platform_event_messagesFlow">
        <sfdc:replay-streaming-channel config-ref="Salesforce__Basic_Authentication"  doc:name="SF Platform Events"  streamingChannel="/event/Low_Ink__e" replayId="-1" replayOption="ALL" autoReplay="true"/>
        <logger message="Received an event: #[payload]" level="INFO" doc:name="Logger"/>
    </flow>
    <flow name="Publish_platform_event_messagesFlow">
        <http:listener config-ref="HTTP_Listener_Configuration" path="/publishMessages" doc:name="HTTP"/>
        <dw:transform-message doc:name="Transform Message">
            <dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
Ink_Percentage__c: 99.99,
Printer_Model__c: "application_network",
Serial_Number__c: "mule_ipo_2017"
}]]></dw:set-payload>
        </dw:transform-message>
        <sfdc:publish-platform-event-message config-ref="Salesforce__Basic_Authentication" platformEventName="Low_Ink__e" doc:name="Salesforce"/>
        <object-to-string-transformer doc:name="Object to String"/>
    </flow>
</mule>
salesforce mulesoft

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.”

salesforce anypoint
salesforce connector anypoint

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 Low_Ink__e. The connector in this demo app is configured to listen to the Low_Ink_e 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.

salesforce anypoint

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.

salesforce mulesoft

As soon as you create a Platform Event message, you will see the following message logged in Studio. (Note: Your result might be different.)

INFO  2017-10-26 16:39:21,846 [[salesforce_platform_events].Receive_platform_event_messagesFlow.stage1.02] org.mule.api.processor.LoggerMessageProcess
or: Received an event: {schema=ZPc9syIuukTisfMAuEe6nA, payload={CreatedById=005f4000000lQwAAAU, CreatedDate=2017-10-26T23:39:20Z, Printer_Model__c=Mul
e2017, Ink_Percentage__c=null, Serial_Number__c=null}, event={replayId=15}}

Let’s take a look at the second flow to see how the Salesforce Connector enables you to publish Platform Event messages in Mule.

salesforce mulesoft

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.

salesforce mulesoft

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.)

INFO  2017-10-26 16:47:37,712 [[salesforce_platform_events].Receive_platform_event_messagesFlow.stage1.02] org.mule.api.processor.LoggerMessageProcess
or: Received an event: {schema=ZPc9syIuukTisfMAuEe6nA, payload={CreatedById=005f4000000lQwAAAU, CreatedDate=2017-10-26T23:47:36Z, Printer_Model__c=app
lication_network, Ink_Percentage__c=99.99, Serial_Number__c=mule_ipo_2017}, event={replayId=16}}

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.