Blog

Working with Salesforce Platform Events and MuleSoft

Sushmitha SiddamWritten by Sushmitha Siddam

Introduction

An Event is a change in state, anything that happens or takes place of importance in a business. Platform Event is a special kind of Salesforce Entity that notifies when an event occurs. Platform Event is based on Event-driven Architecture (EDA).

The paradigm of EDA revolves around the publisher-subscriber model – an event producer broadcasts an event that one or more event consumers listen to. The event producer publishes an event to a channel irrespective of whether or not a consumer is listening to it. On the other hand, event consumer subscribes to an event channel and are notified whenever a new state occurs. Any number of consumers can receive and react to the same events, which come almost in real-time. In this blog we will explore MuleSoft Salesforce Integration, concentrating on Salesforce Platform Events.

By definition,

“Platform events enable you to deliver secure, scalable, and customizable event notifications within Salesforce or from external sources via a Message bus.”

Whatever you want to be notified about, the Salesforce enterprise messaging platform enables it by creating platform events that notify other systems about the change via event message. An event message, also known as event notification, is an instance of a platform event that contains data about the event.

Use-Cases

Salesforce and external systems communicate through platform events.

  • A News agency sends an event to subscribed consumers with the latest breaking news about landslides or road conditions in a mountain getaway destination.
  • When a status changes in Salesforce, it notifies the external vendor app about a new shipment order it needs to process.
  • An external vendor app wants to send a merchandise return request to the Salesforce platform to process and trigger any other actions that need to be taken like informing the sales team or sending a confirmation email to the customer.
  • Notifying changes within Salesforce, for instance, when a new lead is assigned in Salesforce. A lead trigger fires and checks open cases and opportunities assigned to the lead owner.

Pre-requisites:

  • Create a Salesforce developer trial account at https://developer.salesforce.com/signup
  • Create a trial account in Mulesoft’s Anypoint platform at https://anypoint.mulesoft.com/login/signup
  • Download and install Mulesoft’s Anypoint Studio from https://www.mulesoft.com/lp/dl/studio

Create Platform Event in Salesforce

After logging in to the Salesforce Developer account, from ‘Setup’, enter ‘Platform Events’ in Quick Find Box, then select Platform Events in Integration.

Salesforce

  • On the Platform Events page, Click on New Platform Event.
  • Create a platform event by giving it a name and adding custom fields.
  • Let’s say NJC Labs wants to notify its subscribers whenever a new blog post is uploaded. We’ll create a platform event called ‘New NJC Blog’ and a custom field named ‘Blog Name’. Click on ‘Save’ to create the platform event.

Salesforce

  • When you create a platform event, the system appends ‘e’ suffix to the platform event’s label to create the API name of the event. In our example, for New NJC Blog event, the API name is NewNJCBloge.

Salesforce

  • In the platform event’s page, create a custom field ‘Blog Name’ by clicking on ‘New’ in Custom Fields & Relationships Section.
  • The first step to create a custom field is to select the Datatype. In our example we will select ‘Text’ datatype.

Salesforce

  • In the next step, give the custom field name, maximum length for the text field and description (optional) for the custom field. Click on ‘Save’ to create the custom field.

Salesforce

  • After creating the custom field, you’ll notice that ‘c’ suffix is added to the custom field’s name to create an API name for the field. In our example, the API name for Blog Name custom field is Blog_Namec.

Salesforce

Anypoint Connector for Salesforce

  • Create a new mule project called ‘newBlogEvent’ in Anypoint Studio.
  • To add Anypoint Connector for Salesforce, click on ‘Add Modules’ in Mule Palette and drag Salesforce to the palette to add and install the connector to the project. If you do not find the Salesforce Connector in the Palette, Add it from Exchange by clicking on ‘Search in Exchange’.
  • Go to Global Elements and create Salesforce Configuration by searching ‘Salesforce Config’. Provide the username, password and security token to create a Salesforce configuration. It is recommended you use placeholders for values and assign those values in a config file for the project.

Salesforce

Publish Platform event from MuleSoft

Create a flow named ‘publish-platform-event-flow’ which publishes an event to the platform event’s streaming channel. In our example, the event will be published to New NJC Blog platform event’s default streaming channel.

Salesforce

Consume Platform Event in MuleSoft

Create a new mule flow named ‘consume-platform-event-flow’ which subscribes to the streaming channel of ‘New NJC Blog’ platform event, that is /event/NewNJCBlog__e. When a new platform event is published to the channel, this flow is triggered, and it consumes the event message. This flow logs the payload of the event.

Salesforce

Paste the following code to configuration XML to create the above two flows:

Name_c": "Platform Events in Salesforce" }]]]>

Test Project

Run the project to test the two flows. After the project is deployed, send a HTTP Request to http://localhost:8081/publishPlatformEvent to publish an event to New NJC Blog platform event’s channel.

The publish-platform-event-flow publishes the event to the channel and logs the ID of the platform event. The consume-platform-event-flow consumes the event and logs the payload of the event. You will find something like below in the Console log.

Salesforce

Conclusion

This article explores how to publish and consume platform events using MuleSoft.

Another way of consuming Salesforce platform events in MuleSoft is by creating the platform event in Salesforce using a workflow, which would publish the event into a streaming channel.

Think about the different ways you can use Salesforce Platform Events with Salesforce Connector.

References