MuleSoft: Salesforce Platform Events
Integration

Summer 2017 Salesforce released new event-driven architect “Platform Events” feature. Salesforce is known for its custom metadata platform, and now it is delivering a custom messaging platform, so Salesforce customers can build and publish their own events. Platform Events enables customer to increase business productivity and efficiency through integration via event. This feature reduces point-to-point integration and expands the existing capability with more integration options like Outbound Messaging, Apex Callouts, and the Streaming API. With platform events, there are two parties to the communication: a sender and a receiver. They are two of the components of an event-driven architecture.

Before going any further, let’s define some of terminology of platform event.

Event — A change in state that is meaningful in a business process. For example, if opportunities are created or updated in salesforce, this action will generate event within salesforce.

Event message – An event message is payload of event. For example, events are generated after creating or updating opportunities. So, this event has all updated data or updated delta of data which comes as payload.

Event producer – Publishing event with event message is event producer. For example, publish opportunities payload after generating event for other system.

Event channel — A stream of events on which an event producer sends event messages and event consumers read those messages.

Event consumer — A subscriber/Event consumer is an event channel that receives messages from the Event Bus. For example, Application which is subscribing event channel to process further is event consumer.

Event-based software architecture

Set Up Platform Events in Salesforce

  1. On the Salesforce page, click the Setup icon in the top-right navigation menu and select Setup.

    Salesforce setup link
  2. Enter Platform Events into the Quick Find box and then select Data > Platform Events.

    Salesforce platform event link
  3. Click New Platform Event.
  4. In the New Platform Event form, please fill all form
    1. Field Label: EnterpriseTestSync
    2. Plural Label: EnterpriseTestSyncs
    3. Object Name: EnterpriseTestSync

      Salesforce platform event configuration
  5. Click Save
  6. It will be redirected to the EnterpriseTestSync Platform Event page. By default, it creates some standard fields.

    Salesforce platform event standard fields
  7. Now you need to create Custom Platform Event fields that correspond to your EnterpriseTestSync. In the Custom Fields & Relationships section, click New to create a field for EnterpriseTestSync.
  8. Make sure that the Enterprise Test Sync API Name is EnterpriseTestSync__e and that Custom Fields & Relationships looks like this.

    Salesforce platform event API name
  9. If you have any trigger for platform event you can create in trigger section.
  10. Click Save.

Save action will create platform event in salesforce. In next section create Mulesoft integration flow

Integration Mulesoft and Plateform Event

To Integrate with Salesforce Platform Events, please download Mulesoft Salesforce connector v8.4.0 or beyond from Anypoint Exchange.

In my example, I am creating application which syncs  salesforce opportunity between two salesforce instances. So, any create or update opportunity will create platform event in salesforce instance. This platform event is subscribed by Mulesoft Salesforce platform event connector in first salesforce instance. Mulesoft  receives platform event and platform message from first salesforce instance. Mulesoft transforms  this platform message into another format of message and publishes into other salesforce platform event. Platform event can be tracked by replay id. Replay id is unique field when Salesforce generates any platform event. Platform event message persist only 24 hrs in platform Event Bus. We can replay this message within 24hrs.

Here are the steps for Mulesoft integration with Salesforce platform event  and  flow to communicate between two Salesforce platform event.

  1. Please configure Salesforce Basic Authentication from global element in Anypoint studio.

    Mulesoft – Salesforce Basic Authentication configuration
    1. Configure Salesforce connector for platform event which listen Salesforce platform event from event channel.
      • Select operation as “Replay streaming channel”
      • Streaming Channel: Add “/event/EnterpriseTestSync__e”. “EnterpriseTestSync__e” is API name from Salesforce platform event. This API listen event with /event/
      • Replay option: There are 3 options
        1. ALL – This option replays all message from event channel
        2. FROM_REPLAY_ID – This option replays only specific event message replay Id
    • ONLY_NEW – This option replay only new event messages from channel.
    • Replay Id: Replay option ALL we pass -1 value. For FROM_REPLAY_ID option we pass specific event message replay Id and for ONLY_NEW we pass -1
    • Check box “Resume from the Last Replay Id” resume from last replay Id and ignore rest.

      Mulesoft – Salesforce platform event subscribe configuration
  2. Once it is configured, it is ready to accept event message from platform event Channel. Add transformation logic to publish platform event into other Salesforce instances.
  3. Configure Salesforce platform event for publish event message into
    • Operation: Publish platform event message
    • Platform Event Name: Opportunity_Event__e
    • Platform Event Message: Default

      Mulesoft – Salesforce platform event publish configuration
    1. Once you configure these ends point application is ready to listen Event from first instance of Salesforce Platform event and publish platform event into other instance of Salesforce.
    Mulesoft – Salesforce platform event flow

    Here is flow of this application

  4. Here is code of this application
<?xml version="1.0" encoding="UTF-8"?>
<mule xmlns:tracking="https://www.mulesoft.org/schema/mule/ee/tracking" 
xmlns:dw="https://www.mulesoft.org/schema/mule/ee/dw"     
xmlns:sfdc="https://www.mulesoft.org/schema/mule/sfdc" 
xmlns="https://www.mulesoft.org/schema/mule/core" 
xmlns:doc="https://www.mulesoft.org/schema/mule/documentation"     
xmlns:spring="https://www.springframework.org/schema/beans"     
xmlns:xsi="https://www.w3.org/2001/XMLSchema-instance"     
xsi:schemaLocation="https://www.mulesoft.org/schema/mule/ee/dw https://www.mulesoft.org/schema/mule/ee/dw/current/dw.xsd
https://www.springframework.org/schema/beans https://www.springframework.org/schema/beans/spring-beans-current.xsd
https://www.mulesoft.org/schema/mule/core https://www.mulesoft.org/schema/mule/core/current/mule.xsd
https://www.mulesoft.org/schema/mule/sfdc https://www.mulesoft.org/schema/mule/sfdc/current/mule-sfdc.xsd
https://www.mulesoft.org/schema/mule/ee/tracking https://www.mulesoft.org/schema/mule/ee/tracking/current/mule-tracking-ee.xsd">

<flow name="bu-vanrish-eventFlow">

<sfdc:replay-streaming-channel config-ref="Salesforce__Basic_Authentication" streamingChannel="/event/EnterpriseTestSync__e" replayOption="ALL" replayId="-1" doc:name="Salesforce (Streaming)"/>

<logger message="Before   Transformation -- #[payload]" level="INFO" doc:name="Logger"/>
<dw:transform-message doc:name="Transform Message">
<dw:set-payload><![CDATA[%dw 1.0
%output application/java
---
{
Name__c: payload.payload.Name__c,
StageName__c:payload.payload.StageName__c,
CreatedById:payload.payload.CreatedById,
Amount__c:payload.payload.Amount__c,
Opp_ID__c:payload.payload.Opp_ID__c
}]]></dw:set-payload>
</dw:transform-message>

<logger message="After transformation -- #[payload]" level="INFO" doc:name="Logger"/>

<sfdc:publish-platform-event-message config-ref="Salesforce__Basic_Authentication_van" platformEventName="Opportunity_Event__e" doc:name="Salesforce"/>

<logger message="BU Vanrish complete flow-- #[payload]" level="INFO" doc:name="Logger"/>
</flow>
</mule>