Subscribe to Salesforce Platform Events using MuleSoft

This blog introduces subscriptions of Salesforce platform events using the MuleSoft Salesforce Connector.

Arif Ali Mansuri
Another Integration Blog
4 min readFeb 18, 2023

--

Let’s have a look.

Prerequisites For the Integration:

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

What is a salesforce platform event?

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.

Source: ‘Rethinking Your Architecture With Platform Events’ by Florian Hoehn

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. Also known as event notification. An example of an event message is an order notification containing information about an order.

Event producer :

Entity that issued the event message.

Event channel :

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 that 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 orgs:

  • Step 1: Go to Setup and Search Platform Event Settings.
  • Step 2: New Platform Event
  • Step 3: Provide correct label and deployment type
  • Step 4: Create custom fields and relationships. (Optional)
  • Step 5: Save

Publish a platform event:

We can publish our platform events using flows / Apex / triggers and here we are publishing our platform events through salesforce flows.

Publishing Platform Events Using Flows from Flow Builder.

Steps for creating flows.

  • Step 1: Go to Setup
  • Step 2: Search for Flow
  • Step 3: Create a New Flow
  • Step 4: Select a Flow Type
  • Step 5: Add (Screen / Add Record)
  • Step 6: Save and Activate.
Types of flows in salesforce flows.

Types of flows we can create in salesforce orgs.

  • Screen flow : Provide a user interface to collect records from end users (client side)
  • Record-Triggered Flow: Fired when a record is created, updated, or deleted. This automatically started 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: Fired when a platform event message is received. This automatically started thread runs in the background.
  • Autolaunched Flow (No trigger): Executed when an Apex process, REST API, etc. is called, this automatically started thread runs in the background.
Creating flows using flow builder.

Now Subscribe platform event using salesforce connector in Mule Application.

  • Create a new Mule project in Anypoint studio.
New Mule Application in Anypoint Studio.
  • Add salesforce connector form Add Modules in mule palette.
  • Do the configuration for the Salesforce component with username ,password & security token.
  • Now check for listener that listens channel .

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.

  • Add logger with #[payload] in the flow.
  • Save & Deploy your mule application.

Publish event messages in platform event channel by flow for consuming in mule application.

Here the event message is subscribe in mule application see the below logs.

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
}

Thanks for reading the blogs follow for more such article.

Feel free to reach out to me.

--

--

Arif Ali Mansuri
Another Integration Blog

MuleSoft Mentor | Integration Engineer at NTT Data Services | Certified MuleSoft Developer | MCIA | MIA | MCDL2