Platform Events in Salesforce

Platform Event is based on Event-Driven Architecture which enable apps to communicate inside and outside of Salesforce. Platform events are based on the publish/subscribe model and work directly with a message bus which handles the queue of incoming events and processes listening for them. This is built in real time integration patterns in the Salesforce Platform which helps to reduce point-to-point integration.

What Are Platform Events in Salesforce?

Platform Events are used to deliver notification within Salesforce or external app. It is based on Event-Driven Architecture. Platform event are helpful to overcome with point to point Salesforce Integration challenges.

Learn about how to Integrating with Salesforce using Platform Events.

Platform Events Terminology

Here is some terminology we should remember.

  • Event: A change in state that is meaningful in a business process.
  • Event message/Notification: A message that contains data about the event.
  • Event producer: The publisher of an event message over a channel.
  • Channel: A conduit in which an event producer transmits a message. Event consumers subscribe to the channel to receive messages. Also referred to as event bus in Salesforce.
  • Event consumer: A subscriber to a channel that receives messages from the channel.

Understanding of Platform Event

Let understand the platform event in details.

  • SObject like Salesforce Entity
    • Suffixed with __e
    • ReplayId fir replaying specific event
    • Only Checkbox, Date, Date/Time, Number, Text and Text Area field available.
  • Pub/Sub based communication
    • No Polling required.
  • Heterogeneous playloads
    • Define events with different playloads

Difference between SObject and Platform Events

SObjects__cPlatform_Events__e
DMLs (Insert, Update, Delete)Publish (Insert only)
SOQLStreaming API
TriggersSubscribers
Parallel context executionGuaranteed order of execution

Publishing / Subscribing Platform Events

Let understand how we cab publish and subscribe the platform events in Salesforce.

Publishing / Subscribing Platform Events

Publish Platform Events

You can publish event messages from a Force.com app or an external app using Apex or Salesforce APIs and you can subscribe from the Salesforce or external apps or use long polling with cometD as well. 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 );
orderShippingList.add(orderShipping);

List<Database.SaveResult> results = EventBus.publish(orderShippingList);

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. Flow/ Process Builder

Publish Events Messaging using Declarative tools

Learn more about Platform Event Trigger flow.

3. Events Messaging using Salesforce API from external app

You can Publish Events Messaging using Salesforce API from external app. Here is sample of executing it from Salesforce workbench.

Subscribing Platform Events

We can subscribe the platform events with following ways.

1. Apex Trigger

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.

Trigger OrderShippingTrigger on Order_Shipping__e (after Insert) {
}

2. Subscribe to platform event notification in Lightning components

Lightning web components

Use the empApi methods in your Lightning web component, import the methods from the lightning/empApi module as follows.

import { subscribe, unsubscribe, onError, setDebugFlag, isEmpEnabled }
    from 'lightning/empApi';

Subscibe in an Aura Component

Use the empApi methods in your Aura component, add the lightning:empApi component inside your custom component and assign an aura:id attribute to it.

<lightning:empApi aura:id="empApi"/>

3. In an external app

you subscribe to events using CometD as well.

4. Flow and process builder.

Check this post.

Check our FREE Salesforce Training

10+ FREE Salesforce Training

When to use platform events in Salesforce

Overcome Salesforce Governor Limits Using Platform Events. Mixed DML’S Operations, Too Many SOQL Queries, Too Many DML Statements, CPU Timeout: Salesforce’s Governor limits are there for a reason but even when you employ best practices you may still exceed them. A good developer will look at all tools available on the platform and find the best approach to solving the problem they are facing.

YouTube video

Platform Events Considerations

  1. Platform event is appended with__e suffix for API name of the event.
  2. You can not query Platform events through SOQL or SOSL.
  3. You can not use Platform in reports, list views, and search. Platform events don’t have an associated tab.
  4. Published platform events can’t be rolled back.
  5. All platform event fields are read-only by default.
  6. Only after insert Triggers Are Supported.
  7. You can access platform events both through API and declaratively.
  8. You can control platform events though Profiles and permissions.

Summary

Platform events simplify the process of communicating changes and responding to events. Platform events can be used to Overcome Salesforce Governor Limits.

Amit Chaudhary
Amit Chaudhary

Amit Chaudhary is Salesforce Application & System Architect and working on Salesforce Platform since 2010. He is Salesforce MVP since 2017 and have 17 Salesforce Certificates.

He is a active blogger and founder of Apex Hours.

Articles: 473

2 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *