Newer Version Available
Enterprise Messaging Platform Events
For example, a software system can send events containing information about printer ink cartridges. Subscribers can subscribe to the events to monitor printer ink levels and place orders to replace cartridges with low ink levels.
Custom Platform Events
Use custom platform events to publish and process custom notifications. For example, publish custom platform events to send order information to an order fulfillment service. Or publish custom platform events to send printer ink information that is processed by a service app.
You define a custom platform event in Salesforce in the same way that you define a custom object. Create a platform event definition by giving it a name and adding custom fields. Platform events support a subset of field types in Salesforce. See Platform Event Fields. This table lists a sample definition of custom fields for a printer ink event.
| Field Name | Field API Name | Field Type |
|---|---|---|
| Printer Model | Printer_Model__c | Text |
| Serial Number | Serial_Number__c | Text |
| Ink Percentage | Ink_Percentage__c | Number |
You can publish custom platform events on the Lightning Platform by using Apex or point-and-click tools, such as Process Builder and Flow Builder, or an API in external apps. Similarly, you can subscribe to an event channel either on the platform through an Apex trigger or point-and-click tools, or in external apps using the CometD-based Streaming API. When an app publishes an event message, event subscribers receive the event message and execute business logic. Using the printer ink example, a software system monitoring a printer makes an API call to publish an event when the ink is low. The printer event message contains the printer model, serial number, and ink level. After the printer sends the event message, an Apex trigger is fired in Salesforce. The trigger creates a case record to place an order for a new cartridge.
Standard Platform Events
Salesforce provides events with predefined fields, called standard platform events. An example of a standard platform event is AssetTokenEvent, which monitors OAuth 2.0 authentication activity. Another example is BatchApexErrorEvent, which reports errors encountered in batch Apex jobs.
Salesforce publishes standard platform events in response to an action that occurred in the app or errors in batch Apex jobs. You can subscribe to a standard platform event stream using the subscription mechanism that the event supports.
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 or standard object. Unlike custom or standard objects, you can’t update or delete event records. You also can’t view event records in the Salesforce user interface, and platform events don’t have page layouts. When you delete a platform event definition, it’s permanently deleted.
You can set read and create permissions for platform events. Grant permissions to users in profiles or in permission sets.
Platform Events and Transactions
Unlike custom objects, platform events aren’t processed within database transactions in the Lightning Platform. As a result, published platform events can’t be rolled back. Note the following:
- The allOrNoneHeader API header is ignored when publishing platform events through the API.
- The Apex setSavepoint() and rollback() Database methods aren’t supported with platform events.
- The publishing of high-volume platform events is asynchronous. For more information, see Asynchronous Publishing.
When publishing platform events, DML limits and other Apex governor limits apply.
Event Retention
High-volume platform event messages are stored for 72 hours (3 days). Standard-volume platform event messages are stored for 24 hours (1 day). You can retrieve past event messages when using CometD clients to subscribe to a channel.
High-Volume Platform Events
Use high-volume platform events to publish and process millions of events efficiently and to scale your event-based apps. Previously, standard-volume events were available. In API version 45.0 and later, your new custom event definitions are high volume by default. Standard-volume events are still supported but not available for new event definitions. High-volume platform events offer better scalability than standard-volume platform events.
Note the following characteristics of high-volume platform events.
- Asynchronous Publishing
- For efficient processing of high loads of incoming event messages, high-volume platform events are published asynchronously. After the publishing call returns with a successful result, the publish request is queued in Salesforce. The event message might not be published immediately. For more information, see High-Volume Platform Event Persistence.
- Separate Event Allocations
- Each Salesforce edition provides default allocations and usage-based entitlements for the number of high-volume events delivered monthly to CometD clients. See Platform Event Allocations.