Salesforce has recently introduced this feature and its much needed one. Let’s understand couple of Terms before going into deep.
n
I am using TrailHead reference to write on this topic.
n
Event
n
A change in state that is meaningful in a business process.
n
Event message
n
A message that contains data about the event. Also known as an event notification.
n
Event producer
n
The publisher of an event message over a channel.
n
Event channel
n
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.
n
Event consumer
n
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.
n
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.
n
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.
n
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.
n
As an alternative to code, you can publish events with declarative tools, such as Process Builder and Cloud Flow Designer.
n
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.
- n
- 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.
n
n
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.
n
Use platform events in the following cases:
- n
- 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
n
n
n
n
n
n
n
n
n
n
Source : (Trailhead)
ReplayId System Field and Event Retention
- n
- 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.
n
n
n
n
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.
n
How to Publish Event: (Do Practice from TrailHead.)
- n
- 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.
n
n
n
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.
n
Note :
- n
- 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.
n
n
When you publish platform events, DML limits and other Apex governor limits apply.
n
How to Subscribe to Events :
- n
- Apex triggers receive event notifications.n
- 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.n
- 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.
n
n
n
n
n
n