Salesforce has recently introduced this feature and its much needed one. Let’s understand couple of Terms before going into deep.
I am using TrailHead reference to write on this topic.
A change in state that is meaningful in a business process.
A message that contains data about the event. Also known as an event notification.
The publisher of an event message over a channel.
A stream of events on which an event producer sends event messages and event consumers read those messages. Also called an event bus in Salesforce.
A subscriber to a channel that receives messages from the channel.
Platform Events and sObjects
A platform event is a special kind of Salesforce entity, similar in many ways to an sObject.
An event message is an instance of a platform event, similar to how a record is an instance of a custom object. Unlike with custom objects, you can’t update or delete event records, or view event records in the Salesforce user interface.
You can set read and create permissions for platform events. You grant permissions to users in profiles or in permission sets.
Using Platform Events in Native and External Apps
Platform events enable the flow of event messages within Salesforce and to or from external apps.
Apps on the Salesforce platform use an Apex method to publish events and an Apex trigger to consume events. Also, Visualforce and Lightning component apps can subscribe to events using CometD.
As an alternative to code, you can publish events with declarative tools, such as Process Builder and Cloud Flow Designer.
Finally, external apps publish events using the sObject API and consume events using CometD clients.
Differences Between Platform Events and Other Streaming Events
Other events include PushTopic and generic events.
- With PushTopic events, clients receive messages about changes in Salesforce records based on a predefined query.
- With generic events, you can send and receive arbitrary message content (payloads) not necessarily tied to Salesforce records.
Platform events are similar to generic events but offer more powerful customization. With platform events, you can publish any custom data. You define the schema of event data at a granular level as typed fields. Also, you can use platform events in native Salesforce platform apps and external apps alike.
Use platform events in the following cases:
- To send and receive custom event data with a predefined schema
- To publish or subscribe to events in Apex
- For the flexibility of publishing and processing events on and off the Salesforce platform
Source : (Trailhead)
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.
Note : You can retrieve past events only when subscribing in CometD and using a ReplayId option.Even though Salesforce retains event records temporarily, you can’t query them through SOQL or SOSL. Similarly, you can’t use event records in the user interface in reports, list views, and search.
How to Publish Event: (Do Practice from TrailHead.)
- 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.
Unlike with custom objects, platform events aren’t processed within database transactions in the Salesforce platform. As a result, published platform events can’t be rolled back.
- 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.
When you publish platform events, DML limits and other Apex governor limits apply.
How to Subscribe to Events :
- 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.