Learn about Platform Events: Event-driven messaging capability for Salesforce Integrations
Salesforce introduced Platform Events as a part of Summer’17 release. The platform events are addition to other Salesforce integration capabilities such as Outbound Messaging, Apex Callouts and Streaming API. Considering that companies can no longer work solely with one application, this native feature provides an event-driven messaging architecture, enabling apps to communicate inside and outside of Salesforce.
There are five main components:
- The Pipe: adding events in chronological order
- A Window: duration
- The Events
- Publishers: internal or external apps that drive events into the pipe
- Consumers: those who can subscribe to events
Why Platform Events were introduced?
End users are interacting with companies through multiple channels like web, mobile, customer care centers, social media and so on. Behind all these channels, there may be multiple systems which are not necessarily in sync for customer data at all times. But customer expects to see correct and same information across multiple channels. This demands real-time sync between various systems to keep customer data up-to-date. Platform events capability is based on event-driven messaging architecture which enables Salesforce to send out information to external system when event occurs or consume event published by external system. This capability is beneficial in scenarios where such information sync is required in an application echo system where Salesforce is the key system.
What is event-driven messaging architecture?
Event-based (message-driven) communication involves event publisher and event consumer. Events are sent regardless of consumer listening or not. Consumers don’t acknowledge it when they receive an event. Event-based communication takes place in near-real time. Key component of event-driven architecture are Event Producer, Event Consumer, Event Channel, Event Message and Event. . The architecture is works well for large distributed systems as it decouples event producers from event consumers, thus simplifying the communication model in connected systems. An event-driven architecture helps in avoidance of point-to-point integration. It offers scalability and avoids tight coupling between systems.
Platform Events Business scenarios
Platform events enables bi-directional real-time integrations based on events. Salesforce can either act as an event producer or consumer.
Here are examples of business scenarios where platform events can be used:
Salesforce as Event Producer: Salesforce sends out notification to an external order fulfillment app about product shipment order.
Salesforce as Event Consumer: External order fulfillment app sends notification on shipment status for an Order.
Setting up Platform Event
Setting up platform event in Salesforce is very similar to creating custom object. The API name of platform event ends with ‘__e’.
To create a Platform event, follow the steps below.
- Within the Setup, enter Platform Events in the Quick Find box.
- Then select Platform Events and click New Platform Event.
- Enter Event Label, Plural label, description and Save.
- Once event is saved, you can create custom fields based on what all data will be part of event message.
Field types supported by Platform events are Checkbox, Date, Date/Time, Number, Text, and Text Area (Long).
ReplayId System Field
The events are stored by Salesforce for 24-hour period. Each event is assigned an ID in ReplayID system field. You cannot query events through SOQL or SOSL. You can see event records in reports, list views and search. You can retrieve past events only when subscribing in CometD and using a ReplayId option.
Publishing Platform Events
From Salesforce, you can publish events using an Apex method or with declarative tools, like Process Builder or Flow.
To publish event messages, you create an instance of the event and pass it to the EventBus.publish method.
// Create an instance of the event
Sample_Event__e orderEvent = new Sample_Event__e(
// Call method to publish events
Database.SaveResult sr = EventBus.publish(orderEvent);
Using Process Builder or Flow
To publish event messages using Process Builder or Flow, you can use the record creation functionality.
External apps use an API to publish platform event messages. You publish events by creating records of your event in the same way that you insert sObjects.
Consuming or Subscribing Events
On the Salesforce Platform, following tools allow subscribing to platform events – Apex triggers, processes, and flows.
Apex Triggers: You will need to write an after-insert Apex trigger on the event object to subscribe to incoming events.
Process Builder\Flow: To consume platform event though process builder, you will need to create a process that starts when a platform event occurs.
Benefits of Salesforce Platform Events:
Customers can run businesses faster on an event-driven architecture
- Create a full 360-degree customer experience – real-time integration with any business process
- Event-driven workflows to augment data
- Can capture and act on millions of streaming events
About the Author:
Niraj has extensive experience in Enterprise CRM applications, solution design, architecture and system integrations. 3x Salesforce Certified Professional, AWS Certified Cloud Practitioner and playing role of an application architect in his current assignment.
For more insights please feel free to connect with us on firstname.lastname@example.org.