Platform Events in Salesforce

Learn All About Platform Events in Salesforce | The Developer Guide

Platform events are based on an event-driven architecture for delivering secure, extensible, and customizable notifications in Salesforce or external applications

This is a real-time integration model established in the Salesforce platform, which helps to decrease point-to-point integration.

Publishing Platform Event

We can publish the platform events in 3 ways:

1. Publish Events Messaging using APEX.

List<Order_Shipping__e> orderShippingList = new List<Order_Shipping__e>();
Order_Shipping__e orderShipping = new Order_Shipping__e( Order_Number__c='12345', status__c=1 );
List<Database.SaveResult> results = EventBus.publish(newsEventList);
for (Database.SaveResult sr : results) {
    if (sr.isSuccess()) {
        System.debug('Successfully published event.');
    } else {
        for(Database.Error err : sr.getErrors()) {
            System.debug('Error returned: ' + err.getStatusCode() );

2. Use declarative tools (flow builder or Cloud Flow Designer tool/visual workflow) to publish event messages.

3. Use Salesforce API from external applications to publish event messages.

dont miss out iconDon't forget to check out: Salesforce Flows | How to Loop on Multi-Select Picklist Values in a Flow?

Platform Events do Not Support

  1. When publishing platform events via API, the allOrNoneHeaderAPI header will be ignored.
  2. Platform events do not support ApexsetSavepoint() and rollback() database methods.

Subscription in Platform Event

  1. Apex triggers receive event notifications.
  2. Write an "after -insertion" Apex trigger on the event object to subscribe to the incoming event.
  3. Triggers receive event notifications from various sources, regardless of whether they are published via Apex or API.
  4. Visualforce and Lightning component applications receive events through CometD.
  5. CometD is a scalable HTTP-based event routing bus, which uses the AJAX push technology model called Comet.
  6. In external applications, you can also use CometD to subscribe to events.

Defining Objects and Fields in Platform Events

We can create platform events like custom objects. The biggest difference between Platform events and Custom objects is the suffix name of the API name. In platform events, the API name suffixes __e, and the custom object creates the API name after the __c suffix. Unlike custom objects, we cannot update or delete event records or view event records in the 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)

dont miss out iconCheck out another amazing blog by Kirandeep here: Learn All About Flows and Plugins in Salesforce | The Developer Guide

ReplayId System Field and Event Retention

  1. Salesforce stores platform events for 24 hours. You can retrieve stored events in the API client, but not in Apex.
  2. Each event record contains a field called ReplayID, which is filled by the system after the event is published
  3. Ensure that each replay ID is higher than the ID of the previous event, but it is not necessarily continuous for consecutive events.
  4. You can retrieve all stored events, or specify the replay ID of the event as the baseline for the retrieved event part.


Popular Salesforce Blogs