Subscribe to Salesforce Platform Events using MuleSoft

  • February 21, 2023

This technical article introduces subscriptions to Salesforce Platform Events using the MuleSoft Salesforce connector.

Prerequisites for the integration:

  • MuleSofts’s Anypoint Studio 7. X
  • Salesforce Orgs Credentials
    • Salesforce Access Token
    • Username
    • Password

What is a Salesforce Platform Event?

A Salesforce Platform Event is a message-driven (or event-driven) software architecture consisting of event producers, event consumers and channels. This architecture decouples event producers from event consumers and simplifies the communication model for connected systems, making it suitable for large distributed systems.

Salesforce Platform Event

Event

A significant state change in a business process. For example, an order is a significant event because fulfillment centers expect notification before processing an order.

Event message

A message containing information about an event is also known as an event notification. An example of an event message is an order notification containing information about an order.

Event producer

This is the entity that issued the event message.

Event channel

This is a stream of instances. Instance producers send event messages, and event consumers read those messages. A platform event channel can be dedicated to a single platform event or customized to group event messages from many platform events.

Event consumer

A consumer subscribes to a channel and receives communications from the channel. Consider an order processing app that receives notifications of new orders.

The Event Bus

Platform Event messages are posted to the event bus where they are stored temporarily. You can retrieve stored event messages from the event bus using the Streaming API (CometD) or Pub/Sub API clients. Each event message contains a ReplayId field that identifies the event in the stream and allows the stream to be replayed after a specific event. For more information, see the “Message Reliability” section of the Streaming API Developer Guide.

Let’s create a Platform Event in the Salesforce org.

Steps to create a platform event in Salesforce org:

  • Go to Setup and search Platform Event Settings
  • New Platform Event
  • Provide the correct label and deployment type
  • Create custom fields and relationships (optional)
  • Save

Publish a Platform Event

We can publish our Platform Events using flows/Apex/triggers. Here we are publishing our Platform Events through Salesforce flows.

Publishing Platform Events using flows from Flow Builder:

Steps for creating flows:

  • Go to Setup
  • Search for Flow
  • Create a New Flow
  • Select a Flow Type
  • Add (Screen/Add Record)
  • Save and Activate

Flow Builder

  • Screen Flow: This provides a user interface to collect records from end users (client side).
  • Record-Triggered Flow: This is fired when a record is created, updated or deleted. It automatically starts thread runs in the background.
  • Schedule-Triggered Flow: It runs frequently and at specific times for each item in the batch.
  • Platform Event-Triggered Flow: This is fired when a platform event message is received. It automatically starts thread runs in the background.
  • Autolaunched Flow (No trigger): This is executed when an Apex process or REST API is called. It automatically starts thread runs in the background.

Autolaunched Flow

Now, subscribe to Platform Events using the MuleSoft Salesforce connector.

Create a new Mule project in Anypoint studio.

New Mule project

Add the Salesforce connector form and add modules in the Mule palette.

Do the configuration for the Salesforce component with username, password and security token.

Salesforce component configuration

Salesforce component configuration

Replay Channel Listener

Subscribe to streaming channels. This source provides channel-related notification settings for new events and events stored by Salesforce within a 24-hour retention period. A flow is triggered when an event occurs on the specified channel source.

Replay Channel Listener

Add logger with #[payload] in the flow.

Save & deploy your Mule application.

Publish event messages in the platform event channel by flow.

Platform event channel by flow

Platform event channel by flow

Here the event message is subscribed in the Mule application. See the logs below.

INFO  2023-01-03 14:07:05,941 [[MuleRuntime].uber.01: [test1-project].test1-projectFlow.CPU_LITE @42aba0f7] [processor: test1-projectFlow/processors/0; event: cd967c40-8b41-11ed-a951-c025a540da68] org.mule.runtime.core.internal.processor.LoggerMessageProcessor: 
{ "data": {"Company_ID__c": "teste",  "CreatedById": "0055j0000022lPnAAI", "Billing_Account_Id__c": "teste", "CreatedDate": "2023-01-03T08:36:58.492Z", "User_Id__c": "teste","Org_Id__c": "teste"}, "replayId": 15451878, "createdId": null}

References

https://help.salesforce.com/s/articleView?id=sf.flow.htm&type=5

https://developer.salesforce.com/docs/atlas.en-us.platform_events.meta/platform_events/platform_events_intro.htm

— By Arif Ali Mansuri