Platform Events

Platform Events are used to deliver secure, scalable, and customizable notification within Salesforce or external app. Platform Event is based on Event-Driven Architecture.

Introduction: 

Platform Events are used to deliver secure, scalable, and customizable notification within Salesforce or external app. Platform Event is based on Event-Driven Architecture. This is built in real time integration patterns in the Salesforce Platform which helps to reduce point-to-point integration.

Publishing platform event: 

We can publish the platform events in 3 ways:

  1. Publish Events Messaging using APEX.
  2. Publish Events Messaging using Declarative tools (Process Builder or Cloud Flow Designer Tool / Visual Work flow).
  3. Publish Events Messaging using Salesforce API from external app.

Not supported in Platform event: 

  1. The allOrNoneHeader API header is ignored when you publish platform events through the API.
  2. The Apex setSavepoint() and rollback() Database methods aren’t supported with platform events.

Subscription in Platform Event: 

  1. Apex triggers receive event notifications.
  2. write an “after insert” Apex trigger on the event object to subscribe to incoming events.
  3. Triggers receive event notifications from various sources—whether they’re published through Apex or APIs.
  4. Visualforce and Lightning component apps receive events through CometD.
  5. CometD is a scalable HTTP-based event routing bus that uses an AJAX push technology pattern known as Comet.
  6. In an external app, you subscribe to events using CometD as well.

Defining Objects and fields in Platform Events: 

Platform events can be created just like the custom objects. The biggest difference between Platform events and Custom object is suffix name for the api name. In Platform events, its api name suffix with __e where the custom object appends __c suffix to create api name. Unlike with custom objects, we cannot update or delete event record, or view event records in Salesforce User Interface.

Platform Events support only following custom fields type,

  1. Checkbox
  2. Date
  3. Data/Time
  4. Number
  5. Text
  6. Text Area (Long)

ReplayId System Field and Event Retention

  1. Salesforce stores platform events for 24 hours. You can retrieve stored events in API clients but not in Apex.
  2. Each event record contains a field, called ReplayID, that the system populates after the event is published.
  3. Each replay ID is guaranteed to be higher than the ID of the previous event, but not necessarily contiguous for consecutive events.
  4. You can retrieve all stored events, or you can specify the replay ID of an event as the baseline for the retrieved portion of events.

Steps to Create and Publish Platform Events:

  1. From Setup, Search Platform Events and Click on the Platform Events.
platform events salesforce
  1. Click on New button to create Platform Events.
  1. Provide the name for the Platform Events.
platform events salesforce
  1. Create a custom field to show the Notification from this field.
platform events salesforce
  1. Create an Apex Class to publish the Platform Event

NotificationController.cls

platform events salesforce
  1. Create a Lightning Component to subscribe the Platform Event through cometd JavaScript client and get the real-time notification through this component.

helpNotification.cmp:

platform events salesforce

helpNotificationController.js

platform events salesforce

helpNotificationHelper.js

platform events salesforce
  1. From Setup > App Manager > Click Edit on Service Console App.
platform events salesforce
  1. Click on Utility Bar Tab. Click on Add button and choose helpNotification component to add lightning component to Service Console App.

v

  1. Provide the component name as Help and check Load in background apps open option.
  1. Go to the Service Console App. You will see the notification as shown below and Help utility bar.
platform events salesforce
  1. Click on any case record. In Chatter feed, Add #Help with your custom message in chatter feed.
platform events salesforce
  1. Upon posting the message with #Help, it will show the notification on which the platform events have been published. Also, you will get notification in Help Utility bar.
platform events salesforce

Conclusion: 

It is no longer applications are pulling out from multiple endpoints and polling to retrieve the data. We can get the data by subscribing to the platform event. It will help create 360-degree customer experience to get the information when the data changes.

References: 

Platform Event Developer Guide

Platform Events Basics

Build an Instant Notification App

About MST

At MST Solutions our cornerstone is to adapt, engage and create solutions which guarantee the success of our clients. The talent of our team and experiences in varied business verticals gives us an advantage over other competitors.

Recent Articles

Work with us.

Our people aren’t just employees, they are key to the success of our business. We recognize the strengths of each individual and allow them time and resources to further develop those skills, crafting a culture of leaders who are passionate about where they are going within our organization.