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:
- Publish Events Messaging using APEX.
- Publish Events Messaging using Declarative tools (Process Builder or Cloud Flow Designer Tool / Visual Work flow).
- Publish Events Messaging using Salesforce API from external app.
Not supported in Platform event:
- The allOrNoneHeader API header is ignored when you publish platform events through the API.
- The Apex setSavepoint() and rollback() Database methods aren’t supported with platform events.
Subscription in Platform Event:
- Apex triggers receive event notifications.
- write an “after insert” Apex trigger on the event object to subscribe to incoming events.
- Triggers receive event notifications from various sources—whether they’re published through Apex or APIs.
- Visualforce and Lightning component apps receive events through CometD.
- CometD is a scalable HTTP-based event routing bus that uses an AJAX push technology pattern known as Comet.
- 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,
- Text Area (Long)
ReplayId System Field and Event Retention
- Salesforce stores platform events for 24 hours. You can retrieve stored events in API clients but not in Apex.
- Each event record contains a field, called ReplayID, that the system populates after the event is published.
- Each replay ID is guaranteed to be higher than the ID of the previous event, but not necessarily contiguous for consecutive events.
- 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:
- From Setup, Search Platform Events and Click on the Platform Events.
- Click on New button to create Platform Events.
- Provide the name for the Platform Events.
- Create a custom field to show the Notification from this field.
- Create an Apex Class to publish the Platform Event
- From Setup > App Manager > Click Edit on Service Console App.
- Click on Utility Bar Tab. Click on Add button and choose helpNotification component to add lightning component to Service Console App.
- Provide the component name as Help and check Load in background apps open option.
- Go to the Service Console App. You will see the notification as shown below and Help utility bar.